-
Notifications
You must be signed in to change notification settings - Fork 1
/
InfiniteWorkerExternalGeometry.ts
69 lines (57 loc) · 2.24 KB
/
InfiniteWorkerExternalGeometry.ts
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
import {
BufferGeometry
} from 'three';
import {
WorkerTaskCommandResponse,
WorkerMessage,
WorkerTaskWorker,
comRouting
} from 'wtd-core';
import {
MeshPayload
} from 'wtd-three-ext';
class InfiniteWorkerExternalGeometry implements WorkerTaskWorker {
private bufferGeometry?: BufferGeometry = undefined;
init(message: WorkerMessage) {
const wm = WorkerMessage.unpack(message, false);
if (wm.payloads.length > 0) {
this.bufferGeometry = (wm.payloads[0] as MeshPayload).message.bufferGeometry as BufferGeometry;
}
const initComplete = WorkerMessage.createFromExisting(message, {
overrideCmd: WorkerTaskCommandResponse.INIT_COMPLETE
});
self.postMessage(initComplete);
}
execute(message: WorkerMessage) {
if (!this.bufferGeometry) {
self.postMessage(new Error('No initial payload available'));
} else {
// clone before re-using as othewise transferables can not be obtained
const geometry = this.bufferGeometry.clone();
geometry.name = 'tmProto' + message.uuid;
const vertexArray = geometry.getAttribute('position').array;
for (let i = 0; i < vertexArray.length; i++) {
vertexArray[i] = vertexArray[i] + 10 * (Math.random() - 0.5);
}
const meshPayload = new MeshPayload();
meshPayload.setBufferGeometry(geometry, 2);
const randArray = new Uint8Array(3);
self.crypto.getRandomValues(randArray);
meshPayload.message.params = {
color: {
r: randArray[0] / 255,
g: randArray[1] / 255,
b: randArray[2] / 255
}
};
const execComplete = WorkerMessage.createFromExisting(message, {
overrideCmd: WorkerTaskCommandResponse.EXECUTE_COMPLETE
});
execComplete.addPayload(meshPayload);
const transferables = WorkerMessage.pack(execComplete.payloads, false);
self.postMessage(execComplete, transferables);
}
}
}
const worker = new InfiniteWorkerExternalGeometry();
self.onmessage = message => comRouting(worker, message);