Skip to content

Commit

Permalink
Fixed Camera
Browse files Browse the repository at this point in the history
  • Loading branch information
perminder-17 committed Jan 13, 2024
1 parent 5ad1179 commit 9cdd081
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/webgl/shaders/lighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9cdd081

Please sign in to comment.