-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreejsWorker.js
More file actions
385 lines (352 loc) · 15.7 KB
/
ThreejsWorker.js
File metadata and controls
385 lines (352 loc) · 15.7 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
373
374
375
376
377
378
379
380
381
382
383
384
385
import {GLCommandBufferContext, CommandBufferFactory, CommandBuffer, WebGLSyncStrategy, WebGLStrategy} from "./glCommandBuffer.js";
import {MessageInterface} from './WorkerFactory.js';
import * as THREE from './three/three.module.js';
/**
* interfaces with three.js and intializes the engine for the rest of the tool to use for rendering
*/
class ThreejsWorker {
static STATE_CONSTRUCTED = 0;
static STATE_BOOTSTRAP = 1;
static STATE_BOOTSTRAP_DONE = 2;
static STATE_FRAME = 3;
static STATE_FRAME_DONE = 4;
static STATE_CONTEXT_LOST = 5;
static STATE_CONTEXT_RESTORED = 6;
static STATE_CONTEXT_RESTORED_DONE = 7;
/**
*
* @param {MessageInterface} messageInterface
*/
constructor(messageInterface) {
this.clientState = ThreejsWorker.STATE_CONSTRUCTED;
// some values will be set after the bootstrap message has been received
this.lastProjectionMatrix = null;
this.isProjectionMatrixSet = false;
this.workerId = -1;
/**
* @type {Int32Array|null}
*/
this.synclock = null;
this.glCommandBufferContext = null;
this.commandBufferFactory = null;
this.frameCommandBuffer = null;
this.bootstrapProcessed = false;
/**
* @type {Array<function(THREE.Scene):void>}
*/
this.onSceneCreatedCallbacks = [];
/**
* @type {Array<function(number):void>}
*/
this.onRenderCallbacks = [];
this.raycaster = new THREE.Raycaster();
/**
* @type {MessageInterface}
*/
this.messageInterface = messageInterface;
}
/**
* receives messages from the ThreeInteface client class
* @param {MessageEvent<any>} event
*/
onMessageFromInterface(event) {
const message = event.data;
if (!message) {
return;
}
if (typeof message !== 'object') {
return;
}
if (message.hasOwnProperty("name")) {
if (message.name === "touchDecider") {
let result = false;
if (this.clientState !== ThreejsWorker.STATE_CONTEXT_LOST) {
//2. set the picking ray from the camera position and mouse coordinates
this.raycaster.setFromCamera(new THREE.Vector2(message.mouse.x, message.mouse.y), this.camera);
//3. compute intersections
const intersects = this.raycaster.intersectObjects(this.scene.children, true);
result = intersects.length > 0;
}
this.messageInterface.postMessage({workerId: this.workerId, name: "touchDeciderAnswer", result: result});
} else if (message.name === "onWindowResized") {
if (this.fakeCanvas) {
this.fakeCanvas.width = message.width;
this.fakeCanvas.height = message.height;
}
if (this.renderer) {
this.renderer.setSize(message.width, message.height);
}
this.isProjectionMatrixSet = false;
} else if (message.name === "anchoredModelViewCallback") {
switch (this.clientState) {
case ThreejsWorker.STATE_CONSTRUCTED:
case ThreejsWorker.STATE_BOOTSTRAP_DONE:
case ThreejsWorker.STATE_FRAME_DONE:
case ThreejsWorker.STATE_CONTEXT_RESTORED_DONE:
// setup the projection matrix
this.lastProjectionMatrix = message.projectionMatrix;
break;
default:
console.error("wrong state to set projectionMatrix clientState: " + this.clientState);
break;
}
} else if (message.name === "bootstrap") {
if (this.clientState == ThreejsWorker.STATE_CONSTRUCTED) {
this.clientState = ThreejsWorker.STATE_BOOTSTRAP;
// finish initialisation
const {workerId, width, height, synclock} = message;
this.workerId = workerId;
this.synclock = synclock;
// create commandbuffer factory in order to create resource commandbuffers
this.glCommandBufferContext = new GLCommandBufferContext(message, WebGLStrategy.getInstance().syncStrategy);
this.commandBufferFactory = new CommandBufferFactory(this.workerId, this.glCommandBufferContext, this.synclock, this.messageInterface);
// execute three.js startup
const bootstrapCommandBuffers = this.main(width, height, this.commandBufferFactory);
// send all created commandbuffers
for (const bootstrapCommandBuffer of bootstrapCommandBuffers) {
bootstrapCommandBuffer.execute();
}
// create standard rendering command buffer for reuse every frame
this.frameCommandBuffer = this.commandBufferFactory.createAndActivate(true);
this.bootstrapProcessed = true;
// singnal end of frame
this.messageInterface.postMessage({
workerId: this.workerId,
isFrameEnd: true,
});
this.clientState = ThreejsWorker.STATE_BOOTSTRAP_DONE;
} else {
console.error("wrong state for bootstrap clientsState: " + this.clientState);
}
} else if (message.name === "frame") {
switch (this.clientState) {
case ThreejsWorker.STATE_BOOTSTRAP_DONE:
case ThreejsWorker.STATE_FRAME_DONE:
case ThreejsWorker.STATE_CONTEXT_RESTORED_DONE:
this.clientState = ThreejsWorker.STATE_FRAME;
// safety checks
this.workerId = message.workerId;
if (!this.bootstrapProcessed) {
console.log(`Can't render worker with id: ${this.workerId}, it has not yet finished initializing`);
this.messageInterface.postMessage({
workerId: this.workerId,
isFrameEnd: true,
});
this.clientState = ThreejsWorker.STATE_FRAME_DONE;
return;
}
if (Date.now() - message.time > 300) {
console.log('time drift detected');
this.messageInterface.postMessage({
workerId: this.workerId,
isFrameEnd: true,
});
this.clientState = ThreejsWorker.STATE_FRAME_DONE;
return;
}
// erase the previous render command buffer
this.frameCommandBuffer.clear();
// try rendering with three.js
try {
this.frameCommandBuffer = this.render(message.time, this.frameCommandBuffer);
} catch (err) {
console.error('Error in gl-worker render fn', err);
}
// send the commandbuffer (rendering or resource loading)
this.frameCommandBuffer.execute();
// signal end of frame
this.messageInterface.postMessage({
workerId: this.workerId,
isFrameEnd: true,
});
this.clientState = ThreejsWorker.STATE_FRAME_DONE;
break;
default:
console.error("wrong state for generating frames clientState: " + this.clientState);
}
} else if (message.name === "context_lost") {
switch (this.clientState) {
case ThreejsWorker.CONSTRUCTED:
case ThreejsWorker.STATE_BOOTSTRAP_DONE:
case ThreejsWorker.STATE_FRAME_DONE:
case ThreejsWorker.STATE_CONTEXT_RESTORED_DONE:
this.clientState = ThreejsWorker.STATE_CONTEXT_LOST;
this.onContextLost();
break;
default:
console.error("wrong state for lost context clientState: " + this.clientState);
break;
}
} else if (message.name === "context_restored") {
if (this.clientState === ThreejsWorker.STATE_CONTEXT_LOST) {
this.clientState = ThreejsWorker.STATE_CONTEXT_RESTORED;
this.onContextRestored();
this.clientState = ThreejsWorker.STATE_CONTEXT_RESTORED_DONE;
} else {
console.error("wrong state to restore state clientState: " + this.clientState);
}
}
}
}
onContextLost() {
this.glCommandBufferContext.onContextLost();
this.fakeCanvas.webglcontextlost({preventDefault: () => {}});
}
onContextRestored() {
this.glCommandBufferContext.onContextRestored();
let restoreBuffer = this.commandBufferFactory.createAndActivate(true);
this.fakeCanvas.webglcontextrestored();
restoreBuffer.execute();
// signal end of frame
this.messageInterface.postMessage({
workerId: this.workerId,
isFrameEnd: true,
});
}
/**
* registers callbacks for the tool to use during initalisation
* @param {function(THREE.Scene):void} callback
* @returns {ThreejsWorker}
*/
onSceneCreated(callback) {
this.onSceneCreatedCallbacks.push(callback);
return this;
}
/**
* registers a callback for the tool to use during rendering
* @param {function(number):void} callback
* @returns {ThreejsWorker}
*/
onRender(callback) {
this.onRenderCallbacks.push(callback);
return this;
}
/**
* starts three.js engine with the fake webgl context
* @param {number} width
* @param {number} height
* @param {CommandBufferFactory} cmdBufferFactory
* @returns {CommandBuffer[]}
*/
main(width, height, cmdBufferFactory) {
this.commandBufferFactory = cmdBufferFactory;
let cmdBuffer = cmdBufferFactory.createAndActivate(false);
let gl = cmdBufferFactory.getGL();
// this is needed to trick three.js into completing it's initialisation
this.fakeCanvas = new FakeCanvas(width, height);
this.renderer = new THREE.WebGLRenderer({context: gl, alpha: true, canvas: this.fakeCanvas});
this.renderer.debug.checkShaderErrors = false;
this.renderer.setSize(width, height);
// setup camera and scene for the tool
this.camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000);
this.scene = new THREE.Scene();
// call tool specific code to finish initialisation
for (let callback of this.onSceneCreatedCallbacks) {
callback(this.scene);
}
return [cmdBuffer];
}
/**
* renders the scene and returns the command buffer
* @param {number} now timestamp
* @param {CommandBuffer} commandBuffer the commandbuffer to use for rendering
* @returns CommandBuffer to send to the server
*/
innerRender(now, commandBuffer) {
// safety checks
if (!this.camera) {
console.warn('rendering too early');
return commandBuffer;
}
if (!this.isProjectionMatrixSet && this.lastProjectionMatrix && this.lastProjectionMatrix.length === 16) {
// replace the projection matrix
setMatrixFromArray(this.camera.projectionMatrix, this.lastProjectionMatrix);
if (this.camera.projectionMatrixInverse.getInverse) {
this.camera.projectionMatrixInverse.getInverse(this.camera.projectionMatrix);
} else {
this.camera.projectionMatrixInverse.copy(this.camera.projectionMatrix).invert();
}
this.isProjectionMatrixSet = true;
}
// call tool specific code to finish the scene update cycle
for (let callback of this.onRenderCallbacks) {
callback(now);
}
// render the scene using three.js if posible
if (this.isProjectionMatrixSet) {
if (this.renderer && this.scene && this.camera) {
this.renderer.render(this.scene, this.camera);
}
}
}
/**
* tries to create a render command buffer.
* during the first frame after adding assets the three.js renderer adds sync operations to initialize those new resources
* in that case the resulting bufer is not a render command buffer but a resource commandbuffer
* this code will detect that and send the resource command buffer to the server and rerender the whole scen to get a valid render commandbuffer to send to the server
* @param {number} now timestamp
* @param {CommandBuffer} commandBuffer the render commandbuffer to be used for rendering
* @returns {CommandBuffer} a render commandbuffer
*/
render(now, commandBuffer) {
let threeCommandBuffer = this.commandBufferFactory.createAndActivate(false);
this.innerRender(now, threeCommandBuffer);
// check if three.js initialized resources during the renderloop
if (threeCommandBuffer.isCleared) {
// send remaining commands in the resource commandbuffer to the server
threeCommandBuffer.execute();
// rerender scene to get a renderable commandbuffer
this.glCommandBufferContext.setActiveCommandBuffer(commandBuffer);
this.innerRender(now, commandBuffer);
} else {
// three js created a valid render command buffer, replace the current buffer and enable rendering
commandBuffer = threeCommandBuffer;
commandBuffer.isRendering = true;
}
return commandBuffer;
}
}
/**
* Fake canvas element to make sure threejs initializes correctly
*/
class FakeCanvas {
/**
*
* @param {number} width
* @param {number} height
*/
constructor(width, height) {
this.width = width;
this.height = height;
this.style = {width: width.toString() + "px", height: height.toString + "px"};
this.webglcontextlost = () => {};
this.webglcontextrestored = () => {};
}
/**
*
* @param {string} type
* @param {function(Event):void} listener
* @param {boolean} useCapture
*/
addEventListener(type, listener, useCapture) {
// we don't implement context creation error
if (type === "webglcontextlost") {
this.webglcontextlost = listener;
} else if (type === "webglcontextrestored") {
this.webglcontextrestored = listener;
}
}
}
/**
* converts an Float32Array to a THREE.Matrix4
* @param {THREE.Matrix4} matrix
* @param {Float32Array} array
*/
function setMatrixFromArray(matrix, array) {
matrix.set(array[0], array[4], array[8], array[12],
array[1], array[5], array[9], array[13],
array[2], array[6], array[10], array[14],
array[3], array[7], array[11], array[15]
);
}
export {ThreejsWorker, setMatrixFromArray};