Hello, I have a plane
const planeWidth = 1500; // Desired plane width
const planeHeight = planeWidth / imageAspectRatio;
const geometry = new THREE.PlaneGeometry(planeWidth, planeHeight);
const material = new THREE.MeshBasicMaterial({
map: loadedTexture,
// transparent: true,
});
this plane includes a lot of child meshes. I need to implement zoom to object functionality. But When I try to obtain the position of the child mesh. I always get (0,0,0).
Here How did I make a child
const planeCoordinates = entity.coords.map((coord) => {
const rectX = parseFloat(coord[0]);
const rectY = parseFloat(coord[1]);
const planeX = (rectX / imageWidth) * planeWidth - planeWidth / 2;
const planeY = -((rectY / imageHeight) * planeHeight - planeHeight / 2);
return [planeX, planeY];
});
const rectShape = new THREE.Shape();
rectShape.moveTo(planeCoordinates[0][0], planeCoordinates[0][1]);
for (let i = 1; i < planeCoordinates.length; i++) {
rectShape.lineTo(planeCoordinates[i][0], planeCoordinates[i][1]);
}
rectShape.lineTo(planeCoordinates[0][0], planeCoordinates[0][1]);
// NOTE create rect mesh started
const rectGeometry = new THREE.ShapeGeometry(rectShape);
const rectMaterial = new THREE.MeshLambertMaterial({
color: 0xff0000,
});
// fix z-fighting issue
rectMaterial.depthWrite = false;
// NOTE entity mesh
entityMesh = new THREE.Mesh(rectGeometry, rectMaterial);
plane.add(entityMesh);
my function
function zoomInTimeline(x: number, y: number, z: number, zoomOutFactor = 0) {
const clickedEntityMesh = plane.children.find(
(entity) => entity.userData.id === 16105 && entity.userData.type === 'stand-center',
);
const position = new THREE.Vector3();
clickedEntityMesh.getWorldPosition(position);
console.log(position);
// const clickedEntityBoundingBox = new THREE.Box3().setFromObject(scene);
// const clickedEntityCenter = clickedEntityBoundingBox.getCenter(new THREE.Vector3());
gsap.timeline({ defaults: { duration: 1.5, ease: 'expo.out' } })
.to(controls.target, { x: position?.x, y: position?.y })
.to(camera.position, { x: position?.x, y: position?.y, z: z + zoomOutFactor }, 0)
.to(scene.rotation, { x: 0, y: 0 }, 0);
}