Ive pretty much got this working but having an issue when adding multiple objects, currently I’m using a gsap animation to zoom in on the object when you click it, and another gsap animation to zoom back to the original camera settings when you click anything except the object. When adding more than one clickable object this doesnt work because if you are already zoomed in on an object, if you click another one it triggers the animation to zoom back to the original camera settings.
Is there a better way to zoom in on an object and then revert back to the original camera settings when you click outside of the object, which would also allow you to click another object when already zoomed in, and the camera just moves across to the next object without first zooming out?
//Zoom
const zoomInTimeline = (x, y, z, zoomOutFactor = 0) => {
let tl = gsap
.timeline({ defaults: { duration: 1.5, ease: "expo.out", onUpdate: function() {controls.enabled = false;}} })
.to(controls.target, { x, y, z})
.to(camera.position, { x:4, y:2, z: 2 },0)
// .to(group.rotation, { x: 0, y: 0 }, 0);
};
//Raycaster
var mesh=[];
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2()
function onClick(event) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children, true );
//Click Tree006 to focus
if ( intersects.length > 0 && intersects[0].object.name==="Tree006") {
var object = intersects[0].object;
zoomInTimeline(mesh["Tree006"].position.x, mesh["Tree006"].position.y, mesh["Tree006"].position.z, 5);
console.log( 'Intersection:', intersects[ 0 ] );
}
//Click anywhere outside of object to leave focus
if ( intersects.length > 0 && intersects[0].object.name!=="Tree006") {
var object = intersects[0].object;
gsap.to( camera.position, {
duration: 1, // seconds
x: 6,
y: 4,
z: 4,
onUpdate: function() {
controls.enabled = true;
// controls.enableRotate = true;
}
} );
}