I would like to recreate the Three.js OrbitControl movement without the click & drag, i.e. simply making the camera following mouse movement.
I tried to recreate it from scratch, but it was too much effort as the problem is that the camera moves on three axis, not just two. I'm pretty sure some has done before.
Specifically I would like the camera to move around the scene origin keeping the same distance from it.
I would like to recreate the Three.js OrbitControl movement without the click & drag, i.e. simply making the camera following mouse movement.
I tried to recreate it from scratch, but it was too much effort as the problem is that the camera moves on three axis, not just two. I'm pretty sure some has done before.
Specifically I would like the camera to move around the scene origin keeping the same distance from it.
Share Improve this question asked Sep 6, 2016 at 20:14 a.barbieria.barbieri 2,6263 gold badges32 silver badges61 bronze badges 3-
In order to solve the problem you have to understand how
OrbitControls
works: it uses a target and a camera, essentially a vector. The input toOrbitControls
makes calculations on this vector "camera -> target". E.g. a rotation would take the current angle of that vector (relative to some axis), modify that angle by the desired amount, and then move the camera to the new endpoint of the vector plus rotate it towards the target. The length of the vector stays constant, and so does your distance to the target. – Leeft Commented Sep 6, 2016 at 20:31 - I see what you mean, but how do I tell OrbitControls the new position? I've been looking in the script and I've seen no method to input the new vector. I've seen though that it does remove the mousemove event here. Should I hack/fork this script to make a new version? – a.barbieri Commented Sep 6, 2016 at 21:02
-
You'll have to make several changes to get it to do what you want such as removing the click detection, making the mousemove and touchmove events always active. Also look at the
rotateLeft
androtateUp
methods (they also handle Right and Down), these are already used to change the angle of the vector inhandleMouseMoveRotate
. – Leeft Commented Sep 6, 2016 at 21:10
1 Answer
Reset to default 6I had the same requirement as OP. This is how I solved it, with help from Leeft's ments:
Update OrbitControls.js to change scope of function
handleMouseMoveRotate
fromfunction handleMouseMoveRotate( event )
to
this.handleMouseMoveRotate = function ( event )
This is required for you to manually use this method from within your own code.
In the JS code which loads the model, use the
dispose
method to remove the default mouse controls and add your own event handler formousemove
which manually callshandleMouseMoveRotate
:init(); animate(); function init() { // Set up Camera, Scene and OrbitControls camera = new THREE.PerspectiveCamera( 45, containerWidth / containerHeight ); scene = new THREE.Scene(); controls = new THREE.OrbitControls(camera); // Remove default OrbitControls event listeners controls.dispose(); controls.update(); ... // omitted for brevity: Load model and Renderer document.addEventListener('mousemove', onDocumentMouseMove, false); } function onDocumentMouseMove( event ) { // Manually fire the event in OrbitControls controls.handleMouseMoveRotate(event); } function animate() { requestAnimationFrame( animate ); render(); } function render() { controls.update(); camera.lookAt( scene.position ); renderer.render( scene, camera ); }
NOTE: this solution removes ALL library listeners. If you are interested you can enable them again copying them from here to the end of the update method.