diff --git a/src/EarthControls.js b/src/EarthControls.js index 09f93177..f0bfc5f6 100644 --- a/src/EarthControls.js +++ b/src/EarthControls.js @@ -3,29 +3,20 @@ /** * @author mschuetz / http://mschuetz.at * - * adapted from THREE.OrbitControls by * - * @author qiao / https://github.com/qiao - * @author mrdoob / http://mrdoob.com - * @author alteredq / http://alteredqualia.com/ - * @author WestLangley / http://github.com/WestLangley - * @author erich666 / http://erichaines.com + * Navigation similar to Google Earth. * - * This set of controls performs first person navigation without mouse lock. - * Instead, rotating the camera is done by dragging with the left mouse button. - * - * move: a/s/d/w or up/down/left/right - * rotate: left mouse - * pan: right mouse - * change speed: mouse wheel + * left mouse: Drag with respect to intersection + * wheel: zoom towards/away from intersection + * right mouse: Rotate camera around intersection * * */ - - -THREE.EarthControls = function ( object, domElement ) { - this.object = object; +THREE.EarthControls = function ( camera, domElement, renderer ) { + this.camera = camera; + this.renderer = renderer; + this.pointclouds = []; this.domElement = ( domElement !== undefined ) ? domElement : document; // Set to false to disable this control @@ -51,7 +42,7 @@ THREE.EarthControls = function ( object, domElement ) { this.maxAngle = (70 / 180) * Math.PI; // 70° this.update = function (delta) { - var position = this.object.position; + var position = this.camera.position; if(pivot){ if(state === STATE.DRAG){ @@ -69,21 +60,21 @@ THREE.EarthControls = function ( object, domElement ) { var distanceToPlane = ray.distanceToPlane(plane); var newCamPos = new THREE.Vector3().subVectors(pivot, dir.clone().multiplyScalar(distanceToPlane)); - camera.position.copy(newCamPos); + this.camera.position.copy(newCamPos); }else if(state === STATE.ROTATE){ // rotate around pivot point var diff = mouseDelta.clone().multiplyScalar(delta); - camera.updateMatrixWorld(); + this.camera.updateMatrixWorld(); // do calculations on fresh nodes var p = new THREE.Object3D(); var c = new THREE.Object3D(); p.add(c); p.position.copy(pivot); - c.position.copy(camera.position).sub(pivot); - c.rotation.copy(camera.rotation); + c.position.copy(this.camera.position).sub(pivot); + c.rotation.copy(this.camera.rotation); // rotate left/right @@ -91,7 +82,7 @@ THREE.EarthControls = function ( object, domElement ) { // rotate up/down - var dir = camera.getWorldDirection(); + var dir = this.camera.getWorldDirection(); var up = new THREE.Vector3(0,1,0); var side = new THREE.Vector3().crossVectors(up, dir); @@ -114,11 +105,11 @@ THREE.EarthControls = function ( object, domElement ) { } p.rotateOnAxis(side, -amount); - // apply changes to camera + // apply changes to object p.updateMatrixWorld(); - camera.position.copy(c.getWorldPosition()); - camera.quaternion.copy(c.getWorldQuaternion()); + this.camera.position.copy(c.getWorldPosition()); + this.camera.quaternion.copy(c.getWorldQuaternion()); } } @@ -130,32 +121,31 @@ THREE.EarthControls = function ( object, domElement ) { this.reset = function () { state = STATE.NONE; - this.object.position.copy( this.position0 ); + this.camera.position.copy( this.position0 ); }; function onMouseDown( event ) { if ( scope.enabled === false ) return; event.preventDefault(); - // TODO don't use global variables !!! var accuracy = 1; var mouse = { x: ( event.clientX / scope.domElement.clientWidth ) * 2 - 1, y: - ( event.clientY / scope.domElement.clientHeight ) * 2 + 1 }; - var I = getMousePointCloudIntersection(mouse, camera, renderer, scenePointCloud, accuracy) + var I = getMousePointCloudIntersection(mouse, scope.camera, scope.renderer, scope.pointclouds, accuracy) var plane = new THREE.Plane().setFromNormalAndCoplanarPoint(new THREE.Vector3(0, 1, 0), I); var vec = new THREE.Vector3( mouse.x, mouse.y, 0.5 ); - vec.unproject(camera); - var dir = vec.sub(camera.position).normalize(); + vec.unproject(scope.camera); + var dir = vec.sub(scope.camera.position).normalize(); - var ray = new THREE.Ray(camera.position, dir); + var ray = new THREE.Ray(scope.camera.position, dir); pivot = ray.intersectPlane(plane); //pivot = I; - camStart = camera.clone(); - camStart.rotation.copy(camera.rotation); + camStart = scope.camera.clone(); + camStart.rotation.copy(scope.camera.rotation); dragStart.set( event.clientX, event.clientY ); dragEnd.set(event.clientX, event.clientY); @@ -195,22 +185,18 @@ THREE.EarthControls = function ( object, domElement ) { event.preventDefault(); - //var direction = (event.detail<0 || event.wheelDelta>0) ? 1 : -1; - //scope.moveSpeed += scope.moveSpeed * 0.1 * direction; - //scope.moveSpeed = Math.max(0.1, scope.moveSpeed); - var amount = (event.detail<0 || event.wheelDelta>0) ? 1 : -1; var accuracy = 1; var mouse = { x: ( event.clientX / scope.domElement.clientWidth ) * 2 - 1, y: - ( event.clientY / scope.domElement.clientHeight ) * 2 + 1 }; - var I = getMousePointCloudIntersection(mouse, camera, renderer, scenePointCloud, accuracy) + var I = getMousePointCloudIntersection(mouse, scope.camera, scope.renderer, scope.pointclouds, accuracy) if(I){ - var distance = I.distanceTo(camera.position); - var dir = new THREE.Vector3().subVectors(I, camera.position).normalize(); - camera.position.add(dir.multiplyScalar(distance * 0.1 * amount)); + var distance = I.distanceTo(scope.camera.position); + var dir = new THREE.Vector3().subVectors(I, scope.camera.position).normalize(); + scope.camera.position.add(dir.multiplyScalar(distance * 0.1 * amount)); } } diff --git a/src/utils.js b/src/utils.js index 50d5fc7e..1c2a53d5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -164,25 +164,20 @@ Potree.utils.createBackgroundTexture = function(width, height){ -function getMousePointCloudIntersection(mouse, camera, renderer, scene, accuracy){ +function getMousePointCloudIntersection(mouse, camera, renderer, pointclouds, accuracy){ var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ); vector.unproject(camera); var direction = vector.sub(camera.position).normalize(); var ray = new THREE.Ray(camera.position, direction); - var pointClouds = []; - 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]; + for(var i = 0; i < pointclouds.length; i++){ + var pointcloud = pointclouds[i]; var point = pointcloud.pick(renderer, camera, ray, {accuracy: accuracy}); if(!point){