consolidate widget mesh position / rotation updates for setTopZ and setIndex

This commit is contained in:
Stewart Allen 2022-10-31 14:39:51 -04:00
commit abd1a763aa
2 changed files with 49 additions and 35 deletions

View file

@ -74,6 +74,9 @@ CAM.init = function(kiri, api) {
if (!isCamMode) {
return;
}
if (isIndexed) {
settings.process.camZAnchor = "middle";
}
animVer = isIndexed ? 1 : 0;
updateStock();
SPACE.platform.setVisible(!isIndexed);
@ -166,6 +169,7 @@ CAM.init = function(kiri, api) {
api.ui.camStock.marker.style.display = proc.camStockOn ? 'flex' : 'none';
updateStock(settings, 'settings.saved.internal');
setAnimationStyle(settings);
func.opRender();
});
api.event.on("settings.load", (settings) => {
@ -432,7 +436,7 @@ CAM.init = function(kiri, api) {
let inside = true;
let popped = false;
let poprec = popOp[rec.type];
if (type === 'index' && indexing) {
if (type === 'index' & indexing && !rec.disabled) {
index = rec.absolute ? rec.degrees : index + rec.degrees;
}
el.rec = rec;

View file

@ -459,6 +459,14 @@ class Widget {
setIndexed(z) {
this.track.indexed = z;
this.setTopZ(this.track.top);
}
setAxisIndex(deg) {
let rad = deg * (Math.PI / 180);
this.track.indexRad = rad;
this.mesh.rotation.x = rad;
this.setTopZ(this.track.top);
}
/**
@ -468,33 +476,32 @@ class Widget {
*/
setTopZ(z) {
let mesh = this.mesh,
pos = this.track.pos,
ltz = this.last_top_z || {},
track = this.track,
ltop = track.top || 0,
mbz = mesh.getBoundingBox().max.z;
if (z) {
pos.z = mbz - z;
mesh.position.z = -pos.z - 0.01;
this.track.top = z;
track.top = z;
} else {
pos.z = 0;
mesh.position.z = -this.track.zcut || 0;
this.track.top = mbz;
track.top = mbz;
}
this.track.tzoff = mbz - z;
if (this.track.indexed) {
mesh.position.z -= this.track.indexed / 2;
// difference between top of stock/bounds and top of widget (cam mode)
track.tzoff = mbz - z;
if (track.indexed) {
let rad = track.indexRad;
let mz = track.box.h / 2;
let dx = Math.cos(rad) * mz;
let dy = Math.sin(rad) * mz;
track.delta = {
x: 0,
y: Math.sin(rad) * mz,
z: -(track.indexed / 2) + (mz - dx)
};
} else {
track.delta = { x:0, y:0, z:0};
this.mesh.rotation.x = 0;
}
let ntz = {
pz: pos.z,
mpz: mesh.position.z
};
this.modified |= (ltz.pz !== ntz.pz || ltz.mpz !== ntz.mpz);
this.last_top_z = ntz;
}
cutZ(dist) {
this.track.zcut = dist;
this.setTopZ();
this.modified |= (ltop !== z);
this._updateMeshPosition();
}
move(x, y, z, abs) {
@ -506,25 +513,21 @@ class Widget {
_move(x, y, z, abs) {
let mesh = this.mesh,
mat = this.getMaterial(),
pos = this.track.pos,
zcut = this.track.zcut || 0;
// do not allow moves in pure slice view
pos = this.track.pos;
// do not allow moves in pure slice view
if (!mat.visible) return;
if (abs) {
mesh.position.set(x, y, z - zcut);
pos.x = (x || 0);
pos.y = (y || 0);
pos.z = (z || 0);
} else {
mesh.position.x += ( x || 0);
mesh.position.y += ( y || 0);
mesh.position.z += (-z || 0);
pos.x += (x || 0);
pos.y += (y || 0);
pos.z += (z || 0);
}
if (x || y || z) {
this.setModified();
this._updateMeshPosition();
// allow for use in engine / cli
if (api && api.event) {
api.event.emit('widget.move', {widget: this, pos});
@ -532,6 +535,17 @@ class Widget {
}
}
_updateMeshPosition() {
let mesh = this.mesh,
track = this.track,
delta = track.delta || { x:0, y:0, z:0 },
tzoff = track.tzoff,
top = track.top,
tz = tzoff !== top ? -tzoff : 0,
pos = track.pos;
mesh.position.set(pos.x + delta.x, pos.y + delta.y, pos.z + delta.z + tz);
}
scale(x, y, z) {
this.group.forEach(w => {
w._scale(x, y, z);
@ -552,10 +566,6 @@ class Widget {
this.setModified();
}
setAxisIndex(deg) {
this.mesh.rotation.x = deg * (Math.PI / 180);
}
rotate(x, y, z, temp) {
this.group.forEach(w => {
w._rotate(x, y, z, temp);