grid-apps-cmms/src/add/three.js

117 lines
3 KiB
JavaScript
Raw Normal View History

2019-01-21 10:57:11 -05:00
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
2017-04-18 22:22:29 -04:00
2017-04-18 17:09:33 -04:00
"use strict";
(function() {
// dep: ext.three
// dep: ext.three-bgu
// dep: ext.three-svg
2021-12-29 10:37:17 -05:00
gapp.register('add.three');
2022-01-10 11:53:23 -05:00
let MP = THREE.Mesh.prototype,
XP = THREE.Box3.prototype,
2017-04-18 17:09:33 -04:00
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;
};
2022-01-10 11:53:23 -05:00
// 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().multiplyScalar(0.5).add(this.min);
2022-01-10 11:53:23 -05:00
}
}
});
2017-04-18 17:09:33 -04:00
MP.getBoundingBox = function(update) {
return this.geometry.getBoundingBox(update);
};
MP.mirrorX = function() {
return this.mirror(0);
};
MP.mirrorY = function() {
return this.mirror(1);
};
MP.mirrorZ = function() {
return this.mirror(2);
};
2017-04-18 22:22:29 -04:00
// fast mirror of vertices given axis array offset
// array elements are repeating [x,y,z,x,y,z,...]
2017-04-18 17:09:33 -04:00
MP.mirror = function(start) {
2022-01-10 11:53:23 -05:00
let i,
2017-04-18 17:09:33 -04:00
geo = this.geometry,
at = geo.attributes,
pa = at.position.array,
nm = at.normal.array;
for (i = start || 0 ; i < pa.length; i += 3) {
pa[i] = -pa[i];
nm[i] = -nm[i];
}
geo.computeVertexNormals();
return this;
};
2017-04-18 22:22:29 -04:00
// return cached or refreshed (when update = true) bounding box
BP.getBoundingBox = function(update) {
2021-01-24 00:13:09 -05:00
if (update || !this.boundingBox) {
this.boundingBox = null;
this.computeBoundingBox();
}
2022-01-10 11:53:23 -05:00
let bbox = this.boundingBox;
// bbox.dim = bbox.max.clone().sub(bbox.min);
return bbox;
2017-04-18 17:09:33 -04:00
};
2022-01-10 11:53:23 -05:00
BP.moveMesh = function(x = 0, y = 0, z = 0) {
let gap = this.attributes.position,
pa = gap.array;
for (let i=0; i < pa.length; i += 3) {
pa[i ] += x;
pa[i + 1] += y;
pa[i + 2] += z;
2022-01-10 11:53:23 -05:00
}
gap.needsUpdate = true;
2017-04-18 17:09:33 -04:00
return this;
2022-01-10 11:53:23 -05:00
}
2017-04-18 17:09:33 -04:00
BP.shallowClone = function() {
let geo = new THREE.BufferGeometry();
for (let [k, v] of Object.entries(this.attributes)) {
geo.setAttribute(k, new THREE.BufferAttribute(v.array, v.itemSize));
}
return geo;
};
2017-04-18 17:09:33 -04:00
THREE.Object3D.prototype.newGroup = function() {
2022-01-10 11:53:23 -05:00
let group = new THREE.Group();
2017-04-18 17:09:33 -04:00
this.add(group);
return group;
};
THREE.Object3D.prototype.removeAll = function() {
this.children.slice().forEach(function (c) {
c.parent = undefined;
c.dispatchEvent( { type: 'removed' } );
});
this.children = [];
};
})();