To achieve this effect, I implemented a quick and simple coding method -
Within the OrbitControls.js
file, insert the following function within the main declaration (or any suitable location) -
this.inertiaFunction = function()
{
scope.rotateLeft( ( 2 * Math.PI * rotateDelta.x / PIXELS_PER_ROUND * scope.userRotateSpeed )/dividingFactor);
scope.rotateUp( ( 2 * Math.PI * rotateDelta.y / PIXELS_PER_ROUND * scope.userRotateSpeed )/dividingFactor);
dividingFactor+=0.5;
}
Incorporate the following line at the beginning of the onMouseDown(event)
function -
dividingFactor = 1;
(to reset the factor each time the mouse is clicked)
At the start of the onMouseUp(event)
function, include these lines -
dragging2=false;
timer = setTimeout(function(){dragging=false;}, 500);
dragging
and dragging2
serve as flags in the requestAnimFrame function to determine if the mouse has been lifted and if 500 milliseconds have not yet passed.
Add the following snippet within your main animate() or requestAnimationFrame() function -
if(dragging && !dragging2){ controls.inertiaFunction(); }
This condition ensures that if the mouse has been lifted and 500ms have not elapsed, the inertiaFunction() of the controls object (which is an instance of THREE.OrbitControls
) will be called.
To handle cases where the user clicks within 500ms of lifting the mouse, the setTimeout is canceled using the timer
object.
In your onMouseDown function, add the following line -
if(dragging)
{
clearTimeout(timer);
}
Remember to declare dragging
, timer
, dragging2
, and dividingFactor
as global variables. Experiment with the dividingFactor
and the 500ms in the setTimeout()
to adjust the distance traveled and duration of the inertial motion.