summaryrefslogtreecommitdiff
path: root/assets/final/shaders/mesh_render.wgsl
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-06 09:38:10 +0100
committerskal <pascal.massimino@gmail.com>2026-02-06 09:38:10 +0100
commit4e10d1c0b7718509ba8197b869fc88a5f94b0630 (patch)
tree94c793ef716e3aaf5fbf3e59baabb7bcdfefb7ef /assets/final/shaders/mesh_render.wgsl
parent7d60a8a9ece368e365b5c857600004298cb89526 (diff)
fix(shaders): Correct mesh normal transformation - remove double transpose
Fixed critical bug in normal matrix transformation causing mesh stretching and incorrect scaling during rotation. ROOT CAUSE: In WGSL, mat3x3(v0, v1, v2) creates a matrix where v0, v1, v2 are COLUMNS. When extracting rows from inv_model, the constructor already produces the transpose. Applying transpose() again cancels out, giving incorrect normals. ISSUE: let normal_matrix = mat3x3(inv_model[0].xyz, inv_model[1].xyz, inv_model[2].xyz); // This gives transpose(inv_model) already! out.normal = normalize(transpose(normal_matrix) * in.normal); // Double transpose = identity, wrong result FIX: let normal_matrix = mat3x3(inv_model[0].xyz, inv_model[1].xyz, inv_model[2].xyz); // Already transpose(inv_model), which is the correct normal matrix out.normal = normalize(normal_matrix * in.normal); // Correct transformation FILES FIXED: - assets/final/shaders/mesh_render.wgsl:38 (mesh vertex normals) - assets/final/shaders/renderer_3d.wgsl:185 (SDF bump-mapped normals) RESULT: Mesh normals now transform correctly under rotation and non-uniform scaling. Fixes Task A (test_mesh visualization stretching bug). handoff(Claude): Normal transformation bug fixed. Mesh should now render correctly without stretching. Shadow bug on floor plane still remains (separate WGSL shader issue for later investigation).
Diffstat (limited to 'assets/final/shaders/mesh_render.wgsl')
-rw-r--r--assets/final/shaders/mesh_render.wgsl3
1 files changed, 2 insertions, 1 deletions
diff --git a/assets/final/shaders/mesh_render.wgsl b/assets/final/shaders/mesh_render.wgsl
index 3faf7ca..068efbc 100644
--- a/assets/final/shaders/mesh_render.wgsl
+++ b/assets/final/shaders/mesh_render.wgsl
@@ -34,8 +34,9 @@ fn vs_main(in: VertexInput, @builtin(instance_index) instance_index: u32) -> Ver
out.world_pos = world_pos.xyz;
// Use transpose of inverse for normals
+ // Note: mat3x3 constructor takes columns, so passing rows gives us transpose
let normal_matrix = mat3x3<f32>(obj.inv_model[0].xyz, obj.inv_model[1].xyz, obj.inv_model[2].xyz);
- out.normal = normalize(transpose(normal_matrix) * in.normal);
+ out.normal = normalize(normal_matrix * in.normal);
out.uv = in.uv;
out.color = obj.color;