major changes to upgrade to 3JS 1.2.5 -- deprecated THREE.Geometry -- breakage may ensue

This commit is contained in:
Stewart Allen 2021-03-01 22:41:29 -05:00
commit 6aa78d2a6a
8 changed files with 83 additions and 85 deletions

1
app.js
View file

@ -241,6 +241,7 @@ const script = {
kiri : [
"kiri",
"ext/three",
"ext/three-bgu",
"license",
"ext/clip2", // work.test
"ext/tween",

View file

@ -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",

View file

@ -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<len; i++) {
// arr[i] += off[i%3];
// }
// return this;
// }
THREE.Object3D.prototype.newGroup = function() {
var group = new THREE.Group();

View file

@ -1504,51 +1504,22 @@
widgets.forEach(widget => {
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<il; i++) {
// let {a, b, c, normal} = faces[i];
// let xo = 0, yo = 0, zo = 0;
// if (outs.length > 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<il; i += 3) {
let pi = i * position.itemSize;
// average vertex normals to produce a face normal
let nx = (nvals[pi + 0] + nvals[pi + 3] + nvals[pi + 6]) / 3;
let ny = (nvals[pi + 1] + nvals[pi + 4] + nvals[pi + 7]) / 3;
let nz = (nvals[pi + 2] + nvals[pi + 5] + nvals[pi + 8]) / 3;
let p0 = new THREE.Vector3(pvals[pi++], pvals[pi++], pvals[pi++]);
let p1 = new THREE.Vector3(pvals[pi++], pvals[pi++], pvals[pi++]);
let p2 = new THREE.Vector3(pvals[pi++], pvals[pi++], pvals[pi++]);
let norm = THREE.computeFaceNormal(p0, p1, p2);
let xo = 0, yo = 0, zo = 0;
if (outs.length > 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;
}
}

View file

@ -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<len; i++) {
let iv = idx[i];
let ip = iv * 3;
p2[inc++] = pos[ip++];
p2[inc++] = pos[ip++];
p2[inc++] = pos[ip];
}
return p2;
} else {
return pos;
}
};
PRO.getPoints = function() {

View file

@ -207,20 +207,12 @@
// synth support widget for each widget group
let synth = [];
for (let group of kiri.Widget.Groups.list()) {
let count = 0;
let merged = new THREE.Geometry();
for (let widget of group) {
if (widget.sups) {
for (let sup of Object.values(widget.sups)) {
merged.merge(sup.box.geometry, sup.box.matrix);
count++;
}
}
}
if (!count) {
let merge = group.filter(w => 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);

View file

@ -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<count; i += 3) {
let ip = i * itemSize;
let a = new THREE.Vector3(array[ip++], array[ip++], array[ip++]);
let b = new THREE.Vector3(array[ip++], array[ip++], array[ip++]);
let c = new THREE.Vector3(array[ip++], array[ip++], array[ip++]);
let norm = THREE.computeFaceNormal(a,b,c);
// limit to downward faces
if (!filter(norm)) {
continue;
}
// skip tiny faces
let area = BASE.newPolygon().addPoints([a,b,c]).area();
if (BASE.newPolygon().addPoints([a,b,c]).area() < min) {
continue;
}

View file

@ -682,14 +682,15 @@
if (points.length % 2 != 0) {
throw "invalid line : "+points.length;
}
const geo = new THREE.Geometry();
for (let i=0; i < points.length; ) {
const p1 = points[i++];
const p2 = points[i++];
geo.vertices.push(new THREE.Vector3(p1.x, p1.y, p1.z));
geo.vertices.push(new THREE.Vector3(p2.x, p2.y, p2.z));
const geo = new THREE.BufferGeometry();
const vrt = new Float32Array(points.length * 3);
let vi = 0;
for (let p of points) {
vrt[vi++] = p.x;
vrt[vi++] = p.y;
vrt[vi++] = p.z;
}
geo.verticesNeedUpdate = true;
geo.setAttribute('position', new THREE.BufferAttribute(vrt, 3));
return new THREE.LineSegments(geo, new THREE.LineBasicMaterial({
color: color,
opacity: opacity || 1,