From 9cdd081620e7286efd099ee983ece4c03b008c0a Mon Sep 17 00:00:00 2001 From: Perminder Singh Date: Sat, 13 Jan 2024 17:14:28 +0530 Subject: [PATCH 1/4] Fixed Camera --- src/webgl/p5.Shader.js | 3 +++ src/webgl/shaders/lighting.glsl | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 3bb6c63a1a..64a8e06b39 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -343,6 +343,9 @@ p5.Shader = class { this._renderer.uNMatrix.inverseTranspose(this._renderer.uMVMatrix); this.setUniform('uNormalMatrix', this._renderer.uNMatrix.mat3); } + const cameraMat = this._renderer._curCamera.cameraMatrix; + const camMat3x3 = cameraMat.createSubMatrix3x3(); + this.setUniform('uCameraMatrix', camMat3x3.mat3); } /** diff --git a/src/webgl/shaders/lighting.glsl b/src/webgl/shaders/lighting.glsl index 131a0767e1..f222cbb2b5 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 uCameraMatrix; uniform int uDirectionalLightCount; uniform vec3 uLightingDirection[5]; uniform vec3 uDirectionalDiffuseColors[5]; @@ -109,7 +109,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 * uCameraMatrix); vec2 newTexCoor = mapTextureToNormal( worldNormal ); vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor ); // this is to make the darker sections more dark @@ -119,8 +119,8 @@ vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){ 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 worldNormal = normalize(vNormal * uCameraMatrix); + vec3 lightDirection = normalize( vViewPosition * uCameraMatrix - worldCameraPosition ); vec3 R = reflect(lightDirection, worldNormal); vec2 newTexCoor = mapTextureToNormal( R ); #ifdef WEBGL2 From a99faf6ace26459f6f403bdf72d56d47df39a379 Mon Sep 17 00:00:00 2001 From: Perminder Singh <127239756+perminder-17@users.noreply.github.com> Date: Tue, 16 Jan 2024 02:38:16 +0530 Subject: [PATCH 2/4] fixes --- src/webgl/p5.Shader.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 64a8e06b39..7a25d40812 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -343,9 +343,11 @@ p5.Shader = class { this._renderer.uNMatrix.inverseTranspose(this._renderer.uMVMatrix); this.setUniform('uNormalMatrix', this._renderer.uNMatrix.mat3); } - const cameraMat = this._renderer._curCamera.cameraMatrix; - const camMat3x3 = cameraMat.createSubMatrix3x3(); - this.setUniform('uCameraMatrix', camMat3x3.mat3); + if (this.uniforms.uCameraRotation) { + this._renderer.curMatrix.inverseTranspose(this._renderer. + _curCamera.cameraMatrix); + this.setUniform('uCameraRotation', this._renderer.curMatrix.mat3); + } } /** From 2cc59a967b3b78798c51baf77800fb1587f7046c Mon Sep 17 00:00:00 2001 From: Perminder Singh <127239756+perminder-17@users.noreply.github.com> Date: Tue, 16 Jan 2024 02:41:21 +0530 Subject: [PATCH 3/4] fixes --- src/webgl/p5.RendererGL.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 097c9ab8c1..f2a7363d1b 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -529,6 +529,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); From bac0ec4718efdce998f5552cfe9b7346b6351dbe Mon Sep 17 00:00:00 2001 From: Perminder Singh <127239756+perminder-17@users.noreply.github.com> Date: Tue, 16 Jan 2024 02:43:44 +0530 Subject: [PATCH 4/4] fixes --- src/webgl/shaders/lighting.glsl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/webgl/shaders/lighting.glsl b/src/webgl/shaders/lighting.glsl index f222cbb2b5..aa783df1fc 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 uCameraMatrix; +uniform mat3 uCameraRotation; uniform int uDirectionalLightCount; uniform vec3 uLightingDirection[5]; uniform vec3 uDirectionalDiffuseColors[5]; @@ -109,7 +109,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 * uCameraMatrix); + vec3 worldNormal = normalize(vNormal * uCameraRotation); vec2 newTexCoor = mapTextureToNormal( worldNormal ); vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor ); // this is to make the darker sections more dark @@ -119,9 +119,9 @@ vec3 calculateImageDiffuse( vec3 vNormal, vec3 vViewPosition ){ vec3 calculateImageSpecular( vec3 vNormal, vec3 vViewPosition ){ vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); - vec3 worldNormal = normalize(vNormal * uCameraMatrix); - vec3 lightDirection = normalize( vViewPosition * uCameraMatrix - worldCameraPosition ); - vec3 R = reflect(lightDirection, worldNormal); + vec3 worldNormal = normalize(vNormal); + vec3 lightDirection = normalize( vViewPosition - worldCameraPosition ); + vec3 R = reflect(lightDirection, worldNormal) * uCameraRotation; vec2 newTexCoor = mapTextureToNormal( R ); #ifdef WEBGL2 vec4 outColor = textureLod(environmentMapSpecular, newTexCoor, levelOfDetail);