Potree.VolumeTool = function(scene, camera, renderer){ var scope = this; this.enabled = false; this.scene = scene; this.camera = camera; this.renderer = renderer; this.domElement = renderer.domElement; this.mouse = {x: 0, y: 0}; this.accuracy = 0.5; this.volumes = []; var STATE = { DEFAULT: 0, INSERT_VOLUME: 1 }; var state = STATE.DEFAULT; var material = new THREE.MeshBasicMaterial( {color: 0x00ff00, transparent: true, opacity: 0.3} ); var boxGeometry = new THREE.BoxGeometry(1, 1, 1); boxGeometry.computeBoundingBox(); var 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)); function onMouseMove(event){ scope.mouse.x = ( event.clientX / scope.domElement.clientWidth ) * 2 - 1; scope.mouse.y = - ( event.clientY / scope.domElement.clientHeight ) * 2 + 1; }; function onMouseClick(event){ if(state === STATE.INSERT_VOLUME){ scope.finishInsertion(); }else if(event.which === 1){ var I = getHoveredElement(); if(I){ transformationTool.setTargets([I.object]); } } }; function getHoveredElement(){ var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 ); vector.unproject(scope.camera); var raycaster = new THREE.Raycaster(); raycaster.ray.set( scope.camera.position, vector.sub( scope.camera.position ).normalize() ); var objects = []; for(var i = 0; i < scope.volumes.length; i++){ var object = scope.volumes[i].object; objects.push(object); } var intersections = raycaster.intersectObjects(objects, false); if(intersections.length > 0){ return intersections[0]; }else{ return false; } }; function getMousePointCloudIntersection(){ var vector = new THREE.Vector3( scope.mouse.x, scope.mouse.y, 0.5 ); vector.unproject(scope.camera); var direction = vector.sub(scope.camera.position).normalize(); var ray = new THREE.Ray(scope.camera.position, direction); var pointClouds = []; scope.scene.traverse(function(object){ if(object instanceof Potree.PointCloudOctree){ pointClouds.push(object); } }); var closestPoint = null; var closestPointDistance = null; for(var i = 0; i < pointClouds.length; i++){ var pointcloud = pointClouds[i]; var point = pointcloud.pick(scope.renderer, scope.camera, ray, {accuracy: scope.accuracy}); if(!point){ continue; } var distance = scope.camera.position.distanceTo(point.position); if(!closestPoint || distance < closestPointDistance){ closestPoint = point; closestPointDistance = distance; } } return closestPoint ? closestPoint.position : null; } this.update = function(delta){ if(state === STATE.INSERT_VOLUME){ var I = getMousePointCloudIntersection(); if(I){ var object = this.activeVolume.object; //var label = this.activeVolume.label; object.position.copy(I); var wp = object.getWorldPosition().applyMatrix4(this.camera.matrixWorldInverse); var pp = new THREE.Vector4(wp.x, wp.y, wp.z).applyMatrix4(this.camera.projectionMatrix); var w = Math.abs((wp.z / 10)); object.scale.set(w, w, w); //label.scale.set(w,w,w); //box.scale.set(1,1,1); } } var volumes = []; for(var i = 0; i < this.volumes.length; i++){ volumes.push(this.volumes[i]); } if(this.activeVolume){ volumes.push(this.activeVolume); } for(var i = 0; i < volumes.length; i++){ var box = volumes[i].object; var label = volumes[i].label; var volume = box.scale.x * box.scale.y * box.scale.z; var msg = Potree.utils.addCommas(volume.toFixed(1)) + "³"; var wp = label.getWorldPosition().applyMatrix4(this.camera.matrixWorldInverse); var pp = new THREE.Vector4(wp.x, wp.y, wp.z).applyMatrix4(this.camera.projectionMatrix); var w = Math.abs(wp.z / 5); label.setText(msg); var l = label.scale.length(); label.scale.multiplyScalar(w / l); } }; this.startInsertion = function(){ state = STATE.INSERT_VOLUME; var box = new THREE.Mesh( boxGeometry, material); this.scene.add(box); var label = new Potree.TextSprite("0"); label.setBorderColor({r:0, g:255, b:0, a:0.0}); label.setBackgroundColor({r:0, g:255, b:0, a:0.0}); label.material.depthTest = false; label.position.y -= 0.5; //label.material.opacity = 0; //label.position.copy(I); box.add(label); var line = new THREE.Line( boxFrameGeometry, new THREE.LineBasicMaterial({color: 0x000000})); line.mode = THREE.LinePieces; box.add(line); this.activeVolume = { object: box, label: label }; } this.finishInsertion = function(){ this.volumes.push(this.activeVolume); transformationTool.setTargets([this.activeVolume.object]); this.activeVolume = null; state = STATE.DEFAULT; } this.domElement.addEventListener( 'click', onMouseClick, false ); this.domElement.addEventListener( 'mousemove', onMouseMove, false ); };