-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
302 lines (269 loc) · 7.83 KB
/
main.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
import { TransformControls } from "three/examples/jsm/controls/TransformControls"
import { GUI } from "lil-gui"
import { EffectComposer, EffectPass, RenderPass } from "postprocessing"
import { SSREffect } from "screen-space-reflections"
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader"
import {
ACESFilmicToneMapping,
BoxGeometry,
EquirectangularReflectionMapping,
MathUtils,
Mesh,
MeshStandardMaterial,
PerspectiveCamera,
PlaneBufferGeometry,
PointLight,
Scene,
SphereBufferGeometry,
WebGLRenderer,
} from "three"
const params = {
useComposer: false,
addTransformControls: false,
useSelection: false,
selectionFloor: false,
selectionObjects: false,
useHDRI: false,
}
const SSR_PARAMS = {
intensity: 1,
exponent: 1,
distance: 10,
fade: 0,
roughnessFade: 1,
thickness: 10,
ior: 1.45,
maxRoughness: 1,
maxDepthDifference: 10,
blend: 0.9,
correction: 1,
correctionRadius: 1,
blur: 0.5,
blurKernel: 1,
blurSharpness: 10,
jitter: 0,
jitterRoughness: 0,
steps: 20,
refineSteps: 5,
missedRays: true,
useNormalMap: true,
useRoughnessMap: true,
resolutionScale: 1,
velocityResolutionScale: 1,
}
const hdriLoader = new RGBELoader()
let floor, hdri
const objects = []
const renderer = new WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
renderer.toneMapping = ACESFilmicToneMapping
const scene = new Scene()
const camera = new PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
)
camera.position.set(0, 3, -8)
const controls = new OrbitControls(camera, renderer.domElement)
controls.update()
const point = new PointLight()
point.position.set(1, 2, 1)
scene.add(point)
// COMPOSER
const composer = new EffectComposer(renderer)
composer.addPass(new RenderPass(scene, camera))
// SSR
const ssrEffect = new SSREffect(scene, camera)
const ssrPass = new EffectPass(camera, ssrEffect)
composer.addPass(ssrPass)
// Transform con
const transformcontrols = new TransformControls(camera, renderer.domElement)
transformcontrols.enabled = false
transformcontrols.addEventListener("dragging-changed", (event) => {
controls.enabled = !event.value
})
function animate() {
requestAnimationFrame(animate)
// cube.rotation.x += 0.01;
// cube.rotation.y += 0.01;
if (params.useComposer) {
composer.render()
} else {
renderer.render(scene, camera)
}
}
function addCubes() {
for (let index = 0; index < 10; index++) {
const geometry =
Math.random() > 0.5
? new BoxGeometry(1, 1, 1)
: new SphereBufferGeometry(0.5)
const material = new MeshStandardMaterial({ color: 0xffffff, roughness: 0 })
material.color.setHSL(Math.random(), 0.6, 0.6)
const mesh = new Mesh(geometry, material)
mesh.position.x = MathUtils.randFloat(-5, 5)
mesh.position.z = MathUtils.randFloat(-5, 5)
mesh.position.y = 1
scene.add(mesh)
// sel.add(cube)
transformcontrols.attach(mesh)
objects.push(mesh)
}
}
function addFloor() {
const geometry = new PlaneBufferGeometry(10, 10)
geometry.rotateX(-Math.PI / 2)
const material = new MeshStandardMaterial({
color: 0x70cf89,
roughness: 0,
metalness: 0,
})
material.color.setHSL(Math.random(), 0.6, 0.6)
const plane = new Mesh(geometry, material)
scene.add(plane)
floor = plane
}
function addGui() {
const gui = new GUI({ title: "SSR 2.5.0" })
gui.add(params, "useComposer").name("Composer + SSR")
gui.add(params, "useHDRI").onChange((v) => {
useHDRI(v)
})
gui.add(params, "addTransformControls").onChange((v) => {
if (v) {
scene.add(transformcontrols)
transformcontrols.enabled = true
} else {
scene.remove(transformcontrols)
transformcontrols.enabled = false
}
})
gui.add(params, "selectionFloor").onChange((v) => {
if (v) {
ssrEffect.selection.add(floor)
} else {
ssrEffect.selection.delete(floor)
}
})
gui.add(params, "selectionObjects").onChange((v) => {
for (const obj of objects) {
if (v) {
ssrEffect.selection.add(obj)
} else {
ssrEffect.selection.delete(obj)
}
}
})
scene.environment = hdri
scene.background = hdri
addSSRGui(gui)
}
function addSSRGui(gui) {
const params = SSR_PARAMS
const f = gui.addFolder("SSR")
f.close()
const generalFolder = f.addFolder("General")
generalFolder.close()
generalFolder.add(params, "intensity", 0, 3, 0.01).onChange((v) => {
ssrEffect["intensity"] = v
})
generalFolder.add(params, "exponent", 0.125, 8, 0.125).onChange((v) => {
ssrEffect["exponent"] = v
})
generalFolder.add(params, "distance", 0.001, 10, 0.1).onChange((v) => {
ssrEffect["distance"] = v
})
generalFolder.add(params, "fade", 0, 20, 0.01).onChange((v) => {
ssrEffect["fade"] = v
})
generalFolder.add(params, "roughnessFade", 0, 1, 0.01).onChange((v) => {
ssrEffect["roughnessFade"] = v
})
generalFolder.add(params, "thickness", 0, 10).onChange((v) => {
ssrEffect["thickness"] = v
})
generalFolder.add(params, "ior", 1, 2.33333).onChange((v) => {
ssrEffect["ior"] = v
})
const maximumValuesFolder = f.addFolder("Maximum Values")
maximumValuesFolder.close()
maximumValuesFolder.add(params, "maxRoughness", 0, 1, 0.01).onChange((v) => {
ssrEffect["maxRoughness"] = v
})
maximumValuesFolder
.add(params, "maxDepthDifference", 0, 100, 0.1)
.onChange((v) => {
ssrEffect["maxDepthDifference"] = v
})
const temporalResolveFolder = f.addFolder("Temporal Resolve")
temporalResolveFolder.close()
temporalResolveFolder.add(params, "blend", 0, 1, 0.001).onChange((v) => {
ssrEffect["blend"] = v
})
temporalResolveFolder.add(params, "correction", 0, 1, 0.0001).onChange((v) => {
ssrEffect["correction"] = v
})
temporalResolveFolder.add(params, "correctionRadius", 1, 4, 1).onChange((v) => {
ssrEffect["correctionRadius"] = v
})
const blurFolder = f.addFolder("Blur")
blurFolder.close()
blurFolder.add(params, "blur", 0, 1, 0.01).onChange((v) => {
ssrEffect["blur"] = v
})
blurFolder.add(params, "blurKernel", 0, 5, 1).onChange((v) => {
ssrEffect["blurKernel"] = v
})
blurFolder.add(params, "blurSharpness", 0, 100, 1).onChange((v) => {
ssrEffect["blurSharpness"] = v
})
const jitterFolder = f.addFolder("Jitter")
jitterFolder.close()
jitterFolder.add(params, "jitter", 0, 4, 0.01).onChange((v) => {
ssrEffect["jitter"] = v
})
jitterFolder.add(params, "jitterRoughness", 0, 4, 0.01).onChange((v) => {
ssrEffect["jitterRoughness"] = v
})
const definesFolder = f.addFolder("Tracing")
definesFolder.close()
definesFolder.add(params, "steps", 1, 256, 1).onChange((v) => {
ssrEffect["steps"] = v
})
definesFolder.add(params, "refineSteps", 0, 16, 1).onChange((v) => {
ssrEffect["refineSteps"] = v
})
definesFolder.add(params, "missedRays").onChange((v) => {
ssrEffect["missedRays"] = v
})
const resolutionFolder = f.addFolder("Resolution")
resolutionFolder.close()
resolutionFolder.add(params, "resolutionScale", 0.125, 1, 0.125).onChange((v) => {
ssrEffect["resolutionScale"] = v
})
resolutionFolder.add(params, "velocityResolutionScale", 0.125, 1, 0.125).onChange((v) => {
ssrEffect["velocityResolutionScale"] = v
})
}
async function useHDRI(state) {
if (!hdri) {
hdri = await hdriLoader.loadAsync(
"https://threejs.org/examples/textures/equirectangular/blouberg_sunrise_2_1k.hdr"
)
hdri.mapping = EquirectangularReflectionMapping
}
scene.environment = state ? hdri : null
scene.background = state ? hdri : null
}
window.onresize = () => {
renderer.setSize(window.innerWidth, window.innerHeight)
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
}
addCubes()
addFloor()
addGui()
animate()