Skip to content

Commit b17e40f

Browse files
committed
Move what group the normal matrix is in, dedup uniform
1 parent e53d5d1 commit b17e40f

4 files changed

Lines changed: 8 additions & 14 deletions

File tree

src/core/p5.Renderer3D.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,6 @@ export class Renderer3D extends Renderer {
14561456
this.scratchMat3.inverseTranspose4x4(this.states.uViewMatrix);
14571457
shader.setUniform("uCameraNormalMatrix", this.scratchMat3.mat3);
14581458
}
1459-
if (shader.uniforms.uCameraRotation) {
1460-
this.scratchMat3.inverseTranspose4x4(this.states.uViewMatrix);
1461-
shader.setUniform("uCameraRotation", this.scratchMat3.mat3);
1462-
}
14631459
shader.setUniform("uViewport", this._viewport);
14641460
}
14651461

src/webgl/shaders/lighting.glsl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ uniform mat4 uViewMatrix;
77

88
uniform bool uUseLighting;
99

10-
uniform mat3 uCameraRotation;
1110
uniform int uDirectionalLightCount;
1211
uniform vec3 uLightingDirection[5];
1312
uniform vec3 uDirectionalDiffuseColors[5];
@@ -108,7 +107,7 @@ vec2 mapTextureToNormal( vec3 v ){
108107
vec3 calculateImageDiffuse(vec3 vNormal, vec3 vViewPosition, float metallic){
109108
// make 2 seperate builds
110109
vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0); // hardcoded world camera position
111-
vec3 worldNormal = normalize(vNormal * uCameraRotation);
110+
vec3 worldNormal = normalize(vNormal * uCameraNormalMatrix);
112111
vec2 newTexCoor = mapTextureToNormal( worldNormal );
113112
vec4 texture = TEXTURE( environmentMapDiffused, newTexCoor );
114113
// this is to make the darker sections more dark
@@ -120,7 +119,7 @@ vec3 calculateImageSpecular(vec3 vNormal, vec3 vViewPosition, float shininess, f
120119
vec3 worldCameraPosition = vec3(0.0, 0.0, 0.0);
121120
vec3 worldNormal = normalize(vNormal);
122121
vec3 lightDirection = normalize( vViewPosition - worldCameraPosition );
123-
vec3 R = reflect(lightDirection, worldNormal) * uCameraRotation;
122+
vec3 R = reflect(lightDirection, worldNormal) * uCameraNormalMatrix;
124123
vec2 newTexCoor = mapTextureToNormal( R );
125124
#ifdef WEBGL2
126125
// In p5js the range of shininess is >= 1,

src/webgpu/shaders/color.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ struct CameraUniforms {
55
// @p5 ifdef Vertex getWorldInputs
66
uViewMatrix: mat4x4<f32>,
77
// @p5 endif
8+
uCameraNormalMatrix: mat3x3<f32>,
89
}
910
1011
// Group 2: Model Transform
1112
struct ModelUniforms {
1213
// @p5 ifdef Vertex getWorldInputs
1314
uModelMatrix: mat4x4<f32>,
1415
uModelNormalMatrix: mat3x3<f32>,
15-
uCameraNormalMatrix: mat3x3<f32>,
1616
// @p5 endif
1717
// @p5 ifndef Vertex getWorldInputs
1818
uModelViewMatrix: mat4x4<f32>,
@@ -80,7 +80,7 @@ fn main(input: VertexInput) -> VertexOutput {
8080
// @p5 ifdef Vertex getWorldInputs
8181
// Already multiplied by the model matrix, just apply view
8282
inputs.position = (camera.uViewMatrix * vec4<f32>(inputs.position, 1.0)).xyz;
83-
inputs.normal = model.uCameraNormalMatrix * inputs.normal;
83+
inputs.normal = camera.uCameraNormalMatrix * inputs.normal;
8484
// @p5 endif
8585
// @p5 ifndef Vertex getWorldInputs
8686
// Apply both at once

src/webgpu/shaders/material.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ const uniforms = `
33
struct CameraUniforms {
44
uViewMatrix: mat4x4<f32>,
55
uProjectionMatrix: mat4x4<f32>,
6-
uCameraRotation: mat3x3<f32>,
6+
uCameraNormalMatrix: mat3x3<f32>,
77
}
88
99
// Group 2: Model Transform
1010
struct ModelUniforms {
1111
// @p5 ifdef Vertex getWorldInputs
1212
uModelMatrix: mat4x4<f32>,
1313
uModelNormalMatrix: mat3x3<f32>,
14-
uCameraNormalMatrix: mat3x3<f32>,
1514
// @p5 endif
1615
// @p5 ifndef Vertex getWorldInputs
1716
uModelViewMatrix: mat4x4<f32>,
@@ -115,7 +114,7 @@ fn main(input: VertexInput) -> VertexOutput {
115114
// @p5 ifdef Vertex getWorldInputs
116115
// Already multiplied by the model matrix, just apply view
117116
inputs.position = (camera.uViewMatrix * vec4<f32>(inputs.position, 1.0)).xyz;
118-
inputs.normal = model.uCameraNormalMatrix * inputs.normal;
117+
inputs.normal = camera.uCameraNormalMatrix * inputs.normal;
119118
// @p5 endif
120119
// @p5 ifndef Vertex getWorldInputs
121120
// Apply both at once
@@ -222,7 +221,7 @@ fn mapTextureToNormal(v: vec3<f32>) -> vec2<f32> {
222221
fn calculateImageDiffuse(vNormal: vec3<f32>, vViewPosition: vec3<f32>, metallic: f32) -> vec3<f32> {
223222
// make 2 seperate builds
224223
let worldCameraPosition = vec3<f32>(0.0, 0.0, 0.0); // hardcoded world camera position
225-
let worldNormal = normalize(vNormal * camera.uCameraRotation);
224+
let worldNormal = normalize(vNormal * camera.uCameraNormalMatrix);
226225
let newTexCoord = mapTextureToNormal(worldNormal);
227226
let texture = textureSample(environmentMapDiffused, environmentMapDiffused_sampler, newTexCoord);
228227
// this is to make the darker sections more dark
@@ -234,7 +233,7 @@ fn calculateImageSpecular(vNormal: vec3<f32>, vViewPosition: vec3<f32>, shinines
234233
let worldCameraPosition = vec3<f32>(0.0, 0.0, 0.0);
235234
let worldNormal = normalize(vNormal);
236235
let lightDirection = normalize(vViewPosition - worldCameraPosition);
237-
let R = reflect(lightDirection, worldNormal) * camera.uCameraRotation;
236+
let R = reflect(lightDirection, worldNormal) * camera.uCameraNormalMatrix;
238237
let newTexCoord = mapTextureToNormal(R);
239238
240239
// In p5js the range of shininess is >= 1,

0 commit comments

Comments
 (0)