From 3fff787c683767e31eb6738e817e3093baddfd35 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Wed, 2 Nov 2022 09:03:38 -0400 Subject: [PATCH] add full position and rotation control to layers. move stock boundary lines into THREE mesh --- src/kiri-mode/cam/client.js | 53 +++++++++++++++++-------------------- src/kiri/codec.js | 6 +++-- src/kiri/layers.js | 6 +++++ src/kiri/stack.js | 19 ++++++------- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/kiri-mode/cam/client.js b/src/kiri-mode/cam/client.js index e0526e38..787fa561 100644 --- a/src/kiri-mode/cam/client.js +++ b/src/kiri-mode/cam/client.js @@ -1636,11 +1636,6 @@ function updateStock(args, event) { return; } - STACKS.remove('bounds'); - if (isCamMode && isArrange) { - STACKS.clear(); - } - if (!isCamMode) { SPACE.world.remove(camZBottom); SPACE.world.remove(camStock); @@ -1705,6 +1700,29 @@ function updateStock(args, event) { }); camStock = new THREE.Mesh(geo, mat); camStock.renderOrder = 2; + + let lo = 0.5; + let lidat = [ + lo,lo,lo, lo,lo,-lo, + lo,lo,lo, lo,-lo,lo, + lo,lo,lo, -lo,lo,lo, + -lo,-lo,-lo, -lo,-lo,lo, + -lo,-lo,-lo, -lo,lo,-lo, + -lo,-lo,-lo, lo,-lo,-lo, + lo,lo,-lo, -lo,lo,-lo, + lo,lo,-lo, lo,-lo,-lo, + lo,-lo,-lo, lo,-lo,lo, + lo,-lo,lo, -lo,-lo,lo, + -lo,-lo,lo, -lo,lo,lo, + -lo,lo,lo, -lo,lo,-lo + ]; + let ligeo = new THREE.BufferGeometry(); + ligeo.setAttribute('position', new THREE.BufferAttribute(lidat.toFloat32(), 3)); + let limat = new THREE.LineBasicMaterial({ color: 0xaaaaaa }); + let lines = new THREE.LineSegments(ligeo, limat); + camStock.lines = lines; + camStock.add(lines); + SPACE.world.add(camStock); } stock = settings.stock = { @@ -1728,6 +1746,8 @@ function updateStock(args, event) { camStock.position.z = 0; } delta = csz - topZ; + camStock.lines.material.color = + new THREE.Color(API.space.is_dark() ? 0x555555 : 0xaaaaaa); } else if (camStock) { SPACE.world.remove(camStock); camStock = null; @@ -1760,29 +1780,6 @@ function updateStock(args, event) { camZBottom = undefined; } - const {x, y, z} = stock; - if (x && y && z && !STACKS.getStack('bounds')) { - const render = new kiri.Layers() - .setRotation(currentIndex) - .setLayer('bounds', { face: 0xaaaaaa, line: 0xaaaaaa }); - const stack = STACKS.setFreeMem(false).create('bounds', SPACE.world); - const hx = x/2, hy = y/2; - const sz = stock.z || 0; - const zm = isIndexed ? -sz / 2 : 0; - const zM = isIndexed ? sz / 2 : sz; - stack.add(render.addPolys([ - newPolygon().centerRectangle({x:csox, y:csoy, z:zm}, x, y), - newPolygon().centerRectangle({x:csox, y:csoy, z:zM}, x, y) - ], { thin: true } )); - stack.add(render.addLines([ - newPoint(csox + hx, csoy - hy, zm), newPoint(csox + hx, csoy - hy, zM), - newPoint(csox + hx, csoy + hy, zm), newPoint(csox + hx, csoy + hy, zM), - newPoint(csox - hx, csoy - hy, zm), newPoint(csox - hx, csoy - hy, zM), - newPoint(csox - hx, csoy + hy, zm), newPoint(csox - hx, csoy + hy, zM), - ], { thin: true })); - STACKS.setFreeMem(true); - } - API.platform.update_top_z(delta); API.platform.update_origin(); SPACE.update(); diff --git a/src/kiri/codec.js b/src/kiri/codec.js index 7ed92b77..a8ac0988 100644 --- a/src/kiri/codec.js +++ b/src/kiri/codec.js @@ -403,7 +403,8 @@ kiri.Layers.prototype.encode = function(state) { norms: codec.allocFloat32Array(paths.norms, zeros) } : undefined, cpath: layer.cpath || codec.undef, - rotation: layer.rotation + rotation: layer.rotation, + posiiton: layer.position } }) }; @@ -434,7 +435,8 @@ registerDecoder(TYPE.LAYERS, function(v, state) { norms: codec.allocFloat32Array(paths.norms) } : undefined, cpath: data.cpath, - rotation: data.rotation + rotation: data.rotation, + position: data.position }; // fixup null -> Infinity in material counts (JSON stringify sucks) if (d.cface) { diff --git a/src/kiri/layers.js b/src/kiri/layers.js index 6d9f762e..13197e5e 100644 --- a/src/kiri/layers.js +++ b/src/kiri/layers.js @@ -46,6 +46,11 @@ class Layers { return this; } + setPosition(x = 0, y = 0, z = 0) { + this.position = { x, y, z }; + return this; + } + setLayer(layer, colors, off) { let layers = this.layers; if (typeof(colors) === 'number') { @@ -57,6 +62,7 @@ class Layers { } this.current = layers[layer] = layers[layer] || { rotation: this.rotation, + position: this.position, off: off === true, lines: [], polys: [], // colors are an attribute on polygons diff --git a/src/kiri/stack.js b/src/kiri/stack.js index 5faff093..6d1e7fe2 100644 --- a/src/kiri/stack.js +++ b/src/kiri/stack.js @@ -106,7 +106,8 @@ class Stack { } } - const { polys, lines, faces, cface, paths, cpath, color, off, norms, rotation } = layer; + const { polys, lines, faces, cface, paths, cpath } = layer; + const { color, off, norms, rotation, position } = layer; const { opacity } = color; const meshes = []; const defstate = !off; @@ -155,7 +156,8 @@ class Stack { } geo.setFromPoints(vert); const segs = new THREE.LineSegments(geo, mat); - if (rotation) segs.rotation.x = rotation.x; + if (rotation) segs.rotation.set(rotation.x, rotation.y, rotation.z); + if (position) segs.position.set(position.x, position.y, position.z); meshes.push(segs); group.add(segs); mats.appendAll(mat); @@ -176,7 +178,6 @@ class Stack { } if (norms) { geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3)); - // geo.normalizeNormals(); } else { geo.computeVertexNormals(); } @@ -186,9 +187,8 @@ class Stack { geo.addGroup(0, Infinity, 0); } const mesh = new THREE.Mesh(geo, mat); - if (rotation) mesh.rotation.x = rotation.x; - // mesh.castShadow = true; - // mesh.receiveShadow = true; + if (rotation) mesh.rotation.set(rotation.x, rotation.y, rotation.z); + if (position) mesh.position.set(position.x, position.y, position.z); meshes.push(mesh); group.add(mesh); mats.appendAll(mat); @@ -201,7 +201,6 @@ class Stack { mat.push(newMat(color)); } mat.forEach(m => m.visible = defstate); - // const mat = newMat(data); const { index, faces, norms, z } = paths; const geo = new THREE.BufferGeometry(); geo.setAttribute('position', new THREE.BufferAttribute(faces, 3)); @@ -210,7 +209,6 @@ class Stack { } if (norms) { geo.setAttribute('normal', new THREE.BufferAttribute(norms, 3)); - // geo.normalizeNormals(); } else { geo.computeVertexNormals(); } @@ -221,9 +219,8 @@ class Stack { } const mesh = new THREE.Mesh(geo, mat); mesh.position.z = z; - if (rotation) mesh.rotation.x = rotation.x; - // mesh.castShadow = true; - // mesh.receiveShadow = true; + if (rotation) mesh.rotation.set(rotation.x, rotation.y, rotation.z); + if (position) mesh.position.set(position.x, position.y, position.z); meshes.push(mesh); group.add(mesh); mats.appendAll(mat);