simplify update logic

This commit is contained in:
casperlamboo 2017-11-28 14:16:17 +01:00
parent a5d7b22a1c
commit 0acda53661

84
src/d3/ShapeMesh.js vendored
View File

@ -33,6 +33,9 @@ class ShapeMesh extends THREE.Object3D {
this._shapesMap = []; this._shapesMap = [];
this._center = new Vector(); this._center = new Vector();
this._holeMesh = new THREE.Mesh(new THREE.Geometry(), material.clone());
this._holeMesh.name = shapeData.UID;
this._holeMesh.isShapeMesh = true;
this._sculpt = sculpt; this._sculpt = sculpt;
this._rotate = rotate; this._rotate = rotate;
@ -41,16 +44,10 @@ class ShapeMesh extends THREE.Object3D {
this._type = type; this._type = type;
this._transform = transform; this._transform = transform;
this._z = z; this._z = z;
this._shapeData = shapeData;
this._color = color; this._color = color;
this._fill = fill; this._fill = fill;
this.updatePoints(shapeData);
this._holeMesh = new THREE.Mesh(new THREE.Geometry().fromBufferGeometry(this._mesh.geometry), material.clone());
this._holeMesh.name = shapeData.UID;
this._holeMesh.isShapeMesh = true;
this.updateSolid(solid, active); this.updateSolid(solid, active);
this.updatePoints(shapeData);
} }
add(object) { add(object) {
@ -85,42 +82,13 @@ class ShapeMesh extends THREE.Object3D {
update(shapeData, active) { update(shapeData, active) {
let changed = false; let changed = false;
if (shapeChanged(this._shapeData, shapeData)) { if (this.updatePoints(shapeData)) changed = true;
this.updatePoints(shapeData); if (this.updateTransform(shapeData.transform, shapeData.z)) changed = true;
changed = true; if (this.updateHeight(shapeData.height)) changed = true;
} if (this.updateSculpt(shapeData.sculpt)) changed = true;
if (this.updateTwist(shapeData.twist)) changed = true;
if (shapeData.transform !== this._transform || shapeData.z !== this._z) { if (this.updateColor(shapeData.color)) changed = true;
this.updateTransform(shapeData.transform, shapeData.z); if (this.updateSolid(shapeData.solid, active)) changed = true;
changed = true;
}
if (shapeData.height !== this._height) {
this.updateHeight(shapeData.height);
changed = true;
}
if (shapeData.sculpt !== this._sculpt) {
this.updateSculpt(shapeData.sculpt);
changed = true;
}
if (shapeData.twist !== this._twist) {
this.updateTwist(shapeData.twist);
changed = true;
}
if (shapeData.color !== this._color) {
this.updateColor(shapeData.color);
changed = true;
}
let solidChanged = false;
if (shapeData.solid !== this._solid || active !== this._active) {
this.updateSolid(shapeData.solid, active);
changed = true;
solidChanged = true;
}
this._shapeData = shapeData; this._shapeData = shapeData;
return changed; return changed;
@ -129,6 +97,8 @@ class ShapeMesh extends THREE.Object3D {
setOpaque(opaque) { setOpaque(opaque) {
this._holeMesh.material.opacity = opaque ? 1.0 : DESELECT_TRANSPARENCY; this._holeMesh.material.opacity = opaque ? 1.0 : DESELECT_TRANSPARENCY;
this._holeMesh.material.transparent = !opaque; this._holeMesh.material.transparent = !opaque;
this._mesh.material.opacity = opaque ? 1.0 : DESELECT_TRANSPARENCY;
this._mesh.material.transparent = !opaque;
} }
dispose() { dispose() {
@ -137,6 +107,8 @@ class ShapeMesh extends THREE.Object3D {
} }
updatePoints(shapeData) { updatePoints(shapeData) {
if (this._shapeData && !shapeChanged(this._shapeData, shapeData)) return false;
this._fill = shapeData.fill; this._fill = shapeData.fill;
this._points = shapeData.points; this._points = shapeData.points;
this._rectSize = shapeData.rectSize; this._rectSize = shapeData.rectSize;
@ -159,9 +131,13 @@ class ShapeMesh extends THREE.Object3D {
this._updateFaces(); this._updateFaces();
} }
this._updateVertices(); this._updateVertices();
return true;
} }
updateSculpt(sculpt) { updateSculpt(sculpt) {
if (sculpt === this._sculpt) return false;
if (!sculpt.every(({ pos, scale }) => isValidNumber(pos) && isValidNumber(scale))) { if (!sculpt.every(({ pos, scale }) => isValidNumber(pos) && isValidNumber(scale))) {
throw new Error(`Cannot update object ${this.name}: sculpt contains invalid values.`); throw new Error(`Cannot update object ${this.name}: sculpt contains invalid values.`);
} }
@ -169,9 +145,13 @@ class ShapeMesh extends THREE.Object3D {
this._sculpt = sculpt; this._sculpt = sculpt;
this._updateSide(); this._updateSide();
this._updateVertices(); this._updateVertices();
return true;
} }
updateTwist(twist) { updateTwist(twist) {
if (twist === this._twist) return false;
if (!isValidNumber(twist)) { if (!isValidNumber(twist)) {
throw new Error(`Cannot update object ${this.name}: twist is an invalid value.`); throw new Error(`Cannot update object ${this.name}: twist is an invalid value.`);
} }
@ -179,9 +159,13 @@ class ShapeMesh extends THREE.Object3D {
this._twist = twist; this._twist = twist;
this._updateSide(); this._updateSide();
this._updateVertices(); this._updateVertices();
return true;
} }
updateHeight(height) { updateHeight(height) {
if (height === this._height) return false;
if (!isValidNumber(height)) { if (!isValidNumber(height)) {
throw new Error(`Cannot update object ${this.name}: height is an invalid value.`); throw new Error(`Cannot update object ${this.name}: height is an invalid value.`);
} }
@ -189,9 +173,13 @@ class ShapeMesh extends THREE.Object3D {
this._height = height; this._height = height;
this._updateSide(); this._updateSide();
this._updateVertices(); this._updateVertices();
return true;
} }
updateTransform(transform, z) { updateTransform(transform, z) {
if (transform === this._transform && z === this._z) return false;
// TODO // TODO
// transform.matrix.every could improved performance wise // transform.matrix.every could improved performance wise
if (!transform.matrix.every(isValidNumber)) { if (!transform.matrix.every(isValidNumber)) {
@ -201,9 +189,13 @@ class ShapeMesh extends THREE.Object3D {
this._transform = transform; this._transform = transform;
this._z = z; this._z = z;
this._updateVertices(); this._updateVertices();
return true;
} }
updateColor(color) { updateColor(color) {
if (color === this._color) return false;
if (!isValidNumber(color)) { if (!isValidNumber(color)) {
throw new Error(`Cannot update object ${this.name}: color is an invalid value.`); throw new Error(`Cannot update object ${this.name}: color is an invalid value.`);
} }
@ -211,9 +203,13 @@ class ShapeMesh extends THREE.Object3D {
this._mesh.material.color = new THREE.Color(color); this._mesh.material.color = new THREE.Color(color);
this._holeMesh.material.color = new THREE.Color(color); this._holeMesh.material.color = new THREE.Color(color);
this._color = color; this._color = color;
return true;
} }
updateSolid(solid, active) { updateSolid(solid, active) {
if (solid === this._solid && active === this._active) return false;
this._mesh.material.opacity = solid ? 1.0 : 0.0; this._mesh.material.opacity = solid ? 1.0 : 0.0;
this._mesh.material.transparent = !solid; this._mesh.material.transparent = !solid;
this.visible = solid || active; this.visible = solid || active;
@ -228,6 +224,8 @@ class ShapeMesh extends THREE.Object3D {
this._solid = solid; this._solid = solid;
this._active = active; this._active = active;
return true;
} }
_getPoint(point, heightStep, center) { _getPoint(point, heightStep, center) {