-
Notifications
You must be signed in to change notification settings - Fork 203
/
app.js
291 lines (236 loc) · 8.27 KB
/
app.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
import {GLTFParser} from '@loaders.gl/gltf';
import {DracoDecoder} from '@loaders.gl/draco';
import {AnimationLoop, setParameters, clear, createGLTFObjects, withParameters, log} from 'luma.gl';
import {Matrix4, radians} from 'math.gl';
import document from 'global/document';
export const GLTF_BASE_URL = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/";
const GLTF_MODEL_INDEX = `${GLTF_BASE_URL}model-index.json`;
const INFO_HTML = `
<p><b>glTF</b> rendering.</p>
<p>A luma.gl <code>glTF</code> renderer.</p>
<p><img src="https://img.shields.io/badge/WebVR-Supported-orange.svg" /></p>
<div>
Model
<select id="modelSelector">
<option value="DamagedHelmet/glTF-Binary/DamagedHelmet.glb">Default</option>
</select>
<br>
</div>
<div>
Show
<select id="showSelector">
<option value="0 0 0 0 0 0 0 0">Final Result</option>
<option value="0 1 0 0 0 0 0 0">Base Color</option>
<option value="0 0 1 0 0 0 0 0">Metallic</option>
<option value="0 0 0 1 0 0 0 0">Roughness</option>
<option value="1 0 0 0 0 0 0 0">Diffuse</option>
<option value="0 0 0 0 1 0 0 0">Specular Reflection</option>
<option value="0 0 0 0 0 1 0 0">Geometric Occlusion</option>
<option value="0 0 0 0 0 0 1 0">Microfacet Distribution</option>
<option value="0 0 0 0 0 0 0 1">Specular</option>
</select>
<br>
</div>
`;
const DEFAULT_OPTIONS = {
pbrDebug: true,
pbrIbl: true
};
async function loadGLTF(urlOrPromise, gl, options = DEFAULT_OPTIONS) {
let promise = urlOrPromise;
if (typeof urlOrPromise === 'string') {
const url = urlOrPromise;
/* global fetch */
const response = await fetch(url);
promise = url.endsWith('.gltf') ? response.json() : response.arrayBuffer();
}
const data = await promise;
const gltfParser = new GLTFParser();
const gltf = await gltfParser.parse(data, {
uri: urlOrPromise,
decompress: true,
DracoDecoder
});
const {scenes, animator} = createGLTFObjects(gl, gltf, options);
log.info(4, "gltfParser: ", gltfParser)();
log.info(4, "scenes: ", scenes)();
scenes[0].traverse((node, {worldMatrix}) => {
log.info(4, "Using model: ", node)();
});
return {scenes, animator};
}
function loadModelList() {
return window.fetch(GLTF_MODEL_INDEX).then(res => res.json());
}
function addModelsToDropdown(models, modelDropdown) {
const VARIANTS = ['glTF-Draco', 'glTF-Binary', 'glTF-Embedded', 'glTF'];
models.forEach(({name, variants}) => {
const variant = VARIANTS.find(v => variants[v]);
const option = document.createElement('option');
option.text = `${name} (${variant})`;
option.value = `${name}/${variant}/${variants[variant]}`;
modelDropdown.appendChild(option);
});
}
export class DemoApp {
constructor({modelFile = null, initialZoom = 2} = {}) {
this.scenes = [];
this.animator = null;
this.gl = null;
this.modelFile = modelFile;
this.glOptions = {
// Use to test gltf with webgl 1.0 and 2.0
webgl2: true,
// alpha causes issues with some glTF demos
alpha: false
};
this.mouse = {
lastX: 0,
lastY: 0
};
this.translate = initialZoom;
this.rotation = [0, 0];
this.rotationStart = [0, 0];
this.u_ScaleDiffBaseMR = [0, 0, 0, 0];
this.u_ScaleFGDSpec = [0, 0, 0, 0];
this.onInitialize = this.onInitialize.bind(this);
this.onRender = this.onRender.bind(this);
}
initalizeEventHandling(canvas) {
canvas.onwheel = e => {
this.translate += e.deltaY / 10;
if (this.translate < 0.5) {
this.translate = 0.5;
}
e.preventDefault();
};
canvas.onpointerdown = e => {
this.mouse.lastX = e.clientX;
this.mouse.lastY = e.clientY;
this.rotationStart[0] = this.rotation[0];
this.rotationStart[1] = this.rotation[1];
canvas.setPointerCapture(e.pointerId);
e.preventDefault();
};
canvas.onpointermove = e => {
if (e.buttons) {
const dX = e.clientX - this.mouse.lastX;
const dY = e.clientY - this.mouse.lastY;
this.rotation[0] = this.rotationStart[0] + dY / 100;
this.rotation[1] = this.rotationStart[1] + dX / 100;
}
};
canvas.ondragover = e => {
e.dataTransfer.dropEffect = 'link';
e.preventDefault();
};
canvas.ondrop = e => {
e.preventDefault();
if (e.dataTransfer.files && e.dataTransfer.files.length === 1) {
loadGLTF(new Promise(resolve => {
const reader = new window.FileReader();
reader.onload = ev => resolve(ev.target.result);
reader.readAsArrayBuffer(e.dataTransfer.files[0]);
}), this.gl).then(result => Object.assign(this, result));
}
};
}
onInitialize({gl, canvas, _loop: animationLoop}) {
animationLoop.enableWebVR();
setParameters(gl, {
depthTest: true,
blend: false,
});
this.gl = gl;
if (this.modelFile) {
// options for unit testing
const options = {
pbrDebug: false,
pbrIbl: false
};
loadGLTF(this.modelFile, this.gl, options).then(result => Object.assign(this, result));
} else {
const modelSelector = document.getElementById("modelSelector");
loadGLTF(GLTF_BASE_URL + modelSelector.value, this.gl).then(result => Object.assign(this, result));
modelSelector.onchange = event => {
loadGLTF(GLTF_BASE_URL + modelSelector.value, this.gl).then(result => Object.assign(this, result));
};
loadModelList().then(models => addModelsToDropdown(models, modelSelector));
}
const showSelector = document.getElementById("showSelector");
if (showSelector) {
showSelector.onchange = event => {
const value = showSelector.value.split(" ").map(x => parseFloat(x));
this.u_ScaleDiffBaseMR = value.slice(0, 4);
this.u_ScaleFGDSpec = value.slice(4);
};
}
this.initalizeEventHandling(canvas);
}
onRenderFrame(opts) {
const {gl, width, height, _vr: vr} = opts;
if (vr) {
vr.display.getFrameData(vr.frameData);
withParameters(gl, {
viewport: [0, 0, width * 0.5, height],
scissor: [0, 0, width * 0.5, height],
scissorTest: true
}, () => this.onRender(opts));
withParameters(gl, {
viewport: [width * 0.5, 0, width * 0.5, height],
scissor: [width * 0.5, 0, width * 0.5, height],
scissorTest: true
}, () => this.onRender(opts));
vr.display.submitFrame();
} else {
gl.viewport(0, 0, width, height);
this.onRender(opts);
}
}
onRender({gl, time, aspect}) {
clear(gl, {color: [0.2, 0.2, 0.2, 1.0], depth: true});
const [pitch, roll] = this.rotation;
const cameraPos = [
-this.translate * Math.sin(roll) * Math.cos(-pitch),
-this.translate * Math.sin(-pitch),
this.translate * Math.cos(roll) * Math.cos(-pitch)
];
const uView = new Matrix4() //(Array.from(this.frameData[`${eye}ViewMatrix`]))
.translate([0, 0, -this.translate])
.rotateX(pitch)
.rotateY(roll);
const uProjection = new Matrix4().perspective({fov: radians(40), aspect, near: 0.1, far: 9000});
// const uProjection = new Matrix4(Array.from(this.frameData[`${eye}ProjectionMatrix`]));
if (!this.scenes.length) return false;
if (this.animator) {
this.animator.animate(time);
}
let success = true;
this.scenes[0].traverse((model, {worldMatrix}) => {
// In glTF, meshes and primitives do no have their own matrix.
const u_MVPMatrix = new Matrix4(uProjection).multiplyRight(uView).multiplyRight(worldMatrix);
success = success && model.draw({
uniforms: {
u_Camera: cameraPos,
u_MVPMatrix,
u_ModelMatrix: worldMatrix,
u_NormalMatrix: new Matrix4(worldMatrix).invert().transpose(),
u_ScaleDiffBaseMR: this.u_ScaleDiffBaseMR,
u_ScaleFGDSpec: this.u_ScaleFGDSpec,
},
parameters: model.props.parameters
});
});
return success;
}
}
const animationLoop = new AnimationLoop(new DemoApp());
animationLoop.getInfo = () => INFO_HTML;
export default animationLoop;
/* global window */
if (typeof window !== 'undefined' && !window.website) {
animationLoop.start();
const infoDiv = document.createElement('div');
infoDiv.innerHTML = animationLoop.getInfo();
document.body.appendChild(infoDiv);
}