work on getting index translated widget geometry

This commit is contained in:
Stewart Allen 2022-11-01 19:55:30 -04:00
commit 2af361fc5a
8 changed files with 57 additions and 37 deletions

View file

@ -400,7 +400,8 @@ kiri.Layers.prototype.encode = function(state) {
faces: codec.allocFloat32Array(paths.faces, zeros),
norms: codec.allocFloat32Array(paths.norms, zeros)
} : undefined,
cpath: layer.cpath || codec.undef
cpath: layer.cpath || codec.undef,
rotation: layer.rotation
}
})
};
@ -430,7 +431,8 @@ registerDecoder(TYPE.LAYERS, function(v, state) {
faces: codec.allocFloat32Array(paths.faces),
norms: codec.allocFloat32Array(paths.norms)
} : undefined,
cpath: data.cpath
cpath: data.cpath,
rotation: data.rotation
};
// fixup null -> Infinity in material counts (JSON stringify sucks)
if (d.cface) {

View file

@ -40,6 +40,12 @@ class Layers {
return this.layers[layer];
}
// in radians
setRotation(x = 0, y = 0, z = 0) {
this.rotation = { x, y, z };
return this;
}
setLayer(layer, colors, off) {
let layers = this.layers;
if (typeof(colors) === 'number') {
@ -50,6 +56,7 @@ class Layers {
};
}
this.current = layers[layer] = layers[layer] || {
rotation: this.rotation,
off: off === true,
lines: [],
polys: [], // colors are an attribute on polygons
@ -59,8 +66,7 @@ class Layers {
paths: undefined, // 3d extrusion / tube paths (used to be an array, now merged - FIX)
cpath: undefined, // path colors indices
color: colors || {
index: 0,
check: 0,
check: 0, // unused?
line: 0,
face: 0,
opacity: 1

View file

@ -106,8 +106,8 @@ class Stack {
}
}
const { polys, lines, faces, cface, paths, cpath, color, off, norms } = layer;
const { index, opacity } = color;
const { polys, lines, faces, cface, paths, cpath, color, off, norms, rotation } = layer;
const { opacity } = color;
const meshes = [];
const defstate = !off;
const mats = [];
@ -155,7 +155,7 @@ class Stack {
}
geo.setFromPoints(vert);
const segs = new THREE.LineSegments(geo, mat);
if (index) segs.rotation.x = index;
if (rotation) segs.rotation.x = rotation.x;
meshes.push(segs);
group.add(segs);
mats.appendAll(mat);
@ -186,7 +186,7 @@ class Stack {
geo.addGroup(0, Infinity, 0);
}
const mesh = new THREE.Mesh(geo, mat);
if (index) mesh.rotation.x = index;
if (rotation) mesh.rotation.x = rotation.x;
// mesh.castShadow = true;
// mesh.receiveShadow = true;
meshes.push(mesh);
@ -221,7 +221,7 @@ class Stack {
}
const mesh = new THREE.Mesh(geo, mat);
mesh.position.z = z;
if (index) mesh.rotation.x = index;
if (rotation) mesh.rotation.x = rotation.x;
// mesh.castShadow = true;
// mesh.receiveShadow = true;
meshes.push(mesh);

View file

@ -649,40 +649,37 @@ class Widget {
this.points = null;
}
getGeoVertices(unroll) {
getGeoVertices(unroll, index) {
let geo = this.mesh.geometry;
let pos = geo.getAttribute('position').array;
let pos = geo.getAttribute('position');
// if indexed, return points rotated about X and then offset
let track = this.track;
if (index && track.indexed) {
let delta = track.delta;
pos = pos.clone().applyMatrix4(
new THREE.Matrix4().makeRotationX(track.indexRad)
).applyMatrix4(
new THREE.Matrix4().makeTranslation(0, -delta.y, -delta.z + track.tzoff)
);
}
pos = pos.array;
if (geo.index && unroll !== false) {
let idx = geo.index.array;
let len = idx.length;
let p2 = new Float32Array(len * 3);
let pp2 = new Float32Array(len * 3);
let inc = 0;
for (let i=0; i<len; i++) {
let iv = idx[i];
let ip = iv * 3;
p2[inc++] = pos[ip++];
p2[inc++] = pos[ip++];
p2[inc++] = pos[ip];
pp2[inc++] = pos[ip++];
pp2[inc++] = pos[ip++];
pp2[inc++] = pos[ip];
}
return p2;
} else {
return pos;
pos = pp2;
} else if (index) {
pos = pos.slice();
}
}
iterPoints() {
let verts = this.getGeoVertices();
let index = 0;
let count = verts.length;
return new class ITER {
[Symbol.iterator]() { return {
next: () => {
let done = index >= count;
return done ? { done } :
{ value: newPoint(verts[index++], verts[index++], verts[index++]) };
}
} }
};
return pos;
}
getPoints() {