I am facing an issue with animating the ThreeJS camera rotation using TweenJS. While I can successfully tween the camera position, the camera.rotation does not seem to update as expected.
To illustrate my problem, I have created a simple example based on the code provided in the official ThreeJS documentation:
The core of the issue is demonstrated by the following:
new TWEEN.Tween(camera.position).to({x: newPos.x, y: newPos.y, z: newPos.z}).start()
works flawlessly, whereas this:
new TWEEN.Tween(camera.rotation).to({x: newRot.x, y: newRot.y, z: newRot.z}).start()
fails to produce any change in the camera rotation.
My understanding is that the code should be fairly easy to interpret:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
TWEEN.update(); // NOTE: I added this too
};
animate();
// NOTE: I added this:
// In a few seconds, tween the camera rotation
setTimeout(() => {
const newRot = {x: 1, y: 1, z: 1};
console.log('Changing camera rotation from:');
console.log(camera.rotation);
console.log('to:');
console.log(newRot);
new TWEEN.Tween(camera.rotation).to({x: newRot.x, y: newRot.y, z: newRot.z}).start().onComplete(() => {
// Note that it doesn't change at all?
console.log('Camera rotation changed:');
console.log(camera.rotation);
// Manually setting the rotation works fine??
console.log('Manually changing camera rotation:');
camera.rotation.x = newRot.x;
camera.rotation.y = newRot.y;
camera.rotation.z = newRot.z;
});
// Tweening the position is no problem??
const newPos = {x: 0, y: 0, z: 2};
new TWEEN.Tween(camera.position).to({x: newPos.x, y: newPos.y, z: newPos.z}).start()
}, 5000);
body { margin: 0; }
canvas { width: 100%; height: 100% }
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>
<script src="https://threejs.org/build/three.js"></script>