-
Notifications
You must be signed in to change notification settings - Fork 15
/
bvh.html
339 lines (279 loc) · 10.4 KB
/
bvh.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="res/css/main.css" />
</head>
<body>
<canvas id="screen"></canvas>
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/ashes.main.js"></script>
<!-- <script src="build/ashes.main.js"></script> -->
<script>
let { Asset, EntityMgr, Camera, vec2, vec3, mat3, mat4, quat, Screen, OrbitControl, Filter, Shader, Material, QuadMesh, MeshRenderer, Texture, glsl } = Ashes;
let {Vignetting} = Ashes;
let {Bloom} = Ashes;
let {BVHManager, AABB} = Ashes;
// URLs:
let assetRoot = 'https://but0n.github.io/Ashes/'
// let assetRoot = ''
Material.SHADER_PATH = assetRoot + Material.SHADER_PATH;
DamagedHelmet = assetRoot + 'gltfsamples/DamagedHelmet.glb';
ashesLogo = assetRoot + 'gltfsamples/ashes3.glb';
(async () => {
let screen = new Screen('#screen');
screen.bgColor = [0.23,0.23,0.23, 1];
// Prepare textures:
// 1.Download HDR environment texture
let hdrEnv = await Asset.loadTexture(assetRoot + 'res/envmap/HDR_textures/lebombo_2k.hdr');
hdrEnv.flipY = true;
// 2.Download grid texture
let grid = await Asset.loadTexture('res/textures/grid.jpg');
// 3.Download UV checher texture
// let uvck = await Asset.loadTexture(assetRoot + 'res/textures/checker.png');
// 4.Download hdr cube texture which is required in PBR forward renderer
// (Unnecessary!)
let diffenv = await Asset.loadCubemap(assetRoot + 'res/envmap/helipad_diff/', 'hdr');
let skybox = await Asset.loadCubemap(assetRoot + 'res/envmap/helipad/', 'hdr');
// Creat a scene
let scene = EntityMgr.create('root');
// Initialize Camera and controls
let mainCamera = scene.appendChild(EntityMgr.create('camera'));
let cameraTrans = mainCamera.components.Transform;
let cam = mainCamera.addComponent(new Camera(screen.width / screen.height));
vec3.set(cameraTrans.translate, 0, 0, 15);
// Orbit control
let orbitCtl = mainCamera.addComponent(new OrbitControl(screen, mainCamera));
orbitCtl.damping = 0;
orbitCtl.speed = 1;
screen.mainCamera = cam;
// Apply current scene
document.querySelector('body').appendChild(scene);
// NOTE: Downlaod glTF models
// Apped them to current scene
let disposer = [
{
url: ashesLogo,
t: [0, -2, -4],
s: 1.,
},
{
url: DamagedHelmet,
// t: [3., -0.1, 0],
t: [0, -0.1, 0],
// r: [0, -30, 0],
s: 2,
},
].map(async ({url, r, t, s}) => {
if(!url) return () => {};
const m = await Asset.loadGLTF(url, screen, skybox, diffenv);
scene.appendChild(m);
if(t != undefined) {
for(let i = 0; i < 3; i++)
m.components.Transform.translate[i] = t[i];
}
if(s != undefined)
vec3.scale(m.components.Transform.scale, m.components.Transform.scale, s);
if(r && r.length == 3)
quat.fromEuler(m.components.Transform.quaternion, r[0], r[1], r[2]);
return () => scene.removeChild(m);
});
// return;
disposer = await Promise.all(disposer);
// Collect all meshes
meshes = EntityMgr.getComponents('Mesh', scene);
// waiting until world matrices updated
await new Promise((res, rej) => {
setTimeout(() => res(), 200);
})
// NOTE: remove gltf model
disposer.forEach(e => e());
EntityMgr.hasNewMember = true;
// NOTE: Build BVH
let bvhMgr = new BVHManager();
console.log(bvhMgr);
// const {LBVH, triangleTexture, primitives} = bvhMgr.buildBVH([ meshes[0], meshes[1], meshes[14+2], meshes[38+2], meshes[37+2], meshes[41+2], meshes[81+2]]);
const {LBVH, triangleTexture, primitives, matHandler} = bvhMgr.buildBVH(meshes);
console.log(LBVH);
// Detect platform
let commonShader = Screen.platform == 'iOS'
? new Shader(glsl.stylize.vs, glsl.stylize.fs)
: new Shader(glsl.stylize2.vs, glsl.stylize2.fs);
// Initial ray-tracer
const rtShader = new Shader(glsl.ray.vs, glsl.ray.fs, {
iResolution: `vec2(${1/screen.width},${1/screen.height})`,
Resolution: `vec2(${screen.width},${screen.height})`,
iTexel: `vec2(${1/2048})`,
});
rtShader.fragmentSource = rtShader.fragmentSource
.replace('#include <mat_params>', matHandler.params)
.replace('#include <mat_route>', matHandler.route);
let rt = new rayTracing(screen, cam, rtShader);
Material.setTexture(rt.material, 'triangleTex', triangleTexture.tex);
Material.setTexture(rt.material, 'LBVHTex', LBVH.tex);
// HDR environment texture
Material.setTexture(rt.material, 'hdrSky', hdrEnv);
// Grid texture
Material.setTexture(rt.material, 'ground', grid);
// UV checker
// Material.setTexture(rt.material, 'uvck', uvck);
matHandler.init(rt.material);
// screen.attachFilter(rt);
let cache = new Filter(screen, new Shader(
`#version 300 es
#include <macros>
in vec3 POSITION;
void main() {
gl_Position = vec4(POSITION, 1);
}
`,
`#version 300 es
#extension GL_OES_texture_float : enable
#extension GL_OES_texture_float_linear : enable
precision highp float;
#include <macros>
uniform sampler2D base;
uniform sampler2D test;
out vec4 outColor;
void main(){
outColor = texelFetch(test, ivec2(gl_FragCoord), 0);
}
`
));
screen.attachFilter(cache);
screen.attachFilter(rt);
cache.setInput(rt.output, 'test');
rt.setInput(cache.output, 'test');
let rt_final = new Filter(screen, new Shader(glsl.ray_comb.vs, glsl.ray_comb.fs, {
iResolution: `vec2(${1/screen.width},${1/screen.height})`,
Resolution: `vec2(${screen.width},${screen.height})`,
iTexel: `vec2(${1/4096})`,
}));
exp = 1;
rt_final.setInput(rt.output, 'test');
Material.setUniform(rt_final.material, 'EXPOSURE', exp);
screen.attachFilter(rt_final);
// Control
document.addEventListener('keypress', (e) => {
switch(e.key) {
case 'k':
rt.toggleMode();
break;
case ']':
exp += 0.05;
Material.setUniform(rt_final.material, 'EXPOSURE', exp*exp);
break;
case '[':
exp -= 0.05;
Material.setUniform(rt_final.material, 'EXPOSURE', exp*exp);
break;
case 'q':
--rt.aperture;
rt.updateAperture();
break;
case 'e':
++rt.aperture;
rt.updateAperture();
break;
case 'a':
rt.focalLength -= 5;
rt.updateFocalLength();
break;
case 'd':
rt.focalLength += 5;
rt.updateFocalLength();
break;
}
})
})()
class rayTracing extends Filter {
frame = 0;
timer = 0;
tar = vec3.fromValues(0, 1.2, 0);
// tar = vec3.fromValues(0, 0, 0);
cam;
fastMode = true;
mousePos = vec2.create();
aperture = 4;
focalLength = 50;
constructor(screen, cam, shader) {
super(screen, shader);
this.cameraTrans = cam.entity.components.Transform;
this.cam = cam;
this.tar = cam.lookAt;
let update = (e, focus = false) => {
if(e && focus) {
const {pageX, pageY} = e;
this.mousePos[0] = (pageX * 2 - window.innerWidth) / window.innerHeight;
this.mousePos[1] = ((window.innerHeight - pageY) * 2 - window.innerHeight) / window.innerHeight;
Material.setUniform(this.material, 'mousePos', this.mousePos);
}
this.reset();
}
screen.canvas.addEventListener('mousedown', e => {
update(e, true);
screen.canvas.addEventListener('mousemove', update)
})
screen.canvas.addEventListener('mouseup', () => {
screen.canvas.removeEventListener('mousemove', update);
})
screen.canvas.addEventListener('wheel', update);
this.createTBN();
this.reset();
this.toggleMode();
this.onRender = (dt) => {
// this.updateTBN();
document.title = `[F ${this.frame}] - Ashes3d`;
Material.setUniform(this.material, 'iFrame', this.frame);
Material.setUniform(this.material, 'iTime', this.timer);
// Material.setUniform(this.material, 'vp', this.cameraTrans.translate);
// Material.setUniform(this.material, 'TBN', this.tbn);
this.timer += dt;
this.frame += 1;
}
this.updateAperture();
this.updateFocalLength();
}
updateAperture() {
Material.setUniform(this.material, 'aperture', Math.pow(1 / Math.max(0, this.aperture) * 1.414, 2));
this.reset();
}
updateFocalLength() {
Material.setUniform(this.material, 'focalLength', this.focalLength/23);
this.reset();
}
toggleMode() {
this.fastMode = !this.fastMode;
// Material.setUniform(this.material, 'PATH_LENGTH', 12);
Material.setUniform(this.material, 'DEBUG_NORMAL', this.fastMode ? -1 : 1);
this.reset();
}
tbn;
t; // tangent
b; // bitangent
n; // normal
createTBN() {
this.tbn = mat3.create();
this.t = this.tbn.subarray(0, 3);
this.b = this.tbn.subarray(3, 6);
this.n = this.tbn.subarray(6, 9);
}
updateTBN() {
const vp = this.cameraTrans.translate;
const tar = this.tar;
vec3.sub(this.n, tar, vp);
vec3.normalize(this.n, this.n);
vec3.cross(this.t, this.n, this.cam.up);
vec3.normalize(this.t, this.t);
vec3.cross(this.b, this.t, this.n);
Material.setUniform(this.material, 'vp', this.cameraTrans.translate);
Material.setUniform(this.material, 'TBN', this.tbn);
// Material.setUniform(this.materials, 'mousePos', this.mousePos);
}
reset() {
this.frame = 0;
// this.timer = 0;
this.updateTBN();
}
}
</script>
</html>