Skip to content

Commit 9817d92

Browse files
author
Avaer Kazmer
committed
Optimize chunks ranging
1 parent 50395a3 commit 9817d92

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

spatial-engine.js

+36-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const localVector = new THREE.Vector3();
22
const localVector2 = new THREE.Vector3();
33
const localVector3 = new THREE.Vector3();
44
const localVector4 = new THREE.Vector3();
5+
const localVector5 = new THREE.Vector3();
56
const localVector2D = new THREE.Vector2();
67
const localQuaternion = new THREE.Quaternion();
78
const localRaycaster = new THREE.Raycaster();
@@ -17,6 +18,7 @@ const _makePromise = () => {
1718
p.reject = reject;
1819
return p;
1920
};
21+
const _floorVector = v => v.set(Math.floor(v.x), Math.floor(v.y), Math.floor(v.z));
2022

2123
export class XRRaycaster {
2224
constructor({width = 512, height = 512, fov = 60, aspect = 1, depth = 3, near = 0.1, far = 300, renderer = new THREE.WebGLRenderer(), onRender = (target, camera) => {}} = {}) {
@@ -224,6 +226,8 @@ export class XRChunker extends EventTarget {
224226
});
225227
return worker;
226228
})();
229+
230+
this._neededCoordsCache = [];
227231
}
228232
getChunkAt(x, y, z) {
229233
for (let i = 0; i < this.chunks.length; i++) {
@@ -242,14 +246,26 @@ export class XRChunker extends EventTarget {
242246
const quaternion = localQuaternion.fromArray(q);
243247
const scale = localVector2.fromArray(s);
244248

245-
const _floorVector = v => v.set(Math.floor(v.x), Math.floor(v.y), Math.floor(v.z));
246249
const cameraCenter = _floorVector(localVector3.copy(position)).add(localVector4.set(0.5, 0.5, 0.5));
247250

248-
const neededCoords = [];
251+
const neededCoords = this._neededCoordsCache;
252+
let numNeededCoords = 0;
253+
const _hasNeededCoord = coord => {
254+
for (let i = 0; i < numNeededCoords; i++) {
255+
if (neededCoords[i].equals(coord)) {
256+
return true;
257+
}
258+
}
259+
return false;
260+
};
249261
const _addNeededCoord = (x, y, z) => {
250-
const c = _floorVector(cameraCenter.clone().add(localVector4.set(x, y, z).applyQuaternion(quaternion)));
251-
if (!neededCoords.some(c2 => c2.equals(c))) {
252-
neededCoords.push(c);
262+
const c = _floorVector(localVector4.copy(cameraCenter).add(localVector5.set(x, y, z).applyQuaternion(quaternion)));
263+
if (!_hasNeededCoord(c)) {
264+
const index = numNeededCoords++;
265+
if (index >= neededCoords.length) {
266+
neededCoords.push(new THREE.Vector3());
267+
}
268+
neededCoords[index].copy(c);
253269
}
254270
}
255271
for (let z = 0; z >= -scale.z/2; z--) {
@@ -270,18 +286,21 @@ export class XRChunker extends EventTarget {
270286
}
271287
}
272288

273-
const missingChunkCoords = neededCoords.filter(c => !this.getChunkAt(c.x, c.y, c.z));
274-
const outrangedChunks = this.chunks.filter(chunk => !neededCoords.some(coord => coord.equals(chunk.object.position)));
275-
for (let i = 0; i < outrangedChunks.length; i++) {
276-
const chunk = outrangedChunks[i];
277-
this.chunks.splice(this.chunks.indexOf(chunk), 1);
278-
this.dispatchEvent(new MessageEvent('removechunk', {data: chunk}));
279-
}
280-
for (let i = 0; i < missingChunkCoords.length; i++) {
281-
const coord = missingChunkCoords[i];
282-
const chunk = new XRChunk(coord.x, coord.y, coord.z);
283-
this.chunks.push(chunk);
284-
this.dispatchEvent(new MessageEvent('addchunk', {data: chunk}));
289+
this.chunks = this.chunks.filter(chunk => {
290+
if (_hasNeededCoord(chunk.object.position)) {
291+
return true;
292+
} else {
293+
this.dispatchEvent(new MessageEvent('removechunk', {data: chunk}));
294+
return false;
295+
}
296+
});
297+
for (let i = 0; i < numNeededCoords; i++) {
298+
const coord = neededCoords[i];
299+
if (!this.getChunkAt(coord.x, coord.y, coord.z)) {
300+
const chunk = new XRChunk(coord.x, coord.y, coord.z);
301+
this.chunks.push(chunk);
302+
this.dispatchEvent(new MessageEvent('addchunk', {data: chunk}));
303+
}
285304
}
286305
}
287306
async updateMesh(getPointCloud) {

0 commit comments

Comments
 (0)