823 lines
22 KiB
JavaScript
823 lines
22 KiB
JavaScript
|
|
|
|
/**
|
|
* Stands in place for invisible or unloaded octree nodes.
|
|
* If a proxy node becomes visible and its geometry has not been loaded,
|
|
* loading will begin.
|
|
* If it is visible and the geometry has been loaded, the proxy node will
|
|
* be replaced with a point cloud node (THREE.PointCloud as of now)
|
|
*/
|
|
Potree.PointCloudOctreeProxyNode = function(geometryNode){
|
|
THREE.Object3D.call( this );
|
|
|
|
this.geometryNode = geometryNode;
|
|
this.boundingBox = geometryNode.boundingBox;
|
|
this.boundingSphere = this.boundingBox.getBoundingSphere();
|
|
this.name = geometryNode.name;
|
|
this.level = geometryNode.level;
|
|
this.numPoints = geometryNode.numPoints;
|
|
}
|
|
|
|
Potree.PointCloudOctreeProxyNode.prototype = Object.create(THREE.Object3D.prototype);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Potree.ProfileRequest = function(start, end, width, depth, callback){
|
|
this.start = start;
|
|
this.end = end;
|
|
this.width = width;
|
|
this.depth = depth;
|
|
|
|
//var up = start.clone();
|
|
//up.y += 10;
|
|
//this.plane = new THREE.Plane().setFromCoplanarPoints(start, end, up);
|
|
this.callback = callback;
|
|
this.loadQueue = [];
|
|
|
|
var center = new THREE.Vector3().addVectors(end, start).multiplyScalar(0.5);
|
|
var length = new THREE.Vector3().subVectors(end, start).length();
|
|
var side = new THREE.Vector3().subVectors(end, start).normalize();
|
|
var up = new THREE.Vector3(0, 1, 0);
|
|
var forward = new THREE.Vector3().crossVectors(side, up).normalize();
|
|
var N = forward;
|
|
this.plane = new THREE.Plane().setFromNormalAndCoplanarPoint(N, start);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Potree.PointCloudOctree = function(geometry, material){
|
|
THREE.Object3D.call( this );
|
|
|
|
Potree.PointCloudOctree.lru = Potree.PointCloudOctree.lru || new LRU();
|
|
|
|
this.pcoGeometry = geometry;
|
|
//this.boundingBox = this.pcoGeometry.boundingBox;
|
|
this.boundingBox = this.pcoGeometry.root.boundingBox;
|
|
this.boundingSphere = this.boundingBox.getBoundingSphere();
|
|
this.material = material;
|
|
this.visiblePointsTarget = 2*1000*1000;
|
|
this.level = 0;
|
|
this.position.sub(geometry.offset);
|
|
this.updateMatrix();
|
|
|
|
this.LODDistance = 20;
|
|
this.LODFalloff = 1.3;
|
|
this.LOD = 4;
|
|
this.showBoundingBox = false;
|
|
this.loadQueue = [];
|
|
this.visibleBounds = new THREE.Box3();
|
|
this.profileRequests = [];
|
|
this.visibleNodes = [];
|
|
this.pickTarget;
|
|
this.pickMaterial;
|
|
|
|
var rootProxy = new Potree.PointCloudOctreeProxyNode(this.pcoGeometry.root);
|
|
this.add(rootProxy);
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype = Object.create(THREE.Object3D.prototype);
|
|
|
|
Potree.PointCloudOctree.prototype.update = function(camera){
|
|
this.numVisibleNodes = 0;
|
|
this.numVisiblePoints = 0;
|
|
this.loadQueue = [];
|
|
|
|
// create frustum in object space
|
|
camera.updateMatrixWorld();
|
|
var frustum = new THREE.Frustum();
|
|
var viewI = camera.matrixWorldInverse;
|
|
var world = this.matrixWorld;
|
|
var proj = camera.projectionMatrix;
|
|
var fm = new THREE.Matrix4().multiply(proj).multiply(viewI).multiply(world);
|
|
frustum.setFromMatrix( fm );
|
|
|
|
// calculate camera position in object space
|
|
var view = camera.matrixWorld;
|
|
var worldI = new THREE.Matrix4().getInverse(world);
|
|
var camMatrixObject = new THREE.Matrix4().multiply(worldI).multiply(view);
|
|
var camObjPos = new THREE.Vector3().setFromMatrixPosition( camMatrixObject );
|
|
|
|
var ray = new THREE.Ray(camera.position, new THREE.Vector3( 0, 0, -1 ).applyQuaternion( camera.quaternion ) );
|
|
|
|
for(var i = 0; i < this.profileRequests.length; i++){
|
|
var profileRequest = this.profileRequests[i];
|
|
profileRequest.loadQueue = [];
|
|
}
|
|
|
|
// check visibility
|
|
this.visibleNodes = [];
|
|
var outOfRange = [];
|
|
var stack = [];
|
|
stack.push(this);
|
|
while(stack.length > 0){
|
|
var object = stack.shift();
|
|
|
|
if(object instanceof THREE.Mesh || object instanceof THREE.Line ){
|
|
object.visible = true;
|
|
continue;
|
|
}
|
|
|
|
var box = object.boundingBox;
|
|
var distance = box.center().distanceTo(camObjPos);
|
|
var radius = box.size().length() * 0.5;
|
|
var weight = Math.pow(radius, 1) / distance;
|
|
|
|
var insideFrustum = frustum.intersectsBox(box);
|
|
var inRange = true;
|
|
if(object.level > 0){
|
|
var inRange = weight >= (1 / this.LOD);
|
|
|
|
if(!inRange){
|
|
outOfRange.push({node: object, lod: weight});
|
|
}
|
|
}
|
|
|
|
var visible = insideFrustum && inRange;
|
|
|
|
|
|
|
|
|
|
//if(object.level === 0){
|
|
// visible = true;
|
|
//}else if(object.level === 1){
|
|
// //visible = true;
|
|
// visible = object.name === "r2";
|
|
//}else{
|
|
// visible = false;
|
|
//}
|
|
|
|
object.visible = visible;
|
|
object.insideFrustum = insideFrustum;
|
|
object.inRange = inRange;
|
|
|
|
object.matrixWorld.multiplyMatrices( object.parent.matrixWorld, object.matrix );
|
|
|
|
|
|
if(!visible){
|
|
this.hideDescendants(object);
|
|
}else if(visible && this.showBoundingBox && object instanceof THREE.PointCloud){
|
|
if(object.boundingBoxNode === undefined && object.boundingBox !== undefined){
|
|
var boxHelper = new THREE.BoxHelper(object);
|
|
object.add(boxHelper);
|
|
object.boundingBoxNode = boxHelper;
|
|
boxHelper.matrixWorld.copy(object.matrixWorld);
|
|
}
|
|
|
|
object.boundingBoxNode.matrixWorld.copy(object.matrixWorld);
|
|
}else if(!this.showBoundingBox){
|
|
if(object.boundingBoxNode !== undefined){
|
|
object.remove(object.boundingBoxNode);
|
|
object.boundingBoxNode = undefined;
|
|
}
|
|
}
|
|
|
|
if(!visible){
|
|
continue;
|
|
}else if(object instanceof THREE.PointCloud){
|
|
this.numVisibleNodes++;
|
|
this.numVisiblePoints += object.numPoints;
|
|
Potree.PointCloudOctree.lru.touch(object);
|
|
object.material = this.material;
|
|
this.visibleNodes.push({node: object, lod: weight});
|
|
}else if (object instanceof Potree.PointCloudOctreeProxyNode) {
|
|
var geometryNode = object.geometryNode;
|
|
if(geometryNode.loaded === true){
|
|
this.replaceProxy(object);
|
|
}else{
|
|
this.loadQueue.push({node: object, lod: weight});
|
|
}
|
|
}
|
|
|
|
for(var i = 0; i < object.children.length; i++){
|
|
stack.push(object.children[i]);
|
|
}
|
|
|
|
//console.log(object.name);
|
|
}
|
|
|
|
// check profile cut plane intersections
|
|
for(var i = 0; i < this.profileRequests.length; i++){
|
|
var profileRequest = this.profileRequests[i];
|
|
var plane = profileRequest.plane;
|
|
var start = profileRequest.start;
|
|
var end = profileRequest.end;
|
|
var depth = profileRequest.depth;
|
|
|
|
var stack = [];
|
|
stack.push(this);
|
|
while(stack.length > 0){
|
|
var object = stack.shift();
|
|
|
|
if(object instanceof Potree.PointCloudOctreeProxyNode){
|
|
var box = Potree.utils.computeTransformedBoundingBox(object.boundingBox, object.matrixWorld);
|
|
|
|
var sphere = box.getBoundingSphere();
|
|
if(Math.abs(plane.distanceToPoint(sphere.center)) < sphere.radius){
|
|
profileRequest.loadQueue.push(object);
|
|
}
|
|
}
|
|
|
|
|
|
if(object.level < depth){
|
|
for(var i = 0; i < object.children.length; i++){
|
|
var child = object.children[i];
|
|
|
|
if(child instanceof Potree.PointCloudOctreeProxyNode || child instanceof THREE.PointCloud){
|
|
stack.push(object.children[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// increase LOD if few points are visible
|
|
if(this.numVisiblePoints < this.visiblePointsTarget * 0.9 && outOfRange.length > 0 && this.visibleNodes.length > 0){
|
|
// increase lod to load some of the nodes that are currently out of range
|
|
this.visibleNodes.sort(function(a,b){return a.lod - b.lod});
|
|
outOfRange.sort(function(a,b){return b.lod - a.lod});
|
|
var visibleMax = 1 / this.visibleNodes[0].lod;
|
|
var oorIndex = Math.min(outOfRange.length - 1, 5);
|
|
var outOfRangeMax = 1 / outOfRange[oorIndex].lod;
|
|
var newMax = Math.max(visibleMax, outOfRangeMax);
|
|
|
|
this.LOD = newMax;
|
|
}
|
|
|
|
// enforce visible point target limit
|
|
this.visibleNodes.sort(function(a,b){return b.lod - a.lod});
|
|
var pointsSum = 0;
|
|
for(var i = 0; i < this.visibleNodes.length; i++){
|
|
var node = this.visibleNodes[i].node;
|
|
|
|
if(pointsSum > this.visiblePointsTarget){
|
|
node.visible = false;
|
|
}else if(pointsSum + node.numPoints > this.visiblePointsTarget){
|
|
var index = Math.min(i+5, this.visibleNodes.length-1);
|
|
this.LOD = 1 / this.visibleNodes[index].lod;
|
|
this.numVisiblePoints = pointsSum;
|
|
this.numVisibleNodes = i+1;
|
|
}
|
|
|
|
pointsSum += node.numPoints;
|
|
}
|
|
|
|
var leafNodes = [];
|
|
for(var i = 0; i < this.visibleNodes.length; i++){
|
|
var element = this.visibleNodes[i];
|
|
var node = element.node;
|
|
var isLeaf = true;
|
|
|
|
for(var j = 0; j < node.children.length; j++){
|
|
var child = node.children[j];
|
|
if(child instanceof THREE.PointCloud){
|
|
isLeaf = isLeaf && !child.visible;
|
|
}
|
|
}
|
|
|
|
if(isLeaf){
|
|
leafNodes.push(node);
|
|
}
|
|
}
|
|
|
|
this.visibleBounds.min = new THREE.Vector3( Infinity, Infinity, Infinity );
|
|
this.visibleBounds.max = new THREE.Vector3( - Infinity, - Infinity, - Infinity );
|
|
for(var i = 0; i < leafNodes.length; i++){
|
|
var node = leafNodes[i];
|
|
|
|
this.visibleBounds.expandByPoint(node.boundingBox.min);
|
|
this.visibleBounds.expandByPoint(node.boundingBox.max);
|
|
//this.visibleBounds.intersect(node.boundingBox);
|
|
}
|
|
|
|
|
|
// schedule nodes needed for a profile request
|
|
var finishedRequests = [];
|
|
for(var i = 0; i < this.profileRequests.length; i++){
|
|
var request = this.profileRequests[i];
|
|
|
|
if(request.loadQueue.length > 0){
|
|
var object = request.loadQueue[0];
|
|
var geometryNode = object.geometryNode;
|
|
if(geometryNode.loaded === true && object.parent !== undefined){
|
|
var node = this.replaceProxy(object);
|
|
node.updateMatrixWorld();
|
|
node.matrixWorld.multiplyMatrices( node.parent.matrixWorld, node.matrix );
|
|
|
|
//var boxHelper = new THREE.BoxHelper(node);
|
|
//scene.add(boxHelper);
|
|
}else{
|
|
object.geometryNode.load();
|
|
}
|
|
}else{
|
|
var points = this.getProfile(request.start, request.end, request.width, request.depth);
|
|
|
|
request.callback({type: "finished", points: points});
|
|
finishedRequests.push(request);
|
|
}
|
|
|
|
//for(var j = 0; j < profileRequest.loadQueue.length; j++){
|
|
// var object = profileRequest.loadQueue[j];
|
|
// if(object.boundingBoxNode === undefined && object.boundingBox !== undefined){
|
|
// object.geometry = object.geometryNode;
|
|
// var boxHelper = new THREE.BoxHelper(object);
|
|
// object.add(boxHelper);
|
|
// object.boundingBoxNode = boxHelper;
|
|
// object.geometry = undefined;
|
|
// }
|
|
//}
|
|
}
|
|
|
|
for(var i = 0; i < finishedRequests.length; i++){
|
|
var index = this.profileRequests.indexOf(finishedRequests[i]);
|
|
if (index > -1) {
|
|
this.profileRequests.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
// schedule some of the unloaded nodes for loading
|
|
if(this.loadQueue.length > 0){
|
|
if(this.loadQueue.length >= 2){
|
|
this.loadQueue.sort(function(a,b){return b.lod - a.lod});
|
|
}
|
|
|
|
for(var i = 0; i < Math.min(5, this.loadQueue.length); i++){
|
|
this.loadQueue[i].node.geometryNode.load();
|
|
}
|
|
}
|
|
|
|
this.updateVisibilityTexture();
|
|
};
|
|
|
|
Potree.PointCloudOctree.prototype.updateVisibilityTexture = function(){
|
|
|
|
if(!this.material){
|
|
return;
|
|
}
|
|
|
|
var texture = this.material.visibleNodesTexture;
|
|
var data = texture.image.data;
|
|
|
|
var visibleNodes = [];
|
|
for(var i = 0; i < this.visibleNodes.length; i++){
|
|
visibleNodes.push(this.visibleNodes[i].node);
|
|
}
|
|
|
|
|
|
// sort by level and index, e.g. r, r0, r3, r4, r01, r07, r30, ...
|
|
var sort = function(a, b){
|
|
var na = a.name;
|
|
var nb = b.name;
|
|
if(na.length != nb.length) return na.length - nb.length;
|
|
if(na < nb) return -1;
|
|
if(na > nb) return 1;
|
|
return 0;
|
|
};
|
|
visibleNodes.sort(sort);
|
|
|
|
for(var i = 0; i < visibleNodes.length; i++){
|
|
var node = visibleNodes[i];
|
|
var children = [];
|
|
for(var j = 0; j < node.children.length; j++){
|
|
var child = node.children[j];
|
|
if(child instanceof THREE.PointCloud && child.visible){
|
|
children.push(child);
|
|
}
|
|
}
|
|
children.sort(function(a, b){
|
|
if(a.name < b.name) return -1;
|
|
if(a.name > b.name) return 1;
|
|
return 0;
|
|
});
|
|
|
|
data[63*64*3 + i*3 + 0] = 0;
|
|
data[63*64*3 + i*3 + 1] = 0;
|
|
data[63*64*3 + i*3 + 2] = 0;
|
|
for(var j = 0; j < children.length; j++){
|
|
var child = children[j];
|
|
var index = parseInt(child.name.substr(-1));
|
|
data[63*64*3 + i*3 + 0] += Math.pow(2, index);
|
|
|
|
if(j === 0){
|
|
var vArrayIndex = visibleNodes.indexOf(child);
|
|
data[63*64*3 + i*3 + 1] = vArrayIndex;
|
|
}
|
|
|
|
//ibuffer[4*i] += Math.pow(2, index);
|
|
}
|
|
|
|
//for(var j = 0; j <= node.children.length; j++){
|
|
// var child = node.children[j];
|
|
//
|
|
// if(child instanceof THREE.PointCloud && child.visible){
|
|
// var index = parseInt(child.name.substr(1,1));
|
|
// var vArrayIndex = this.visibleNodes.indexOf(child);
|
|
// ibuffer[4*i] += Math.pow(2, index);
|
|
//
|
|
// }
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
this.material.uniforms.nodeSize.value = this.boundingBox.size().x;
|
|
texture.needsUpdate = true;
|
|
|
|
//console.log(data[63*64*3] + ", " + ibuffer[0]);
|
|
|
|
|
|
|
|
//if(this.material){
|
|
// var texture = this.material.visibleNodesTexture;
|
|
// var data = texture.image.data;
|
|
//
|
|
// for(var i = 0; i < 64; i++){
|
|
// for(var j = 0; j < 64; j++){
|
|
// var k = i*64 + j;
|
|
// data[3*k] = 255-j*4;
|
|
// data[3*k+1] = 255-j*4;
|
|
// data[3*k+2] = 255-j*4;
|
|
// }
|
|
// }
|
|
//
|
|
// //data[63*64*3+0] = 100;
|
|
// //data[63*64*3+0] = 100;
|
|
// //data[63*64*3+0] = 100;
|
|
//
|
|
// data[63*64*3+0] = ibuffer[0];
|
|
//
|
|
// this.material.uniforms.nodeSize.value = this.boundingBox.size().x;
|
|
//
|
|
// texture.needsUpdate = true;
|
|
//}
|
|
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.nodesOnRay = function(nodes, ray){
|
|
var nodesOnRay = [];
|
|
|
|
var _ray = ray.clone();
|
|
for(var i = 0; i < nodes.length; i++){
|
|
var node = nodes[i].node;
|
|
var inverseWorld = new THREE.Matrix4().getInverse(node.matrixWorld);
|
|
var sphere = node.boundingSphere.clone().applyMatrix4(node.matrixWorld);
|
|
|
|
if(_ray.isIntersectionSphere(sphere)){
|
|
nodesOnRay.push(nodes[i]);
|
|
//node.visible = true;
|
|
}else{
|
|
//node.visible = false;
|
|
}
|
|
}
|
|
|
|
return nodesOnRay;
|
|
};
|
|
|
|
Potree.PointCloudOctree.prototype.updateMatrixWorld = function( force ){
|
|
//node.matrixWorld.multiplyMatrices( node.parent.matrixWorld, node.matrix );
|
|
|
|
if ( this.matrixAutoUpdate === true ) this.updateMatrix();
|
|
|
|
if ( this.matrixWorldNeedsUpdate === true || force === true ) {
|
|
|
|
if ( this.parent === undefined ) {
|
|
|
|
this.matrixWorld.copy( this.matrix );
|
|
|
|
} else {
|
|
|
|
this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
|
|
|
|
}
|
|
|
|
this.matrixWorldNeedsUpdate = false;
|
|
|
|
force = true;
|
|
|
|
}
|
|
};
|
|
|
|
|
|
Potree.PointCloudOctree.prototype.replaceProxy = function(proxy){
|
|
|
|
var geometryNode = proxy.geometryNode;
|
|
if(geometryNode.loaded === true){
|
|
var geometry = geometryNode.geometry;
|
|
var node = new THREE.PointCloud(geometry, this.material);
|
|
node.name = proxy.name;
|
|
node.level = proxy.level;
|
|
node.numPoints = proxy.numPoints;
|
|
node.boundingBox = geometry.boundingBox;
|
|
node.boundingSphere = node.boundingBox.getBoundingSphere();
|
|
node.pcoGeometry = geometryNode;
|
|
var parent = proxy.parent;
|
|
parent.remove(proxy);
|
|
parent.add(node);
|
|
|
|
node.matrixWorld.multiplyMatrices( node.parent.matrixWorld, node.matrix );
|
|
|
|
for(var i = 0; i < 8; i++){
|
|
if(geometryNode.children[i] !== undefined){
|
|
var child = geometryNode.children[i];
|
|
var childProxy = new Potree.PointCloudOctreeProxyNode(child);
|
|
node.add(childProxy);
|
|
}
|
|
}
|
|
|
|
return node;
|
|
}
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.hideDescendants = function(object){
|
|
var stack = [];
|
|
for(var i = 0; i < object.children.length; i++){
|
|
var child = object.children[i];
|
|
if(child.visible){
|
|
stack.push(child);
|
|
}
|
|
}
|
|
|
|
while(stack.length > 0){
|
|
var object = stack.shift();
|
|
|
|
object.visible = false;
|
|
|
|
for(var i = 0; i < object.children.length; i++){
|
|
var child = object.children[i];
|
|
if(child.visible){
|
|
stack.push(child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.moveToOrigin = function(){
|
|
this.position.set(0,0,0);
|
|
this.updateMatrixWorld();
|
|
var box = this.boundingBox;
|
|
var transform = this.matrixWorld;
|
|
var tBox = Potree.utils.computeTransformedBoundingBox(box, transform);
|
|
this.position.set(0,0,0).sub(tBox.center());
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.moveToGroundPlane = function(){
|
|
this.updateMatrixWorld();
|
|
var box = this.boundingBox;
|
|
var transform = this.matrixWorld;
|
|
var tBox = Potree.utils.computeTransformedBoundingBox(box, transform);
|
|
this.position.y += -tBox.min.y;
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.getBoundingBoxWorld = function(){
|
|
this.updateMatrixWorld();
|
|
var box = this.boundingBox;
|
|
var transform = this.matrixWorld;
|
|
var tBox = Potree.utils.computeTransformedBoundingBox(box, transform);
|
|
|
|
return tBox;
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.getProfile = function(start, end, width, depth, callback){
|
|
if(callback !== undefined){
|
|
this.profileRequests.push(new Potree.ProfileRequest(start, end, width, depth, callback));
|
|
}else{
|
|
var stack = [];
|
|
stack.push(this);
|
|
|
|
var center = new THREE.Vector3().addVectors(end, start).multiplyScalar(0.5);
|
|
var length = new THREE.Vector3().subVectors(end, start).length();
|
|
var side = new THREE.Vector3().subVectors(end, start).normalize();
|
|
var up = new THREE.Vector3(0, 1, 0);
|
|
var forward = new THREE.Vector3().crossVectors(side, up).normalize();
|
|
var N = forward;
|
|
var cutPlane = new THREE.Plane().setFromNormalAndCoplanarPoint(N, start);
|
|
var halfPlane = new THREE.Plane().setFromNormalAndCoplanarPoint(side, center);
|
|
|
|
var inside = [];
|
|
|
|
|
|
while(stack.length > 0){
|
|
var object = stack.shift();
|
|
|
|
|
|
var pointsFound = 0;
|
|
|
|
if(object instanceof THREE.PointCloud){
|
|
var geometry = object.geometry;
|
|
var positions = geometry.attributes.position;
|
|
var p = positions.array;
|
|
var pointCount = positions.length / positions.itemSize;
|
|
|
|
for(var i = 0; i < pointCount; i++){
|
|
var pos = new THREE.Vector3(p[3*i], p[3*i+1], p[3*i+2]);
|
|
pos.applyMatrix4(this.matrixWorld);
|
|
var distance = Math.abs(cutPlane.distanceToPoint(pos));
|
|
var centerDistance = Math.abs(halfPlane.distanceToPoint(pos));
|
|
|
|
if(distance < width / 2 && centerDistance < length / 2){
|
|
inside.push(pos);
|
|
pointsFound++;
|
|
}
|
|
}
|
|
}
|
|
|
|
//console.log("traversing: " + object.name + ", #points found: " + pointsFound);
|
|
|
|
if(object == this || object.level < depth){
|
|
for(var i = 0; i < object.children.length; i++){
|
|
var child = object.children[i];
|
|
if(child instanceof THREE.PointCloud){
|
|
var sphere = child.boundingSphere.clone().applyMatrix4(child.matrixWorld);
|
|
if(cutPlane.distanceToSphere(sphere) < sphere.radius){
|
|
stack.push(child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return inside;
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* amount: minimum number of points to remove
|
|
*/
|
|
Potree.PointCloudOctree.disposeLeastRecentlyUsed = function(amount){
|
|
|
|
|
|
var freed = 0;
|
|
do{
|
|
var node = this.lru.first.node;
|
|
var parent = node.parent;
|
|
var geometry = node.geometry;
|
|
var pcoGeometry = node.pcoGeometry;
|
|
var proxy = new Potree.PointCloudOctreeProxyNode(pcoGeometry);
|
|
|
|
var result = Potree.PointCloudOctree.disposeNode(node);
|
|
freed += result.freed;
|
|
|
|
parent.add(proxy);
|
|
|
|
if(result.numDeletedNodes == 0){
|
|
break;
|
|
}
|
|
}while(freed < amount);
|
|
}
|
|
|
|
Potree.PointCloudOctree.disposeNode = function(node){
|
|
|
|
var freed = 0;
|
|
var numDeletedNodes = 0;
|
|
var descendants = [];
|
|
|
|
node.traverse(function(object){
|
|
descendants.push(object);
|
|
});
|
|
|
|
for(var i = 0; i < descendants.length; i++){
|
|
var descendant = descendants[i];
|
|
if(descendant instanceof THREE.PointCloud){
|
|
freed += descendant.pcoGeometry.numPoints;
|
|
descendant.pcoGeometry.dispose();
|
|
descendant.geometry.dispose();
|
|
Potree.PointCloudOctree.lru.remove(descendant);
|
|
numDeletedNodes++;
|
|
}
|
|
}
|
|
|
|
Potree.PointCloudOctree.lru.remove(node);
|
|
node.parent.remove(node);
|
|
|
|
return {
|
|
"freed": freed,
|
|
"numDeletedNodes": numDeletedNodes
|
|
};
|
|
}
|
|
|
|
Potree.PointCloudOctree.prototype.getVisibleExtent = function(){
|
|
return this.visibleBounds.applyMatrix4(this.matrixWorld);
|
|
};
|
|
|
|
var point = Potree.PointCloudOctree.prototype.pick = function(renderer, camera, ray, params){
|
|
var params = params || {};
|
|
var accuracy = params.accuracy || 0.5;
|
|
|
|
var nodes = this.nodesOnRay(this.visibleNodes, ray);
|
|
|
|
if(nodes.length === 0){
|
|
return null;
|
|
}
|
|
|
|
var width = Math.ceil(renderer.domElement.clientWidth * accuracy);
|
|
var height = Math.ceil(renderer.domElement.clientHeight * accuracy);
|
|
|
|
if(!this.pickTarget){
|
|
this.pickTarget = new THREE.WebGLRenderTarget(
|
|
1, 1,
|
|
{ minFilter: THREE.LinearFilter,
|
|
magFilter: THREE.NearestFilter,
|
|
format: THREE.RGBAFormat }
|
|
);
|
|
}
|
|
|
|
if(!this.pickMaterial){
|
|
this.pickMaterial = new Potree.PointCloudIndexMaterial({ size: 0.01, minSize: 3});
|
|
}
|
|
|
|
this.pickTarget.setSize(width, height);
|
|
|
|
this.pickMaterial.size = accuracy * (this.material.size || 0.01);
|
|
this.pickMaterial.minSize = accuracy * (this.material.minSize || 1);
|
|
|
|
var _gl = renderer.context;
|
|
|
|
var material = this.pickMaterial;
|
|
|
|
renderer.setRenderTarget( this.pickTarget );
|
|
|
|
renderer.setDepthTest( material.depthTest );
|
|
renderer.setDepthWrite( material.depthWrite )
|
|
renderer.setBlending( THREE.NoBlending );
|
|
|
|
renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
|
|
|
|
for(var i = 0; i < nodes.length; i++){
|
|
var object = nodes[i].node;
|
|
var geometry = object.geometry;
|
|
|
|
if(!geometry.attributes.indices.buffer){
|
|
continue;
|
|
}
|
|
|
|
material.pcIndex = i;
|
|
|
|
if(material.program){
|
|
var program = material.program.program;
|
|
_gl.useProgram( program );
|
|
//_gl.disable( _gl.BLEND );
|
|
|
|
var attributePointer = _gl.getAttribLocation(program, "indices");
|
|
var attributeSize = 4;
|
|
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.attributes.indices.buffer );
|
|
//if(!bufferSubmitted){
|
|
// _gl.bufferData( _gl.ARRAY_BUFFER, new Uint8Array(geometry.attributes.indices.array), _gl.STATIC_DRAW );
|
|
// bufferSubmitted = true;
|
|
//}
|
|
_gl.enableVertexAttribArray( attributePointer );
|
|
_gl.vertexAttribPointer( attributePointer, attributeSize, _gl.UNSIGNED_BYTE, true, 0, 0 );
|
|
|
|
_gl.uniform1f(material.program.uniforms.pcIndex, material.pcIndex);
|
|
}
|
|
|
|
renderer.renderBufferDirect(camera, [], null, material, geometry, object);
|
|
}
|
|
|
|
|
|
//_gl.bindTexture( _gl.TEXTURE_2D, this.pickTarget.__webglTexture );
|
|
//_gl.generateMipmap( _gl.TEXTURE_2D );
|
|
//_gl.bindTexture( _gl.TEXTURE_2D, null );
|
|
|
|
|
|
var ps = new THREE.Vector3().addVectors(camera.position, ray.direction).project(camera);
|
|
ps.addScalar(1).multiplyScalar(0.5);
|
|
|
|
var buffer = new ArrayBuffer(4);
|
|
var pixels = new Uint8Array(buffer);
|
|
var ibuffer = new Uint32Array(buffer);
|
|
renderer.context.readPixels(
|
|
ps.x * width, ps.y * height,
|
|
1, 1,
|
|
renderer.context.RGBA, renderer.context.UNSIGNED_BYTE, pixels);
|
|
var pcIndex = pixels[3];
|
|
pixels[3] = 0;
|
|
var pIndex = ibuffer[0];
|
|
|
|
var pc = nodes[pcIndex].node;
|
|
var positionArray = pc.geometry.attributes.position.array;
|
|
var x = positionArray[3*pIndex+0];
|
|
var y = positionArray[3*pIndex+1];
|
|
var z = positionArray[3*pIndex+2];
|
|
var position = new THREE.Vector3(x, y, z);
|
|
position.applyMatrix4(this.matrixWorld);
|
|
|
|
if(pIndex === 0 && pcIndex === 0){
|
|
return null;
|
|
}
|
|
|
|
return {position: position};
|
|
} |