-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
372 lines (329 loc) · 12.3 KB
/
main.js
File metadata and controls
372 lines (329 loc) · 12.3 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import * as THREE from 'three'
import {OrbitControls} from 'three/addons/controls/OrbitControls.js'
import {GUI} from 'dat.gui'
import {Color, FramebufferTexture, RGBAFormat, Vector2, Vector3} from "three"
let scene;
let camera;
let renderer;
let orbit;
let gui;
let plane;
let lastFrameTexture;
let accumulatedFrameCount;
const MAX_SPHERES = 16;
const MAX_TRIANGLES = 90;
let shader;
let spheres = []
let triangles = []
function createPlane() {
const planeHeight = camera.position.z * Math.tan(camera.fov * 0.5 * Math.PI / 180) * 2;
const planeWidth = planeHeight * camera.aspect;
lastFrameTexture = new FramebufferTexture(window.innerWidth, window.innerHeight, RGBAFormat);
accumulatedFrameCount = 0;
let uniforms = {
"canvasSize": {value: {x: window.innerWidth, y: window.innerHeight}},
"ViewParams": {value: {x: planeWidth, y: planeHeight, z: -camera.position.z}},
"CamToWorldMatrix": {value: camera.matrixWorld},
"MaxBouceCount": {value: 4},
"LastFrame": {type: 't', value: lastFrameTexture},
"NumRenderedFrames": {value: accumulatedFrameCount},
"accumulateFrames": {value: false},
"sphereList": {value: []},
"sphereNumber": {value: spheres.length},
"triangleList": {value: []},
"triangleNumber": {value: triangles.length}
}
for (let i = 0; i < MAX_SPHERES; i++) {
uniforms.sphereList.value.push({
position: new Vector3(0, 0, 0),
radius: 0,
material: {
color: {x: 0, y: 0, z: 0, w: 1.0},
emissionColor: {x: 0, y: 0, z: 0, w: 1.0},
emissionStrength: 0,
smoothness: 0
}
})
}
for (let i = 0; i < MAX_TRIANGLES; i++) {
uniforms.triangleList.value.push({
posA: new Vector3(0, 0, 0),
posB: new Vector3(0, 0, 0),
posC: new Vector3(0, 0, 0),
normalA: new Vector3(0, 0, 0),
normalB: new Vector3(0, 0, 0),
normalC: new Vector3(0, 0, 0),
material: {
color: {x: 0, y: 0, z: 0, w: 1.0},
emissionColor: {x: 0, y: 0, z: 0, w: 1.0},
emissionStrength: 0,
smoothness: 0
}
})
}
shader = {
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent,
uniforms: uniforms,
glslVersion: THREE.GLSL3
}
const planeGeo = new THREE.BufferGeometry()
const vertices = new Float32Array([
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0
]);
planeGeo.setAttribute('position', new THREE.BufferAttribute(vertices, 3))
const planeMat = new THREE.ShaderMaterial(shader)
plane = new THREE.Mesh(planeGeo, planeMat)
plane.visible = false;
scene.add(plane)
}
function addGui() {
gui = new GUI();
gui.add(plane, 'visible', true).name("Raytracing");
gui.add(plane.material.uniforms.accumulateFrames, 'value').name("Accumulate").onChange(() => accumulatedFrameCount = 0);
gui.add(plane.material.uniforms.MaxBouceCount, 'value').name("Max Bouce");
function addGroup(s, i) {
let g = gui.addFolder("Sphere " + i)
let colors = {
"color": [
plane.material.uniforms.sphereList.value[i].material.color.x * 255,
plane.material.uniforms.sphereList.value[i].material.color.y * 255,
plane.material.uniforms.sphereList.value[i].material.color.z * 255
],
"emission": [
plane.material.uniforms.sphereList.value[i].material.emissionColor.x * 255,
plane.material.uniforms.sphereList.value[i].material.emissionColor.y * 255,
plane.material.uniforms.sphereList.value[i].material.emissionColor.z * 255
]
}
g.addColor(colors, 'color').name('Color').onChange((c) => {
plane.material.uniforms.sphereList.value[i].material.color = {
x: c[0] / 255,
y: c[1] / 255,
z: c[2] / 255,
w: 1.0
}
s.material.color = new Color(c[0] / 255, c[1] / 255, c[2] / 255);
})
g.addColor(colors, 'emission').name('EmssionColor').onChange((c) => {
plane.material.uniforms.sphereList.value[i].material.emissionColor = {
x: c[0] / 255,
y: c[1] / 255,
z: c[2] / 255,
w: 1.0
}
})
g.add(plane.material.uniforms.sphereList.value[i].material, 'emissionStrength', 0, 1, 0.05)
g.add(plane.material.uniforms.sphereList.value[i].material, 'smoothness', 0, 1, 0.05)
}
spheres.forEach(addGroup);
// triangles.forEach(addGroup)
}
function addSphere(radius, pos, diffuseColor, emissionColor, emissionStrength, smoothness) {
let geo = new THREE.SphereGeometry(radius)
let mat = new THREE.MeshBasicMaterial({color: new Color().setRGB(diffuseColor[0], diffuseColor[1], diffuseColor[2])})
let sphere = new THREE.Mesh(geo, mat)
sphere.position.set(pos[0], pos[1], pos[2])
scene.add(sphere);
shader.uniforms.sphereList.value[spheres.length] = {
position: new Vector3(pos[0], pos[1], pos[2]),
radius: radius,
material: {
color: {x: diffuseColor[0], y: diffuseColor[1], z: diffuseColor[2], w: 1.0},
emissionColor: {x: emissionColor[0], y: emissionColor[1], z: emissionColor[2], w: 1.0},
emissionStrength: emissionStrength,
smoothness: smoothness
}
}
spheres.push(sphere);
plane.material.uniforms.sphereNumber.value = spheres.length;
}
function addMesh(geo, pos, rot, diffuseColor, emissionColor, emissionStrength, smoothness) {
let mat = new THREE.MeshBasicMaterial({color: new Color().setRGB(diffuseColor[0], diffuseColor[1], diffuseColor[2])});
let m = new THREE.Mesh(geo, mat);
m.position.set(pos[0], pos[1], pos[2])
m.rotation.set(rot[0], rot[1], rot[2])
scene.add(m)
geo.computeVertexNormals();
let index = geo.getIndex();
let vertex = geo.getAttribute('position')
let normal = geo.getAttribute('normal')
for (let i = 0; i < index.array.length / 3; i++) {
let posA = m.localToWorld(new Vector3(
vertex.array[3 * index.array[3 * i]],
vertex.array[3 * index.array[3 * i] + 1],
vertex.array[3 * index.array[3 * i] + 2]
))
let posB = m.localToWorld(new Vector3(
vertex.array[3 * index.array[3 * i + 1]],
vertex.array[3 * index.array[3 * i + 1] + 1],
vertex.array[3 * index.array[3 * i + 1] + 2]
))
let posC = m.localToWorld(new Vector3(
vertex.array[3 * index.array[3 * i + 2]],
vertex.array[3 * index.array[3 * i + 2] + 1],
vertex.array[3 * index.array[3 * i + 2] + 2]
))
// let normalA = m.localToWorld(new Vector3(
// normal.array[3 * index.array[3 * i]],
// normal.array[3 * index.array[3 * i] + 1],
// normal.array[3 * index.array[3 * i] + 2]
// ))
// let normalB = m.localToWorld(new Vector3(
// normal.array[3 * index.array[3 * i + 1]],
// normal.array[3 * index.array[3 * i + 1] + 1],
// normal.array[3 * index.array[3 * i + 1] + 2]
// ))
// let normalC = m.localToWorld(new Vector3(
// normal.array[3 * index.array[3 * i + 2]],
// normal.array[3 * index.array[3 * i + 2] + 1],
// normal.array[3 * index.array[3 * i + 2] + 2]
// ))
let normalA = new Vector3(
normal.array[3 * index.array[3 * i]],
normal.array[3 * index.array[3 * i] + 1],
normal.array[3 * index.array[3 * i] + 2]
)
let normalB = new Vector3(
normal.array[3 * index.array[3 * i + 1]],
normal.array[3 * index.array[3 * i + 1] + 1],
normal.array[3 * index.array[3 * i + 1] + 2]
)
let normalC = new Vector3(
normal.array[3 * index.array[3 * i + 2]],
normal.array[3 * index.array[3 * i + 2] + 1],
normal.array[3 * index.array[3 * i + 2] + 2]
)
shader.uniforms.triangleList.value[triangles.length] = {
posA: posA,
posB: posB,
posC: posC,
normalA: normalA,
normalB: normalB,
normalC: normalC,
material: {
color: {x: diffuseColor[0], y: diffuseColor[1], z: diffuseColor[2], w: 1.0},
emissionColor: {x: emissionColor[0], y: emissionColor[1], z: emissionColor[2], w: 1.0},
emissionStrength: emissionStrength,
smoothness: smoothness
}
}
triangles.push(m);
plane.material.uniforms.triangleNumber.value = triangles.length;
}
return m;
}
function createHeartGeo() {
const vertices = new Float32Array([
0, 0, 0, // point C
0, 5, -1.5,
5, 5, 0, // point A
9, 9, 0,
5, 9, 2,
7, 13, 0,
3, 13, 0,
0, 11, 0,
5, 9, -2,
0, 8, -3,
0, 8, 3,
0, 5, 1.5, // point B
-9, 9, 0,
-5, 5, 0,
-5, 9, -2,
-5, 9, 2,
-7, 13, 0,
-3, 13, 0,
]);
const trianglesIndexes = [
// face 1
2, 11, 0, // This represents the 3 points A,B,C which compose the first triangle
2, 3, 4,
5, 4, 3,
4, 5, 6,
4, 6, 7,
4, 7, 10,
4, 10, 11,
4, 11, 2,
0, 11, 13,
12, 13, 15,
12, 15, 16,
16, 15, 17,
17, 15, 7,
7, 15, 10,
11, 10, 15,
13, 11, 15,
// face 2
0, 1, 2,
1, 9, 2,
9, 8, 2,
5, 3, 8,
8, 3, 2,
6, 5, 8,
7, 6, 8,
9, 7, 8,
14, 17, 7,
14, 7, 9,
14, 9, 1,
9, 1, 13,
1, 0, 13,
14, 1, 13,
16, 14, 12,
16, 17, 14,
12, 14, 13
]
const geo = new THREE.BufferGeometry()
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3))
geo.setIndex(trianglesIndexes);
geo.computeVertexNormals();
geo.scale(0.15, 0.15, 0.15)
return geo;
}
function checkCurrentMode() {
if (!plane.visible) {
spheres.forEach((val) => val.visible = true);
triangles.forEach((val) => val.visible = true);
accumulatedFrameCount = 0;
} else {
if (accumulatedFrameCount === 0) lastFrameTexture.dispose()
spheres.forEach((val) => val.visible = false);
triangles.forEach((val) => val.visible = false);
accumulatedFrameCount++;
plane.material.uniforms.NumRenderedFrames.value = accumulatedFrameCount;
}
}
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
renderer = new THREE.WebGLRenderer();
orbit = new OrbitControls(camera, renderer.domElement);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// const axesHelper = new THREE.AxesHelper(5);
// scene.add(axesHelper);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 10;
orbit.update();
createPlane();
addMesh(createHeartGeo(), [0, -1.5, 0], [0, 0, 0], [1, 1, 1], [0, 0, 0], 0, 0);
addMesh(new THREE.BoxGeometry(3.8, 0.2, 4), [0, 2, 0], [0, 0, 0], [0.95, 1, 1], [0, 0, 0], 0, 0);
addMesh(new THREE.PlaneGeometry(2, 2), [0, 1.7, 0], [-Math.PI / 2, 0, 0], [1, 1, 1], [1, 1, 1], 10, 0);
addMesh(new THREE.BoxGeometry(3.8, 0.2, 4), [0, -2, 0], [0, 0, 0], [0, 1, 0], [0, 0, 0], 0, 0);
addMesh(new THREE.BoxGeometry(0.2, 4.2, 4), [-2, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0], 0, 0);
addMesh(new THREE.BoxGeometry(4.2, 4.2, 0.2), [0, 0, -2.1], [0, 0, 0], [0.1, 0.1, 0.1], [0, 0, 0], 0, 0);
addMesh(new THREE.BoxGeometry(0.2, 4.2, 4), [2, 0, 0], [0, 0, 0], [0, 0, 1], [0, 0, 0], 0, 0);
addGui();
animate()
}
function animate() {
checkCurrentMode();
renderer.render(scene, camera)
renderer.copyFramebufferToTexture(new Vector2(0, 0), lastFrameTexture)
requestAnimationFrame(animate)
}
init()