diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index d36e12b04c..dbcfbca1b4 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -533,6 +533,7 @@ p5.RendererGL = class RendererGL extends p5.Renderer { this.uMVMatrix = new p5.Matrix(); this.uPMatrix = new p5.Matrix(); this.uNMatrix = new p5.Matrix('mat3'); + this.curMatrix = new p5.Matrix('mat3'); // Current vertex normal this._currentNormal = new p5.Vector(0, 0, 1); diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 3bb6c63a1a..7a25d40812 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -343,6 +343,11 @@ p5.Shader = class { this._renderer.uNMatrix.inverseTranspose(this._renderer.uMVMatrix); this.setUniform('uNormalMatrix', this._renderer.uNMatrix.mat3); } + if (this.uniforms.uCameraRotation) { + this._renderer.curMatrix.inverseTranspose(this._renderer. + _curCamera.cameraMatrix); + this.setUniform('uCameraRotation', this._renderer.curMatrix.mat3); + } } /** diff --git a/src/webgl/shaders/lighting.glsl b/src/webgl/shaders/lighting.glsl index 0a2eb26371..f95dd92733 100644 --- a/src/webgl/shaders/lighting.glsl +++ b/src/webgl/shaders/lighting.glsl @@ -9,7 +9,7 @@ uniform bool uUseLighting; uniform int uAmbientLightCount; uniform vec3 uAmbientColor[5]; - +uniform mat3 uCameraRotation; uniform int uDirectionalLightCount; uniform vec3 uLightingDirection[5]; uniform vec3 uDirectionalDiffuseColors[5]; @@ -112,7 +112,7 @@ vec2 mapTextureToNormal( vec3 v ){ vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){ // make 2 seperate builds vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); // hardcoded world camera position - vec3 worldNormal = normalize(vNormal); + vec3 worldNormal = normalize(vNormal * uCameraRotation); vec2 newTexCoor = mapTextureToNormal( worldNormal ); vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor ); // this is to make the darker sections more dark @@ -124,7 +124,7 @@ vec3 calculateImageSpecular( vec3 vNormal, vec3 vViewPosition ){ vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); vec3 worldNormal = normalize(vNormal); vec3 lightDirection = normalize( vViewPosition - worldCameraPosition ); - vec3 R = reflect(lightDirection, worldNormal); + vec3 R = reflect(lightDirection, worldNormal) * uCameraRotation; vec2 newTexCoor = mapTextureToNormal( R ); #ifdef WEBGL2 vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, levelOfDetail);