Skip to content

Commit 8ecd7cf

Browse files
slimbuckMartin Valigursky
authored andcommitted
Support gsplat worker on nodejs (#7770)
1 parent 71388da commit 8ecd7cf

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/scene/gsplat/gsplat-sorter.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { EventHandler } from '../../core/event-handler.js';
22
import { TEXTURELOCK_READ } from '../../platform/graphics/constants.js';
3+
import { platform } from '../../core/platform.js';
34

45
// sort blind set of data
56
function SortWorker() {
7+
const myself = (typeof self !== 'undefined' && self) || (require('node:worker_threads').parentPort);
8+
69
let order;
710
let centers;
811
let chunks;
@@ -196,26 +199,28 @@ function SortWorker() {
196199
}
197200

198201
// send results
199-
self.postMessage({
202+
myself.postMessage({
200203
order: order.buffer,
201204
count
202205
}, [order.buffer]);
203206

204207
order = null;
205208
};
206209

207-
self.onmessage = (message) => {
208-
if (message.data.order) {
209-
order = new Uint32Array(message.data.order);
210+
myself.addEventListener('message', (message) => {
211+
const msgData = message.data ?? message;
212+
213+
if (msgData.order) {
214+
order = new Uint32Array(msgData.order);
210215
}
211-
if (message.data.centers) {
212-
centers = new Float32Array(message.data.centers);
216+
if (msgData.centers) {
217+
centers = new Float32Array(msgData.centers);
213218
forceUpdate = true;
214219

215-
if (message.data.chunks) {
216-
const chunksSrc = new Float32Array(message.data.chunks);
220+
if (msgData.chunks) {
221+
const chunksSrc = new Float32Array(msgData.chunks);
217222
// reuse chunks memory, but we only need 4 floats per chunk
218-
chunks = new Float32Array(message.data.chunks, 0, chunksSrc.length * 4 / 6);
223+
chunks = new Float32Array(msgData.chunks, 0, chunksSrc.length * 4 / 6);
219224

220225
boundMin.x = chunksSrc[0];
221226
boundMin.y = chunksSrc[1];
@@ -297,15 +302,15 @@ function SortWorker() {
297302
}
298303
}
299304
}
300-
if (message.data.hasOwnProperty('mapping')) {
301-
mapping = message.data.mapping ? new Uint32Array(message.data.mapping) : null;
305+
if (msgData.hasOwnProperty('mapping')) {
306+
mapping = msgData.mapping ? new Uint32Array(msgData.mapping) : null;
302307
forceUpdate = true;
303308
}
304-
if (message.data.cameraPosition) cameraPosition = message.data.cameraPosition;
305-
if (message.data.cameraDirection) cameraDirection = message.data.cameraDirection;
309+
if (msgData.cameraPosition) cameraPosition = msgData.cameraPosition;
310+
if (msgData.cameraDirection) cameraDirection = msgData.cameraDirection;
306311

307312
update();
308-
};
313+
});
309314
}
310315

311316
class GSplatSorter extends EventHandler {
@@ -318,12 +323,23 @@ class GSplatSorter extends EventHandler {
318323
constructor() {
319324
super();
320325

321-
this.worker = new Worker(URL.createObjectURL(new Blob([`(${SortWorker.toString()})()`], {
322-
type: 'application/javascript'
323-
})));
326+
const workerSource = `(${SortWorker.toString()})()`;
327+
const nodeEnv = platform.environment === 'node';
324328

325-
this.worker.onmessage = (message) => {
326-
const newOrder = message.data.order;
329+
if (nodeEnv) {
330+
this.worker = new Worker(workerSource, {
331+
eval: true
332+
});
333+
} else {
334+
this.worker = new Worker(URL.createObjectURL(new Blob([workerSource], {
335+
type: 'application/javascript'
336+
})));
337+
}
338+
339+
this.worker[nodeEnv ? 'on' : 'addEventListener']('message', (message) => {
340+
const msgData = message.data ?? message;
341+
342+
const newOrder = msgData.order;
327343
const oldOrder = this.orderTexture._levels[0].buffer;
328344

329345
// send vertex storage to worker to start the next frame
@@ -336,8 +352,8 @@ class GSplatSorter extends EventHandler {
336352
this.orderTexture.upload();
337353

338354
// set new data directly on texture
339-
this.fire('updated', message.data.count);
340-
};
355+
this.fire('updated', msgData.count);
356+
});
341357
}
342358

343359
destroy() {

0 commit comments

Comments
 (0)