summaryrefslogtreecommitdiff
path: root/assets/final/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'assets/final/shaders')
-rw-r--r--assets/final/shaders/lighting.wgsl9
-rw-r--r--assets/final/shaders/math/sdf_utils.wgsl9
-rw-r--r--assets/final/shaders/render/scene_query_linear.wgsl58
-rw-r--r--assets/final/shaders/renderer_3d.wgsl19
4 files changed, 27 insertions, 68 deletions
diff --git a/assets/final/shaders/lighting.wgsl b/assets/final/shaders/lighting.wgsl
index 277909b..ac2142b 100644
--- a/assets/final/shaders/lighting.wgsl
+++ b/assets/final/shaders/lighting.wgsl
@@ -1,10 +1,11 @@
-fn get_normal_basic(p: vec3<f32>, obj_type: f32) -> vec3<f32> {
+fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> {
+ let obj_type = obj_params.x;
if (obj_type == 1.0) { return normalize(p); }
let e = vec2<f32>(0.001, 0.0);
return normalize(vec3<f32>(
- get_dist(p + e.xyy, obj_type) - get_dist(p - e.xyy, obj_type),
- get_dist(p + e.yxy, obj_type) - get_dist(p - e.yxy, obj_type),
- get_dist(p + e.yyx, obj_type) - get_dist(p - e.yyx, obj_type)
+ get_dist(p + e.xyy, obj_params) - get_dist(p - e.xyy, obj_params),
+ get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params),
+ get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params)
));
}
diff --git a/assets/final/shaders/math/sdf_utils.wgsl b/assets/final/shaders/math/sdf_utils.wgsl
index 502ba5b..c2e49cf 100644
--- a/assets/final/shaders/math/sdf_utils.wgsl
+++ b/assets/final/shaders/math/sdf_utils.wgsl
@@ -1,10 +1,11 @@
-fn get_normal_basic(p: vec3<f32>, obj_type: f32) -> vec3<f32> {
+fn get_normal_basic(p: vec3<f32>, obj_params: vec4<f32>) -> vec3<f32> {
+ let obj_type = obj_params.x;
if (obj_type == 1.0) { return normalize(p); }
let e = vec2<f32>(0.001, 0.0);
return normalize(vec3<f32>(
- get_dist(p + e.xyy, obj_type) - get_dist(p - e.xyy, obj_type),
- get_dist(p + e.yxy, obj_type) - get_dist(p - e.yxy, obj_type),
- get_dist(p + e.yyx, obj_type) - get_dist(p - e.yyx, obj_type)
+ get_dist(p + e.xyy, obj_params) - get_dist(p - e.xyy, obj_params),
+ get_dist(p + e.yxy, obj_params) - get_dist(p - e.yxy, obj_params),
+ get_dist(p + e.yyx, obj_params) - get_dist(p - e.yyx, obj_params)
));
}
diff --git a/assets/final/shaders/render/scene_query_linear.wgsl b/assets/final/shaders/render/scene_query_linear.wgsl
index ab3845a..b61a7e4 100644
--- a/assets/final/shaders/render/scene_query_linear.wgsl
+++ b/assets/final/shaders/render/scene_query_linear.wgsl
@@ -1,15 +1,6 @@
#include "math/sdf_shapes"
#include "math/sdf_utils"
-struct BVHNode {
- min: vec3<f32>,
- left_idx: i32,
- max: vec3<f32>,
- obj_idx_or_right: i32,
-};
-
-@group(0) @binding(2) var<storage, read> bvh_nodes: array<BVHNode>;
-
fn get_dist(p: vec3<f32>, obj_params: vec4<f32>) -> f32 {
let obj_type = obj_params.x;
if (obj_type == 1.0) { return length(p) - 1.0; } // Unit Sphere
@@ -22,42 +13,19 @@ fn get_dist(p: vec3<f32>, obj_params: vec4<f32>) -> f32 {
fn map_scene(p: vec3<f32>, skip_idx: u32) -> f32 {
var d = 1000.0;
- var stack: array<i32, 32>;
- var stack_ptr = 0;
-
- if (arrayLength(&bvh_nodes) > 0u) {
- stack[stack_ptr] = 0;
- stack_ptr++;
- }
-
- while (stack_ptr > 0) {
- stack_ptr--;
- let node_idx = stack[stack_ptr];
- let node = bvh_nodes[node_idx];
-
- if (aabb_sdf(p, node.min, node.max) < d) {
- if (node.left_idx < 0) { // Leaf
- let obj_idx = u32(node.obj_idx_or_right);
- if (obj_idx == skip_idx) { continue; }
- let obj = object_data.objects[obj_idx];
- let q = (obj.inv_model * vec4<f32>(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));
- }
- } else { // Internal
- if (stack_ptr < 31) {
- stack[stack_ptr] = node.left_idx;
- stack_ptr++;
- stack[stack_ptr] = node.obj_idx_or_right;
- stack_ptr++;
- }
- }
+ 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<f32>(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));
}
}
return d;
diff --git a/assets/final/shaders/renderer_3d.wgsl b/assets/final/shaders/renderer_3d.wgsl
index 2f4f79c..c290df8 100644
--- a/assets/final/shaders/renderer_3d.wgsl
+++ b/assets/final/shaders/renderer_3d.wgsl
@@ -65,21 +65,10 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32,
out.color = obj.color;
out.instance_index = instance_index;
out.world_pos = world_pos.xyz;
-
- // Correct normal transformation for meshes: transpose of inverse of model matrix
- // For non-uniform scaling, this is necessary. For other primitives, we use their analytical normals.
- if (obj_type == 5.0) {
- // Calculate inverse transpose of the model matrix (upper 3x3 part)
- let model_matrix = mat3x3<f32>(obj.model[0].xyz, obj.model[1].xyz, obj.model[2].xyz);
- let normal_matrix = transpose(inverse(model_matrix));
- out.transformed_normal = normalize(normal_matrix * in.normal);
- } else {
- // For SDF primitives, we don't use vertex normals directly here; they are computed in the fragment shader.
- // However, we still need to output a normal for the fragment shader to use if it were a rasterized primitive.
- // The transformed_normal is not used by the SDF fragment shader, but for correctness, we'll pass it.
- // If this were a rasterized mesh, it would be used.
- out.transformed_normal = normalize(vec3<f32>(0.0, 1.0, 0.0)); // Placeholder for non-mesh types
- }
+
+ // For SDF primitives, we don't use vertex normals - they are computed analytically in the fragment shader.
+ // This field is only used by the mesh pipeline (mesh_render.wgsl), not this SDF pipeline.
+ out.transformed_normal = normalize(vec3<f32>(0.0, 1.0, 0.0)); // Placeholder
return out;
}