threepipe
Version:
A 3D viewer framework built on top of three.js in TypeScript with a focus on quality rendering, modularity and extensibility.
84 lines • 3.53 kB
JavaScript
import { Box2, Box3, Vector3 } from 'three';
export class Box3B extends Box3 {
constructor() {
super(...arguments);
this._vector = new Vector3();
}
expandByObject(object, precise = false, ignoreInvisible = false, ignoreObject) {
if (object.userData?.bboxVisible === false)
return this;
if (!object.visible && ignoreInvisible)
return this;
if (ignoreObject && ignoreObject(object))
return this;
// copied the whole function from three.js to pass in ignoreInvisible
// Computes the world-axis-aligned bounding box of an object (including its children),
// accounting for both the object's, and children's, world transforms
object.updateWorldMatrix(false, false);
// InstancedMesh has boundingBox = null, so it can be computed
if (object.boundingBox !== undefined) {
if (object.boundingBox === null) {
object.computeBoundingBox();
}
Box3B._box.copy(object.boundingBox);
Box3B._box.applyMatrix4(object.matrixWorld);
this.union(Box3B._box);
}
else {
const geometry = object.geometry;
if (geometry !== undefined) {
if (precise && geometry.attributes != undefined && geometry.attributes.position !== undefined) {
const position = geometry.attributes.position;
for (let i = 0, l = position.count; i < l; i++) {
this._vector.fromBufferAttribute(position, i).applyMatrix4(object.matrixWorld);
this.expandByPoint(this._vector);
}
}
else {
if (geometry.boundingBox === null)
geometry.computeBoundingBox();
Box3B._box.copy(geometry.boundingBox);
Box3B._box.applyMatrix4(object.matrixWorld);
this.union(Box3B._box);
}
}
}
const children = object.children;
for (let i = 0, l = children.length; i < l; i++) {
this.expandByObject(children[i], precise, ignoreInvisible);
}
return this;
}
expandByObjects(objects, precise = false, ignoreInvisible = false) {
for (let i = 0, l = objects.length; i < l; i++)
this.expandByObject(objects[i], precise, ignoreInvisible);
return this;
}
/**
* Get corner points.
*/
getPoints() {
return [
new Vector3(this.min.x, this.min.y, this.min.z), // 000
new Vector3(this.min.x, this.min.y, this.max.z), // 001
new Vector3(this.min.x, this.max.y, this.min.z), // 010
new Vector3(this.min.x, this.max.y, this.max.z), // 011
new Vector3(this.max.x, this.min.y, this.min.z), // 100
new Vector3(this.max.x, this.min.y, this.max.z), // 101
new Vector3(this.max.x, this.max.y, this.min.z), // 110
new Vector3(this.max.x, this.max.y, this.max.z), // 111
];
}
getScreenSpaceBounds(camera) {
const vertices = this.getPoints();
const box = new Box2();
for (const vertex of vertices) {
const vertexScreenSpace = vertex.project(camera);
box.min.min(vertexScreenSpace);
box.max.max(vertexScreenSpace);
}
return box;
}
}
Box3B._box = new Box3B();
//# sourceMappingURL=Box3B.js.map