When clicking on the surface of the cube, rotating the focused surface forward

I have a cube and when any surface of this cube is clicked, I want the clicked surface to come to the front and I want the camera to focus on that surface.

What did you try to achieve such an effect so far :eyes: ? Some hints:


import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);


const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);


camera.position.z = 5;


const controls = new OrbitControls(camera, renderer.domElement);


const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

function onMouseClick(event) {

    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    const intersects = raycaster.intersectObjects([cube]);

    if (intersects.length > 0) {
        const face = intersects[0].face;
        const point = intersects[0].point;
        camera.lookAt(point);
    }
}

window.addEventListener('click', onMouseClick, false);


const animate = function () {
    requestAnimationFrame(animate);
    controls.update();
    renderer.render(scene, camera);
};


animate();

I tried something like this but the result was not what I wanted.

Ah, but it’s not far from the solution. Just instead of point you’d need to use normal :grin:

For example - lines 57 to 108. Move the camera / mock to object.position + normal to place the camera in front of the clicked face. Then use lookAt in camera / mock to make it look at object.position.

1 Like

Thank you very much bro

Bro, do you have any advice on adding markers to the surfaces of the cube?