clean up three adds. remove unused

This commit is contained in:
Stewart Allen 2022-01-10 11:53:23 -05:00
commit 7932b2945e
2 changed files with 32 additions and 50 deletions

View file

@ -9,7 +9,8 @@
// dep: ext.three-svg // dep: ext.three-svg
gapp.register('add.three'); gapp.register('add.three');
var MP = THREE.Mesh.prototype, let MP = THREE.Mesh.prototype,
XP = THREE.Box3.prototype,
BP = THREE.BufferGeometry.prototype; BP = THREE.BufferGeometry.prototype;
THREE.computeFaceNormal = function(vA,vB,vC) { THREE.computeFaceNormal = function(vA,vB,vC) {
@ -22,15 +23,24 @@
return cb; return cb;
}; };
// add .dim and .mid getters to Box3
Object.defineProperties(XP, {
dim: {
get: function() {
return this.max.clone().sub(this.min);
}
},
mid: {
get: function() {
return this.dim.clone().mutiplyScalar(0.5).add(this.min);
}
}
});
MP.getBoundingBox = function(update) { MP.getBoundingBox = function(update) {
return this.geometry.getBoundingBox(update); return this.geometry.getBoundingBox(update);
}; };
MP.center = function() {
this.geometry.center();
return this;
};
MP.mirrorX = function() { MP.mirrorX = function() {
return this.mirror(0); return this.mirror(0);
}; };
@ -46,7 +56,7 @@
// fast mirror of vertices given axis array offset // fast mirror of vertices given axis array offset
// array elements are repeating [x,y,z,x,y,z,...] // array elements are repeating [x,y,z,x,y,z,...]
MP.mirror = function(start) { MP.mirror = function(start) {
var i, let i,
geo = this.geometry, geo = this.geometry,
at = geo.attributes, at = geo.attributes,
pa = at.position.array, pa = at.position.array,
@ -59,57 +69,28 @@
return this; return this;
}; };
// center geometry on x,y,z coordinates (defaults to 0,0,0)
BP.center = function(x,y,z) {
var box = this.getBoundingBox(),
mid = box.dim.clone().multiplyScalar(0.5),
dif = mid.clone().add(box.min),
pos = this.attributes.position,
arr = pos.array,
maxx = Math.max(mid.x, mid.y, mid.z),
i = 0;
if (x) dif.x -= (maxx - mid.x) * x;
if (y) dif.y -= (maxx - mid.y) * y;
if (z) dif.z -= (maxx - mid.z) * z;
while (i < arr.length) {
arr[i++] -= dif.x;
arr[i++] -= dif.y;
arr[i++] -= dif.z;
}
pos.needsUpdate = true;
// force update of (obsolete) bounding box
this.getBoundingBox(true);
return this;
};
// return cached or refreshed (when update = true) bounding box // return cached or refreshed (when update = true) bounding box
BP.getBoundingBox = function(update) { BP.getBoundingBox = function(update) {
if (update || !this.boundingBox) { if (update || !this.boundingBox) {
this.boundingBox = null; this.boundingBox = null;
this.computeBoundingBox(); this.computeBoundingBox();
} }
this.boundingBox.dim = this.boundingBox.max.clone().sub(this.boundingBox.min); let bbox = this.boundingBox;
return this.boundingBox; // bbox.dim = bbox.max.clone().sub(bbox.min);
return bbox;
}; };
// uniformly scale any mesh to a max x/y/z dim of 'unit' (defaults to 1) BP.moveMesh = function(x = 0, y = 0, z = 0) {
BP.unitScale = function(unit) { let gap = this.attributes.position,
var bbox = this.getBoundingBox().clone(), pa = gap.array;
scale = (unit || 1) / Math.max( for (let i=0; i < pa.length; i += 3) {
bbox.max.x - bbox.min.x, pa[i ] -= x;
bbox.max.y - bbox.min.y, pa[i + 1] -= y;
bbox.max.z - bbox.min.z pa[i + 2] -= z;
); }
this.applyMatrix4(new THREE.Matrix4().makeScale(scale,scale,scale)); gap.needsUpdate = true;
// force update of (obsolete) bounding box
this.getBoundingBox(true);
return this; return this;
}; }
BP.fixNormals = function() {
this.computeVertexNormals();
return this;
};
BP.shallowClone = function() { BP.shallowClone = function() {
let geo = new THREE.BufferGeometry(); let geo = new THREE.BufferGeometry();
@ -120,7 +101,7 @@
}; };
THREE.Object3D.prototype.newGroup = function() { THREE.Object3D.prototype.newGroup = function() {
var group = new THREE.Group(); let group = new THREE.Group();
this.add(group); this.add(group);
return group; return group;
}; };

View file

@ -556,6 +556,7 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
/** /**
* called by center() and Group.center() * called by center() and Group.center()
* todo use new prototype.moveMesh()
*/ */
PRO.moveMesh = function(x, y, z) { PRO.moveMesh = function(x, y, z) {
let gap = this.mesh.geometry.attributes.position, let gap = this.mesh.geometry.attributes.position,