increase performance of holes

This commit is contained in:
casperlamboo 2018-01-06 20:27:23 +01:00
parent ab27e7c8c2
commit 834ca531fd
2 changed files with 34 additions and 20 deletions

14
src/d3/ShapeMesh.js vendored
View File

@ -64,7 +64,7 @@ class ShapeMesh extends THREE.Object3D {
if (holes === this._holes && !this._changedGeometry) return false; if (holes === this._holes && !this._changedGeometry) return false;
const fill = this._shapeData.type === 'EXPORT_SHAPE' ? !this._shapeData.originalFill : !this._fill; const fill = this._shapeData.type === 'EXPORT_SHAPE' ? !this._shapeData.originalFill : !this._fill;
if (holes === null || fill) { if (holes.length === 0 || fill) {
if (this._holeMeshIsOriginal && !this._changedGeometry) return false; if (this._holeMeshIsOriginal && !this._changedGeometry) return false;
this._holeMesh.geometry.dispose(); this._holeMesh.geometry.dispose();
@ -76,10 +76,16 @@ class ShapeMesh extends THREE.Object3D {
const objectGeometry = new THREE.Geometry().fromBufferGeometry(this._mesh.geometry); const objectGeometry = new THREE.Geometry().fromBufferGeometry(this._mesh.geometry);
objectGeometry.mergeVertices(); objectGeometry.mergeVertices();
let objectBSP = new THREE_BSP(objectGeometry); const box = new THREE.Box3().setFromPoints(objectGeometry.vertices);
let bsp = new THREE_BSP(objectGeometry);
objectGeometry.dispose(); objectGeometry.dispose();
objectBSP = objectBSP.subtract(holes);
this._holeMesh.geometry = objectBSP.toMesh().geometry; for (const hole of holes) {
if (hole.box.intersectsBox(box)) {
bsp = bsp.subtract(hole.bsp);
}
}
this._holeMesh.geometry = bsp.toMesh().geometry;
this._holes = holes; this._holes = holes;
this._changedGeometry = false; this._changedGeometry = false;

View File

@ -14,7 +14,7 @@ export default class ShapesManager extends THREE.Object3D {
this._spaces = {}; this._spaces = {};
this.name = 'shapes-manager'; this.name = 'shapes-manager';
this._holes = null; this._holes = [];
// this._edges = {}; // this._edges = {};
} }
@ -77,23 +77,33 @@ export default class ShapesManager extends THREE.Object3D {
} }
if (holesChanged) { if (holesChanged) {
this._holes = null; this._holes = [];
for (let i = 0; i < ids.length; i ++) { for (let i = 0; i < ids.length; i ++) {
const id = ids[i]; const id = ids[i];
if (activeShapes[id]) continue;
const { solid, type } = state.objectsById[id]; const { solid, type } = state.objectsById[id];
const d3Visible = SHAPE_TYPE_PROPERTIES[type].D3Visible; const d3Visible = SHAPE_TYPE_PROPERTIES[type].D3Visible;
if (!solid && d3Visible) { if (solid || !d3Visible) continue;
const hole = this._meshes[id].mesh._mesh;
const holeGeometry = new THREE.Geometry().fromBufferGeometry(hole.geometry); const holeMesh = this._meshes[id].mesh._mesh;
if (holeGeometry.vertices.length === 0) continue; const geometry = new THREE.Geometry().fromBufferGeometry(holeMesh.geometry);
const holeBSP = new THREE_BSP(holeGeometry); if (geometry.vertices.length === 0) continue;
if (!this._holes) {
this._holes = holeBSP; const box = new THREE.Box3().setFromPoints(geometry.vertices);
} else { const intersectHole = this._holes.find(hole => hole.box.intersectsBox(box));
this._holes = this._holes.union(holeBSP);
} const bsp = new THREE_BSP(geometry);
holeGeometry.dispose();
if (intersectHole) {
intersectHole.bsp = intersectHole.bsp.union(bsp);
const min = box.min.min(intersectHole.box.min);
const max = box.max.max(intersectHole.box.max);
intersectHole.box = new THREE.Box3(min, max);
} else {
this._holes.push({ bsp, box });
} }
geometry.dispose();
} }
} }
@ -104,9 +114,7 @@ export default class ShapesManager extends THREE.Object3D {
const d3Visible = SHAPE_TYPE_PROPERTIES[type].D3Visible; const d3Visible = SHAPE_TYPE_PROPERTIES[type].D3Visible;
if (!active && solid && d3Visible) { if (!active && solid && d3Visible) {
const shape = this._meshes[id].mesh; const shape = this._meshes[id].mesh;
if (shape.updateHoleGeometry(this._holes)) { if (shape.updateHoleGeometry(this._holes)) render = true;
render = true;
}
} }
} }