Skip to content

Commit

Permalink
fix: transform normals for lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 8, 2024
1 parent 4ac0822 commit 4c4d351
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lib/map/terrain/shader/vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const VERTEX_SHADER_PRECISION = 'highp float';
const VERTEX_SHADER_UNIFORMS = [
{ name: 'modelMatrix', type: 'mat4' },
{ name: 'modelViewMatrix', type: 'mat4' },
{ name: 'viewMatrix', type: 'mat4' },
{ name: 'normalMatrix', type: 'mat3' },
{ name: 'projectionMatrix', type: 'mat4' },
{ name: 'cameraPosition', type: 'vec3 ' },
];
Expand Down Expand Up @@ -42,7 +44,9 @@ vSplatCoord.yx = position.xy * splatScale.xy;
const VERTEX_SHADER_MAIN_LIGHTING = `
// TODO - Replace with lighting manager controlled value
vec3 lightDirection = vec3(-1, -1, -1);
vLight = dot(normal, -normalize(lightDirection));
vec3 viewLightDirection = (viewMatrix * vec4(lightDirection, 0.0)).xyz;
vec3 viewNormal = normalize(normalMatrix * normal);
vLight = clamp(dot(-viewLightDirection, viewNormal), 0.0, 1.0);
`;

const VERTEX_SHADER_MAIN_FOG = `
Expand Down
6 changes: 5 additions & 1 deletion src/lib/model/shader/vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const VERTEX_SHADER_PRECISION = 'highp float';
const VERTEX_SHADER_UNIFORMS = [
{ name: 'modelMatrix', type: 'mat4' },
{ name: 'modelViewMatrix', type: 'mat4' },
{ name: 'normalMatrix', type: 'mat3' },
{ name: 'viewMatrix', type: 'mat4' },
{ name: 'projectionMatrix', type: 'mat4' },
{ name: 'cameraPosition', type: 'vec3' },
];
Expand All @@ -28,7 +30,9 @@ const VERTEX_SHADER_FUNCTIONS = [];
const VERTEX_SHADER_MAIN_LIGHTING = `
// TODO - Replace with lighting manager controlled value
vec3 lightDirection = vec3(-1, -1, -1);
vLight = dot(normal, -normalize(lightDirection));
vec3 viewLightDirection = (viewMatrix * vec4(lightDirection, 0.0)).xyz;
vec3 viewNormal = normalize(normalMatrix * normal);
vLight = clamp(dot(-viewLightDirection, viewNormal), 0.0, 1.0);
`;

const VERTEX_SHADER_MAIN_FOG = `
Expand Down

0 comments on commit 4c4d351

Please sign in to comment.