From 6aa78d2a6aaaf105ce51d87edb78738b60a8176a Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Mon, 1 Mar 2021 22:41:29 -0500 Subject: [PATCH] major changes to upgrade to 3JS 1.2.5 -- deprecated THREE.Geometry -- breakage may ensue --- app.js | 1 + package.json | 2 +- src/add/three.js | 36 +++++++++++++++--------- src/kiri/main.js | 63 ++++++++++++------------------------------ src/kiri/widget.js | 19 ++++++++++++- src/mode/fdm/client.js | 16 +++-------- src/mode/fdm/slice.js | 16 +++++++---- src/moto/space.js | 15 +++++----- 8 files changed, 83 insertions(+), 85 deletions(-) diff --git a/app.js b/app.js index c0a26730..9e877619 100644 --- a/app.js +++ b/app.js @@ -241,6 +241,7 @@ const script = { kiri : [ "kiri", "ext/three", + "ext/three-bgu", "license", "ext/clip2", // work.test "ext/tween", diff --git a/package.json b/package.json index 5474089a..72c1d131 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "moment": "^2.24.0", "pixi.js": "^4.8.9", "serve-static": "^1.14.1", - "three": "^0.122.0", + "three": "^0.125.0", "tween.js": "^0.14.0", "uglify-es": "3.3.9", "validator": "^8.2.0", diff --git a/src/add/three.js b/src/add/three.js index 4baccf91..97567201 100644 --- a/src/add/three.js +++ b/src/add/three.js @@ -5,9 +5,18 @@ (function() { var MP = THREE.Mesh.prototype, - GP = THREE.Geometry.prototype, BP = THREE.BufferGeometry.prototype; + THREE.computeFaceNormal = function(vA,vB,vC) { + const ab = new THREE.Vector3(); + const cb = new THREE.Vector3(); + cb.subVectors( vC, vB ); + ab.subVectors( vA, vB ); + cb.cross( ab ); + cb.normalize(); + return cb; + }; + MP.getBoundingBox = function(update) { return this.geometry.getBoundingBox(update); }; @@ -46,7 +55,7 @@ }; // center geometry on x,y,z coordinates (defaults to 0,0,0) - GP.center = function(x,y,z) { + BP.center = function(x,y,z) { var box = this.getBoundingBox(), mid = box.dim.clone().multiplyScalar(0.5), dif = mid.clone().add(box.min), @@ -69,7 +78,7 @@ }; // return cached or refreshed (when update = true) bounding box - GP.getBoundingBox = function(update) { + BP.getBoundingBox = function(update) { if (update || !this.boundingBox) { this.boundingBox = null; this.computeBoundingBox(); @@ -79,7 +88,7 @@ }; // uniformly scale any mesh to a max x/y/z dim of 'unit' (defaults to 1) - GP.unitScale = function(unit) { + BP.unitScale = function(unit) { var bbox = this.getBoundingBox().clone(), scale = (unit || 1) / Math.max( bbox.max.x - bbox.min.x, @@ -92,20 +101,21 @@ return this; }; - BP.center = GP.center; - BP.getBoundingBox = GP.getBoundingBox; - BP.unitScale = GP.unitScale; - BP.fixNormals = function() { this.computeVertexNormals(); return this; }; - THREE.Geometry.fromVertices = function(vertices) { - var geometry = new THREE.BufferGeometry(); - geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - return geometry; - }; + // may be faster than the equivalent matrix transform. to be tested +// BP.translate = function(x,y,z) { +// let off = [x,y,z], +// arr = this.attributes.position.array, +// len = arr.length; +// for (let i=0; i { let mesh = widget.mesh; let geo = mesh.geometry; - // let geo = new THREE.Geometry().fromBufferGeometry(mesh.geometry); outs.push({geo, widget}); facets += geo.attributes.position.count; - // facets += geo.faces.length; }); let stl = new Uint8Array(80 + 4 + facets * 50); let dat = new DataView(stl.buffer); let pos = 84; dat.setInt32(80, facets, true); - // outs.forEach(out => { - // let { faces, vertices } = out.geo; - // for (let i=0, il=faces.length; i 1) { - // let {x, y, z} = out.widget.track.pos; - // xo = x; - // yo = y; - // zo = z; - // } - // dat.setFloat32(pos + 0, normal.x, true); - // dat.setFloat32(pos + 4, normal.y, true); - // dat.setFloat32(pos + 8, normal.z, true); - // dat.setFloat32(pos + 12, vertices[a].x + xo, true); - // dat.setFloat32(pos + 16, vertices[a].y + yo, true); - // dat.setFloat32(pos + 20, vertices[a].z + zo, true); - // dat.setFloat32(pos + 24, vertices[b].x + xo, true); - // dat.setFloat32(pos + 28, vertices[b].y + yo, true); - // dat.setFloat32(pos + 32, vertices[b].z + zo, true); - // dat.setFloat32(pos + 36, vertices[c].x + xo, true); - // dat.setFloat32(pos + 40, vertices[c].y + yo, true); - // dat.setFloat32(pos + 44, vertices[c].z + zo, true); - // pos += 50; - // } - // }); for (let out of outs) { - let { normal, position } = out.geo.attributes; - let nvals = normal.array; + let { position } = out.geo.attributes; let pvals = position.array; for (let i=0, il=position.count; i 1) { let {x, y, z} = out.widget.track.pos; @@ -1556,18 +1527,18 @@ yo = y; zo = z; } - dat.setFloat32(pos + 0, nx, true); - dat.setFloat32(pos + 4, ny, true); - dat.setFloat32(pos + 8, nz, true); - dat.setFloat32(pos + 12, pvals[pi + 0] + xo, true); - dat.setFloat32(pos + 16, pvals[pi + 1] + yo, true); - dat.setFloat32(pos + 20, pvals[pi + 2] + zo, true); - dat.setFloat32(pos + 24, pvals[pi + 3] + xo, true); - dat.setFloat32(pos + 28, pvals[pi + 4] + yo, true); - dat.setFloat32(pos + 32, pvals[pi + 5] + zo, true); - dat.setFloat32(pos + 36, pvals[pi + 6] + xo, true); - dat.setFloat32(pos + 40, pvals[pi + 7] + yo, true); - dat.setFloat32(pos + 44, pvals[pi + 8] + zo, true); + dat.setFloat32(pos + 0, norm.x, true); + dat.setFloat32(pos + 4, norm.y, true); + dat.setFloat32(pos + 8, norm.z, true); + dat.setFloat32(pos + 12, p0.x + xo, true); + dat.setFloat32(pos + 16, p0.y + yo, true); + dat.setFloat32(pos + 20, p0.z + zo, true); + dat.setFloat32(pos + 24, p1.x + xo, true); + dat.setFloat32(pos + 28, p1.y + yo, true); + dat.setFloat32(pos + 32, p1.z + zo, true); + dat.setFloat32(pos + 36, p2.x + xo, true); + dat.setFloat32(pos + 40, p2.y + yo, true); + dat.setFloat32(pos + 44, p2.z + zo, true); pos += 50; } } diff --git a/src/kiri/widget.js b/src/kiri/widget.js index b99137e4..0eb4a720 100644 --- a/src/kiri/widget.js +++ b/src/kiri/widget.js @@ -557,7 +557,24 @@ }; PRO.getGeoVertices = function() { - return this.mesh.geometry.getAttribute('position').array; + let geo = this.mesh.geometry; + let pos = geo.getAttribute('position').array; + if (geo.index) { + let idx = geo.index.array; + let len = idx.length; + let p2 = new Float32Array(len * 3); + let inc = 0; + for (let i=0; i w.sups).map(w => Object.values(w.sups)).flat(); + if (!merge.length) { continue; } - let bbg = new THREE.BufferGeometry().fromGeometry(merged); + let boxen = merge.map(m => m.box.geometry.clone().translate(m.x, m.y, m.z)); + let bbg = THREE.BufferGeometryUtils.mergeBufferGeometries(boxen); let sw = kiri.newWidget(null, group); let fwp = group[0].track.pos; sw.loadGeometry(bbg); diff --git a/src/mode/fdm/slice.js b/src/mode/fdm/slice.js index c5ae80f9..b0a68eee 100644 --- a/src/mode/fdm/slice.js +++ b/src/mode/fdm/slice.js @@ -1323,19 +1323,25 @@ point.added = true; } } - let filter = isBelt ? (f) => { - return f.normal.z < thresh && f.normal.y < -0.001; - } : (f) => { - return f.normal.z < thresh; + let filter = isBelt ? (norm) => { + return norm.z < thresh && norm.y < -0.001; + } : (norm) => { + return norm.z < thresh; }; - let position = geo.attributes.position; + let { position } = geo.attributes; let { itemSize, count, array } = position; for (let i = 0; i