rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_voxelsdf.sc -rw-r--r-- 4.1 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env scopes

using import Array
using import glm
using import glsl
using import ..tukan.gl
using import ..tukan.sdf
using import ..tukan.rotation
using import ..tukan.color
using import ..tukan.packing
using import ..tukan.sdl
using import ..tukan.thread
using import .testfragment

fn map (p)
    sdOr
        sdBox
            p -
                vec3 1.0 -0.5 1.0
            vec3 0.5
        sdUberprim p
            vec4 1.0 1.0 0.25 0.25
            vec3 1.0 0.25 0.0
        sdPlane p
            vec4 0.0 1.0 0.0 1.0

render-fragment-shader
    fn ()
        fn per-frame-setup ()

        let FAR_LIMIT = 100.0
        let MAX_STEPS = 50
        let CONTACT_LIMIT = 0.001
        fn raymarch (mapf ro rd)
            loop (i t) = 0 0.0
            let p = (ro + rd * t)
            let d = (mapf p)
            if ((d > CONTACT_LIMIT) & (t < FAR_LIMIT) & (i < MAX_STEPS))
                repeat (i + 1) (t + d)
            return p (not (t >= FAR_LIMIT))

        fn raymarch-softshadow (mapf ro rd opts...)
            let getopt = (gen-get-option opts...)
            let mint = (getopt 'min-t 0.05)
            let maxt = (getopt 'max-t FAR_LIMIT)
            let k = (/ (getopt 'aperture 0.03))
            loop (i t res ph) = 0 mint 1.0 1e+20
            let p = (ro + rd * t)
            let h = (mapf p)
            if ((h > CONTACT_LIMIT) & (t < maxt) & (i < MAX_STEPS))
                let y =
                    h * h / (2.0 * ph)
                let d =
                    sqrt (h * h - y * y)
                repeat (i + 1) (t + h)
                    min res (k * d / (max 0.0 (t - y)))
                    h
            ? (h <= CONTACT_LIMIT) 0.0 res

        fn raymarch-ambient-occlusion (mapf ro rd)
            let N = 5
            loop (i occ sca) = 0 0.0 1.0
            if (i < N)
                let hr =
                    0.01 + 0.2 * (f32 i) / (f32 (N - 1))
                let aopos = (rd * hr + ro)
                let dd = (mapf aopos)
                repeat (i + 1)
                    occ - (dd - hr) * sca
                    sca * 0.95
            clamp
                1.0 - occ
                \ 0.0 1.0

        fn shader (uv)
            let t =
                shglobals.time as immutable
            let uv =
                (uv * 2.0 - 1.0) * (vec2 shglobals.aspect 1)
            let yaw =
                versor (vec3 0 1 0)
                    * 0.5 t
            let pitch =
                versor-rotate yaw
                    versor (vec3 1 0 0) 0.5
            let ro = (vec3 0)
            let rd =
                normalize
                    vec3 uv 1
            let ro =
                versor-rotate yaw
                    + ro
                        vec3 0 1 -3
            let rd =
                versor-rotate pitch rd
            let p hit =
                raymarch map ro rd
            let color =
                if hit
                    let sundir =
                        normalize
                            vec3 1 -2 3
                    let suncolor =
                        vec3 0.8 0.2 0.01
                    let skycolor =
                        vec3 0.2 0.5 1.0
                    let rsundir = (- sundir)
                    let n = (sdNormal map p)
                    let shadowing =
                        raymarch-softshadow map p rsundir
                            #aperture =
                                mix 0.03
                                    0.5
                                    (sin (t * 10.0)) * 0.5 + 0.5

                    let ao =
                        raymarch-ambient-occlusion map p n
                    +
                        *
                            max 0.0
                                dot n rsundir
                            shadowing
                            suncolor
                        * ao
                            skycolor * 0.2
                            (dot n rsundir) * 0.5 + 0.5

                else
                    unconst (vec3 0)
            vec4
                linear->sRGB
                    tonemap color
                1

        return
            per-frame-setup
            shader
    debug = true