potree/src/utils/Volume.js

143 lines
4.4 KiB
JavaScript

Potree.Volume = class{
constructor(args = {}){
this.sceneNode = new THREE.Object3D();
this.dispatcher = new THREE.EventDispatcher();
this._clip = args.clip || false;
this._modifiable = args.modifiable || true;
let boxGeometry = new THREE.BoxGeometry(1, 1, 1);
boxGeometry.computeBoundingBox();
let boxFrameGeometry = new THREE.Geometry();
{
// bottom
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
// top
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
// sides
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
}
this.dimension = new THREE.Vector3(1,1,1);
this.material = new THREE.MeshBasicMaterial( {
color: 0x00ff00,
transparent: true,
opacity: 0.3,
depthTest: true,
depthWrite: true} );
this.box = new THREE.Mesh(boxGeometry, this.material);
this.box.geometry.computeBoundingBox();
this.boundingBox = this.box.geometry.boundingBox;
this.sceneNode.add(this.box);
this.frame = new THREE.LineSegments( boxFrameGeometry, new THREE.LineBasicMaterial({color: 0x000000}));
//this.frame.mode = THREE.Lines;
this.sceneNode.add(this.frame);
this.label = new Potree.TextSprite("0");
this.label.setBorderColor({r:0, g:255, b:0, a:0.0});
this.label.setBackgroundColor({r:0, g:255, b:0, a:0.0});
this.label.material.depthTest = false;
this.label.material.depthWrite = false;
this.label.material.transparent = true;
this.label.position.y -= 0.5;
this.sceneNode.add(this.label);
this.label.updateMatrixWorld = () => {
var volumeWorldPos = new THREE.Vector3();
volumeWorldPos.setFromMatrixPosition( this.sceneNode.matrixWorld );
this.label.position.copy(volumeWorldPos);
this.label.updateMatrix();
this.label.matrixWorld.copy(this.label.matrix);
this.label.matrixWorldNeedsUpdate = false;
for ( var i = 0, l = this.label.children.length; i < l; i ++ ) {
this.label.children[ i ].updateMatrixWorld( true );
}
};
this.update();
}
setDimension(x,y,z){
this.dimension.set(x,y,z);
this.box.scale.set(x,y,z);
this.frame.scale.set(x,y,z);
}
getVolume(){
return Math.abs(this.scale.x * this.scale.y * this.scale.z);
}
update(){
this.boundingBox = this.box.geometry.boundingBox;
this.boundingSphere = this.boundingBox.getBoundingSphere();
if(this._clip){
this.box.visible = false;
this.label.visible = false;
}else{
this.box.visible = true;
this.label.visible = true;
}
};
raycast(raycaster, intersects){
let is = [];
this.box.raycast(raycaster, is);
if(is.length > 0){
var I = is[0];
intersects.push({
distance: I.distance,
object: this,
point: I.point.clone()
});
}
};
get clip(){
return this._clip;
}
set clip(value){
this._clip = value;
this.update();
}
get modifieable(){
return this._modifiable;
}
set modifieable(value){
this._modifiable = value;
this.update();
}
};