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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 | import {
Break,
cos,
float,
If,
Loop,
normalize,
sin,
time,
uniform,
vec2,
vec3,
Fn,
positionLocal,
reflect,
mix,
exp,
int,
abs
} from 'three/tsl'
import { atmosphericScattering } from './AtmosphericScattering'
import Land from './Land'
import ShadowMarcher from './ShadowMarcher'
import AmbientOcclusion from './AmbientOcclusion'
import Fog from './Fog'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'
// import Ocean from './Ocean'
// import Fresnel from './Fresnel'
export default class SDFScene {
private static options = {
maxSteps: 384,
surfaceDistance: 0.001,
cameraNear: 0.01,
cameraFar: 192.0,
reflectivity: 0.5
}
private static maxSteps = uniform(this.options.maxSteps)
private static surfaceDistance = uniform(this.options.surfaceDistance)
private static cameraNear = uniform(this.options.cameraNear)
private static cameraFar = uniform(this.options.cameraFar)
private static reflectivity = uniform(this.options.reflectivity)
// @ts-ignore
private static scene = Fn(([position]) => {
const land = Land.scene(position)
//const ocean = Ocean.scene(position)
const distance = vec2(land, 0).toVar()
// If(distance.x.greaterThan(ocean), () => {
// distance.assign(vec2(ocean, 1))
// })
return distance
})
// @ts-ignore
private static getNormal = Fn(([position, distance]) => {
const offset = vec2(0.0001, 0)
return normalize(
distance.sub(
vec3(
this.scene(position.sub(offset.xyy)).x,
this.scene(position.sub(offset.yxy)).x,
this.scene(position.sub(offset.yyx)).x
)
)
)
})
// @ts-ignore
static render = Fn(([rayOrigin_immutable]) => {
const rayOrigin = rayOrigin_immutable.toVar()
const p = positionLocal
const rayDirection = normalize(p).toVar()
const t = time.div(5)
//const lightPosition = vec3(0, 50, this.cameraFar.negate())
//const lightPosition = vec3(0, sin(t).mul(30).add(43), this.cameraFar.negate())
const lightPosition = vec3(sin(t).mul(this.cameraFar), 50, cos(t).mul(this.cameraFar))
//const lightPosition = vec3(sin(t).mul(this.cameraFar), sin(t).mul(40).add(50), cos(t).mul(this.cameraFar))
const lightDirection = normalize(lightPosition.sub(p))
const skyColour = atmosphericScattering(p, normalize(lightPosition)).toVar()
//const skyColour = atmosphericScattering(vec3(p.x, abs(p.y), p.z), normalize(lightPosition)).toVar()
const finalColour = skyColour.toVar()
//const reflectivityFactor = float(1.0).toVar()
const accumulatedDistance = float(this.cameraNear).toVar()
//const fogDistance = accumulatedDistance.toVar()
//const maxReflections = int(1).toVar()
//Loop({ start: 0, end: maxReflections, condition: '<=' }, () => {
accumulatedDistance.assign(0)
const distance = vec2(0).toVar()
const position = vec3(0).toVar()
// @ts-ignore
Loop({ start: 0, end: this.maxSteps }, () => {
position.assign(rayOrigin.add(rayDirection.mul(accumulatedDistance)))
distance.assign(this.scene(position))
If(abs(distance.x).lessThan(this.surfaceDistance).or(accumulatedDistance.greaterThan(this.cameraFar)), () => {
Break()
})
accumulatedDistance.addAssign(distance.x)
//fogDistance.addAssign(distance.x)
})
const normal = this.getNormal(position, distance.x).toVar()
If(accumulatedDistance.lessThan(this.cameraFar), () => {
//If(distance.y.equal(0), () => {
// land
finalColour.assign(Land.render(position, normal, rayDirection, lightPosition, lightDirection, this.scene))
// finalColour.assign(
// mix(
// finalColour,
// Land.render(position, normal, rayDirection, lightPosition, lightDirection, this.scene),
// reflectivityFactor
// )
// )
//maxReflections.assign(0) //do not calc reflections on land layer
//})
// .Else(() => {
// // ocean
// //normal.assign(mix(vec3(0, 1, 0), normal, exp(accumulatedDistance.pow(3).mul(-0.005))))
// // finalColour.assign(
// // Ocean.render(finalColour, position, normal, rayDirection, lightPosition, lightDirection, this.scene)
// // )
// rayOrigin.assign(position.add(normal.mul(this.surfaceDistance)))
// rayDirection.assign(reflect(rayDirection, normal))
// //reflectivityFactor.assign(this.reflectivity)
// })
// fog
finalColour.assign(Fog.render(skyColour, finalColour, accumulatedDistance))
//finalColour.assign(Fog.render(skyColour, finalColour, fogDistance))
})
//})
return finalColour
})
static setGUI(gui: GUI) {
const folder = gui.addFolder('SDF Scene')
folder
.add(this.options, 'maxSteps', 1, 512, 1)
.name('Raymarch Max Steps')
.onChange((v) => {
this.maxSteps.value = v
})
folder
.add(this.options, 'surfaceDistance', 0, 0.01, 0.0001)
.name('Surface Distance')
.onChange((v) => {
this.surfaceDistance.value = v
})
folder
.add(this.options, 'cameraNear', 0.0001, 10, 0.1)
.name('Camera Near')
.onChange((v) => {
this.cameraNear.value = v
})
folder
.add(this.options, 'cameraFar', 1, 512, 0.1)
.name('Camera Far')
.onChange((v) => {
this.cameraFar.value = v
})
folder
.add(this.options, 'reflectivity', 0, 1, 0.01)
.name('Reflectivity')
.onChange((v) => {
this.reflectivity.value = v
})
folder.close()
ShadowMarcher.setGUI(gui)
AmbientOcclusion.setGUI(gui)
Land.setGUI(gui)
//Ocean.setGUI(gui)
//Fresnel.setGUI(gui)
Fog.setGUI(gui)
}
}
|