From b8e6929cafa41681f0b27ac104c9cf1d4e510837 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 8 Feb 2026 07:38:28 +0100 Subject: feat(3d): Fix ObjectType::PLANE scaling and consolidate ObjectType mapping - Implemented correct scaling for planes in both CPU (physics) and GPU (shaders) using the normal-axis scale factor. - Consolidated ObjectType to type_id mapping in Renderer3D to ensure consistency and support for CUBE. - Fixed overestimation of distance for non-uniformly scaled ground planes, which caused missing shadows. - Updated documentation and marked Task A.2 as completed. --- assets/final/shaders/render/scene_query_bvh.wgsl | 19 +++++----- .../final/shaders/render/scene_query_linear.wgsl | 42 +++++++++++++++++----- 2 files changed, 44 insertions(+), 17 deletions(-) (limited to 'assets/final') diff --git a/assets/final/shaders/render/scene_query_bvh.wgsl b/assets/final/shaders/render/scene_query_bvh.wgsl index 61efe49..3e6f895 100644 --- a/assets/final/shaders/render/scene_query_bvh.wgsl +++ b/assets/final/shaders/render/scene_query_bvh.wgsl @@ -41,15 +41,18 @@ fn map_scene(p: vec3, skip_idx: u32) -> f32 { if (obj_idx == skip_idx) { continue; } 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) 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)); + + // Extract scale factors from the model matrix + let sx = length(obj.model[0].xyz); + let sy = length(obj.model[1].xyz); + let sz = length(obj.model[2].xyz); + + var s = min(sx, min(sy, sz)); + if (obj.params.x == 4.0) { + s = sy; // Plane normal is (0,1,0) in local space } + + d = min(d, get_dist(q, obj.params) * s); } else { // Internal if (stack_ptr < 31) { stack[stack_ptr] = node.left_idx; diff --git a/assets/final/shaders/render/scene_query_linear.wgsl b/assets/final/shaders/render/scene_query_linear.wgsl index b61a7e4..0497a40 100644 --- a/assets/final/shaders/render/scene_query_linear.wgsl +++ b/assets/final/shaders/render/scene_query_linear.wgsl @@ -12,21 +12,45 @@ fn get_dist(p: vec3, obj_params: vec4) -> f32 { } fn map_scene(p: vec3, skip_idx: u32) -> f32 { + var d = 1000.0; + let num_objects = arrayLength(&object_data.objects); + for (var i = 0u; i < num_objects; i++) { + if (i == skip_idx) { continue; } + let obj = object_data.objects[i]; + 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) 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)); + + + + // Extract scale factors from the model matrix + + let sx = length(obj.model[0].xyz); + + let sy = length(obj.model[1].xyz); + + let sz = length(obj.model[2].xyz); + + + + var s = min(sx, min(sy, sz)); + + if (obj.params.x == 4.0) { + + s = sy; // Plane normal is (0,1,0) in local space + } + + + + d = min(d, get_dist(q, obj.params) * s); + } + return d; -} \ No newline at end of file + +} -- cgit v1.2.3