summaryrefslogtreecommitdiff
path: root/workspaces/main/shaders/sdf_test.wgsl
blob: 3c97613a759263c41ecb975aecdf06d88c73531b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SDF Test Effect - demonstrates SDFEffect base class usage
// Simple scene with a sphere and box

#include "common_uniforms"
#include "camera_common"
#include "math/sdf_shapes"
#include "render/raymarching"

@group(0) @binding(0) var<uniform> uniforms: CommonUniforms;
@group(0) @binding(1) var<uniform> camera: CameraParams;

// Scene distance function
fn df(p: vec3<f32>) -> f32 {
    // Animated sphere
    let sphere_pos = vec3<f32>(sin(uniforms.beat_time * 0.5) * 2.0, 0.0, 0.0);
    let d_sphere = sdSphere(p - sphere_pos, 1.0);

    // Static box
    let box_pos = vec3<f32>(0.0, -2.0, 0.0);
    let d_box = sdBox(p - box_pos, vec3<f32>(3.0, 0.5, 3.0));

    return min(d_sphere, d_box);
}

// Simple lighting
fn shade(pos: vec3<f32>, rd: vec3<f32>) -> vec3<f32> {
    let n = normal(pos);
    let light_dir = normalize(vec3<f32>(1.0, 1.0, -1.0));
    let diff = max(dot(n, light_dir), 0.0);
    let amb = 0.2;

    // Color based on position
    let col = mix(vec3<f32>(0.2, 0.6, 0.9), vec3<f32>(0.9, 0.3, 0.2),
                  smoothstep(-2.0, 2.0, pos.x));

    return col * (diff + amb);
}

@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> @builtin(position) vec4<f32> {
    // Fullscreen triangle
    let x = f32((vid & 1u) << 2u) - 1.0;
    let y = f32((vid & 2u) << 1u) - 1.0;
    return vec4<f32>(x, y, 0.0, 1.0);
}

@fragment
fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
    // UV coordinates (-1 to 1)
    let uv = (pos.xy / uniforms.resolution - 0.5) * 2.0;

    // Generate ray
    let ray = getCameraRay(camera, uv);

    // Raymarch
    let t = rayMarch(ray.origin, ray.direction, 0.0);

    // Background color
    var col = vec3<f32>(0.1, 0.1, 0.15);

    // Shade hit point
    if (t < MAX_RAY_LENGTH) {
        let hit_pos = ray.origin + ray.direction * t;
        col = shade(hit_pos, ray.direction);
    }

    return vec4<f32>(col, 1.0);
}