-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (110 loc) · 3.17 KB
/
main.go
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
package main
import (
"fmt"
sdfviewergo "github.com/Yeicor/sdf-viewer-go/sdf-viewer-go"
"math"
)
//export init
func init() {
// This is the only function you need to call to initialize the SDF Viewer.
sdfviewergo.SetRootSDF(sceneSDF())
}
func main() {
fmt.Println("This is not an executable. Compile this with `" +
"tinygo build -o example.wasm -target wasi -opt 2 -x -no-debug ." +
"` and use the SDF Viewer app (github.com/Yeicor/sdf-viewer) to visualize the SDF.")
}
// sceneSDF returns the root SDF of the scene.
func sceneSDF() sdfviewergo.SDF {
return &SampleSDF{isRoot: true, name: "test-root-cube", cubeHalfSide: 0.99, changed: false,
child: &SampleSDF{isRoot: false, name: "test-fake-child", cubeHalfSide: 0.51, changed: false}}
}
// ######################## START OF EXAMPLE MANUAL SDF IMPLEMENTATION ########################
// NOTE: Other modules of this repo have better examples of how to implement SDFs.
type SampleSDF struct {
isRoot bool
child *SampleSDF // May be nil
name string
cubeHalfSide float32 // Cube side length
changed bool // Whether any param changed, affecting the whole SDF
}
func (s *SampleSDF) AABB() [2][3]float32 {
return [2][3]float32{{-1, -1, -1}, {1, 1, 1}}
}
func (s *SampleSDF) Sample(point [3]float32, distanceOnly bool) (sample sdfviewergo.SDFSample) {
// Cube SDF
sample.Distance = maxF32(maxF32(absF32(point[0]), absF32(point[1])), absF32(point[2])) - s.cubeHalfSide
if !distanceOnly {
sample.Color = [3]float32{sinF32(point[0] * 2.0), (point[1] + 1.0) / 2.0, (point[2] + 1.0) / 2.0}
sample.Metallic = modF32(point[0], 1.0)
sample.Roughness = modF32(point[1], 1.0)
sample.Occlusion = modF32(point[2], 1.0)
}
return
}
func (s *SampleSDF) Children() []sdfviewergo.SDF {
if s.child != nil { // Fake, just for testing...
return []sdfviewergo.SDF{s.child}
}
return []sdfviewergo.SDF{}
}
func (s *SampleSDF) Name() string {
return s.name
}
func (s *SampleSDF) Parameters() []sdfviewergo.SDFParam {
if !s.isRoot {
return []sdfviewergo.SDFParam{}
}
return []sdfviewergo.SDFParam{
{
ID: 0,
Name: "Cube half side",
Kind: sdfviewergo.SDFParamKindFloat{
Min: 0.01,
Max: 0.99,
Step: 0.01,
},
Value: s.cubeHalfSide, // float32, as described
Description: "Half side length of the cube",
},
}
}
func (s *SampleSDF) SetParameter(paramId uint32, value sdfviewergo.SDFParamValue) error {
if paramId == 0 {
s.cubeHalfSide = value.(float32)
s.changed = true
return nil
}
return fmt.Errorf("unknown parameter id: %d", paramId)
}
func (s *SampleSDF) Changed() sdfviewergo.ChangedAABB {
changed := s.changed
s.changed = false
return sdfviewergo.ChangedAABB{
Changed: changed,
AABB: s.AABB(),
}
}
// ######################## END OF EXAMPLE MANUAL SDF IMPLEMENTATION ########################
func sinF32(f float32) float32 {
return float32(math.Sin(float64(f)))
}
func modF32(f float32, f2 float32) float32 {
res := float32(math.Mod(float64(f), float64(f2)))
if res < 0 {
return res + f2
}
return res
}
func maxF32(v1 float32, v2 float32) float32 {
if v1 > v2 {
return v1
}
return v2
}
func absF32(v float32) float32 {
if v < 0 {
return -v
}
return v
}