xxxxxxxxxx
var canvas = document.getElementById('canvas');
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true});
var camera = new THREE.PerspectiveCamera(45, canvas.clientWidth / canvas.clientWidth, 1, 1000);
var geometry = new THREE.BoxGeometry(50, 50, 50);
var material = new THREE.MeshBasicMaterial({color: '#f00'});
var box = new THREE.Mesh(geometry, material);
scene.add(box);
// important, otherwise the camera will spin on the spot.
camera.position.z = 200;
var period = 5; // rotation time in seconds
var clock = new THREE.Clock();
var matrix = new THREE.Matrix4(); // Pre-allocate empty matrix for performance. Don't want to make one of these every frame.
render();
function render() {
requestAnimationFrame(render);
if (canvas.width !== canvas.clientWidth || canvas.height !== canvas.clientHeight) {
// This stuff in here is just for auto-resizing.
renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
// Create a generic rotation matrix that will rotate an object
// The math here just makes it rotate every 'period' seconds.
matrix.makeRotationY(clock.getDelta() * 2 * Math.PI / period);
// Apply matrix like this to rotate the camera.
camera.position.applyMatrix4(matrix);
// Make camera look at the box.
camera.lookAt(box.position);
// Render.
renderer.render(scene, camera);
}
xxxxxxxxxx
// THREE JS, I tested this with "PointerLockControls"
//IF YOU WANT OBJECT SAME Y ROTATION OF CAMERA :
const getCameraRotate = new THREE.Vector3();
camera.getWorldDirection( getCameraRotate);
getCameraRotate.y = 0;
getCameraRotate.add( object.position );
object.lookAt( getCameraRotate );
// IF YOU WANT OBJECT SAME X.Y.Z ROTATION OF CAMERA :
object.rotation.copy(camera.rotation)
// IF you want object same position to camera :
object.position.copy (new THREE.Vector3 (camera.position.x, camera.position.y, camera.position.z));
/*if your object gets the same position as the camera, you certainly wouldn't
see it, because you would be inside, try to change some value, for example
new THREE.Vector3 (camera.position.x-5, camera.position .y -5, camera.position.z)
**/
xxxxxxxxxx
t = Math.min(t + 0.01, 1);
camera.position.copy(startPosition).lerp(endPosition, t);
camera.lookAt(0, 0, 0);