From f573e9e12855f9a90878c57d1083985dc29d75b5 Mon Sep 17 00:00:00 2001 From: skal Date: Fri, 6 Feb 2026 09:46:10 +0100 Subject: fix(shaders): Exclude meshes from SDF scaling factor in shadow calculations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed incorrect mesh shadow rendering caused by applying scale factor to mesh AABBs which already have correct local-space extents. ROOT CAUSE: The 's' scale factor (line 44) is meant for UNIT primitives (sphere, box, torus) that are scaled by the model matrix. Meshes (type 5.0) already store their correct AABB extents in obj_params.yzw, so applying 's' caused incorrect shadow sizes. ISSUE: let s = min(length(obj.model[0].xyz), ...); if (obj.params.x != 4.0) { // Excluded planes only d = min(d, get_dist(q, obj.params) * s); // ❌ WRONG for meshes! } FIX: if (obj.params.x != 4.0 && obj.params.x != 5.0) { // Exclude planes AND meshes d = min(d, get_dist(q, obj.params) * s); } else { d = min(d, get_dist(q, obj.params)); // No scaling } EXPLANATION: - Type 1.0 (Sphere): Unit sphere, scaled by 's' ✓ - Type 2.0 (Box): Unit box, scaled by 's' ✓ - Type 3.0 (Torus): Unit torus, scaled by 's' ✓ - Type 4.0 (Plane): Special case, no scaling ✓ - Type 5.0 (Mesh): AABB already has correct size, no scaling needed ✓ FILES FIXED: - assets/final/shaders/render/scene_query_linear.wgsl - assets/final/shaders/render/scene_query_bvh.wgsl PARTIAL FIX: This fixes mesh shadow sizing issues. Shadow rotation limitation remains (AABBs are axis-aligned, don't rotate with mesh - this is a fundamental AABB limitation, not a bug). handoff(Claude): Mesh shadows now correctly sized. Investigating floor shadow stretching issue next (likely related to plane scaling). --- assets/final/shaders/render/scene_query_bvh.wgsl | 8 ++++---- assets/final/shaders/render/scene_query_linear.wgsl | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'assets') diff --git a/assets/final/shaders/render/scene_query_bvh.wgsl b/assets/final/shaders/render/scene_query_bvh.wgsl index d040c1b..61efe49 100644 --- a/assets/final/shaders/render/scene_query_bvh.wgsl +++ b/assets/final/shaders/render/scene_query_bvh.wgsl @@ -42,10 +42,10 @@ fn map_scene(p: vec3, skip_idx: u32) -> f32 { let obj = object_data.objects[obj_idx]; let q = (obj.inv_model * vec4(p, 1.0)).xyz; let s = min(length(obj.model[0].xyz), min(length(obj.model[1].xyz), length(obj.model[2].xyz))); - // IMPORTANT: Plane (type 4.0) should not be scaled by 's' in this way. - // The sdPlane function expects its own scale/offset implicitly handled by the model matrix. - // The 's' factor is meant for primitives whose SDFs are defined relative to a unit size. - if (obj.params.x != 4.0) { // Only scale if not a plane + // IMPORTANT: Plane (type 4.0) and Mesh (type 5.0) should not be scaled by 's'. + // The 's' factor is meant for unit primitives (sphere, box, torus) that are + // scaled by the model matrix. Meshes already have correct local-space extents. + if (obj.params.x != 4.0 && obj.params.x != 5.0) { // Not plane, not mesh d = min(d, get_dist(q, obj.params) * s); } else { d = min(d, get_dist(q, obj.params)); diff --git a/assets/final/shaders/render/scene_query_linear.wgsl b/assets/final/shaders/render/scene_query_linear.wgsl index 30a0371..ab3845a 100644 --- a/assets/final/shaders/render/scene_query_linear.wgsl +++ b/assets/final/shaders/render/scene_query_linear.wgsl @@ -42,10 +42,10 @@ fn map_scene(p: vec3, skip_idx: u32) -> f32 { let obj = object_data.objects[obj_idx]; let q = (obj.inv_model * vec4(p, 1.0)).xyz; let s = min(length(obj.model[0].xyz), min(length(obj.model[1].xyz), length(obj.model[2].xyz))); - // IMPORTANT: Plane (type 4.0) should not be scaled by 's' in this way. - // The sdPlane function expects its own scale/offset implicitly handled by the model matrix. - // The 's' factor is meant for primitives whose SDFs are defined relative to a unit size. - if (obj.params.x != 4.0) { // Only scale if not a plane + // IMPORTANT: Plane (type 4.0) and Mesh (type 5.0) should not be scaled by 's'. + // The 's' factor is meant for unit primitives (sphere, box, torus) that are + // scaled by the model matrix. Meshes already have correct local-space extents. + if (obj.params.x != 4.0 && obj.params.x != 5.0) { // Not plane, not mesh d = min(d, get_dist(q, obj.params) * s); } else { d = min(d, get_dist(q, obj.params)); -- cgit v1.2.3