简体   繁体   中英

THREE.JS : click event with raycaster and perspective camera

I want to create a menu for a web page in three.js where each tab is materialized by an object (a 2D rectangle) and we access a page of the website by clicking on the right rectangle. For this I read that I have to use a raycaster triggered when I right-click on my mouse and then use the array of the intersected objects to see on which of them I clicked.

I tried a lot of tutorial on the internet but never succeeded to have a functional raycaster and eventListener for the right-click. the raycast function is just copy/paste from the three.js documentation.

Here is my code (called script.js), do you see what I did wrong or do you have any advice to improve? Thanks in advance for your help guys!

var camera, scene, rendu;
var geometrie, materiau, mesh;
var controle;
var couleur, plan, onglet1, onglet2, onglet3, onglet4, onglet5, onglet6;
var r, t;
var rayon, souris;

init();

function init() {
    r = 3;
    t = 1.1;
    // ---------- scene et camera --------- //
    camera = new THREE.PerspectiveCamera( 70 , window.innerWidth / window.innerHeight , 0.01 , 10 );
    camera.position.set( 0 , 0 , 4 );
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0x000000);

    // --------- cube --------- //
    //materiau = new THREE.MeshBasicMaterial( { color: 0xffffff});
    //geometrie = new THREE.BoxGeometry( t , 3*t , t );
    //mesh = new THREE.Mesh( geometrie, materiau );
    //scene.add( mesh );

    // --------- onglets ---------- //
    couleur = new THREE.MeshBasicMaterial( {color: 0x031f3c , side: THREE.DoubleSide } );
    plan = new THREE.PlaneGeometry( 0.75 , 0.4 );

    onglet1 = new THREE.Mesh( plan , couleur );
    onglet1.position.set( 0, 0, r );
    scene.add( onglet1 );

    onglet2 = new THREE.Mesh( plan , couleur );
    onglet2.position.set( r*Math.sqrt(3/4), 0, 0.5*r );
    onglet2.rotation.y = Math.PI / 3;
    scene.add( onglet2 );

    onglet3 = new THREE.Mesh( plan , couleur );
    onglet3.position.set( r*Math.sqrt(3/4), 0, -0.5*r );
    onglet3.rotation.y = 2 *Math.PI / 3;
    scene.add( onglet3 );

    onglet4 = new THREE.Mesh( plan , couleur );
    onglet4.position.set( 0, 0, -r );
    scene.add( onglet4 );

    onglet5 = new THREE.Mesh( plan , couleur );
    onglet5.position.set( -r*Math.sqrt(3/4), 0, -0.5*r );
    onglet5.rotation.y = -2*Math.PI / 3;
    scene.add( onglet5 );

    onglet6 = new THREE.Mesh( plan , couleur );
    onglet6.position.set( -r*Math.sqrt(3/4), 0, 0.5*r );
    onglet6.rotation.y = -Math.PI / 3;
    scene.add( onglet6 );

    // ---------- rendu ------------- //
    rendu = new THREE.WebGLRenderer( { antialias: true} );
    rendu.setSize( window.innerWidth, window.innerHeight );
    rendu.setPixelRatio(window.devicePixelRatio);
    rendu.setAnimationLoop( animation );
    document.body.appendChild( rendu.domElement );

    // ------------- liens sur onglets ------------- //
    //window.addEventListener('click', onclick, false);
    // ecouter l'event de clic sur la souris
    // on ajoute un capteur de rayon qui regarde si on croise un objet de la scene
    // si c'est le cas on fait ce qu'on veut en fonction de l'objet

    rayon = new THREE.Raycaster();
    rendu.domElement.addEventListener( 'click', raycast, false );

}

function animation() {
    rendu.render( scene, camera );
    controle.update();
}

function raycast ( e ) {
    //1. sets the mouse position with a coordinate system where the center
    // of the screen is the origin
    souris.x = ( e.clientX / window.innerWidth ) * 2 - 1;
    souris.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
    //2. set the picking ray from the camera position and mouse coordinates
    rayon.setFromCamera( souris, camera );
    //3. compute intersections
    var intersections = rayon.intersectObjects( scene.children );
    intersections[0]
}

// ---------- orbitControls --------- //
controle = new THREE.OrbitControls(camera, rendu.domElement);
controle.minPolarAngle = Math.PI / 2;
controle.maxPolarAngle = Math.PI / 2;
controle.autoRotate = true;
controle.autoRotateSpeed = -0.1;
controle.enableZoom = false;
controle.mouseButtons = {
    LEFT: THREE.MOUSE.ROTATE,
    MIDDLE: THREE.MOUSE.DOLLY,
    RIGHT: THREE.MOUSE.ROTATE
}

// ----------- redimensionnement fenetre ------------ //
window.addEventListener('resize', function() {
    camera.aspect = window.innerWidth / window.innerHeight ;
    camera.updateProjectionMatrix();
    rendu.setSize(window.innerWidth, window.innerHeight);
}, false);

let loader = new THREE.GLTFLoader();
loader.load('voiture/scene.gltf', function(gltf){ 
    voiture = gltf.scene.children[0];
    //voiture.scale.set(0.5, 0.5, 0.5);
    scene.add(gltf.scene);
    rendu.render(scene, camera);
});

And here is my html code (called index.html)

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>

    <style>
        body{margin: 0px;}
        canvas{width: 100%; height: 100%;}
    </style>
</head>


<body>
    <script src="js/build/three.js"></script>
    <script src="js/build/three.min.js"></script>
    <script src="js/OrbitControls.js"></script>
    <script src="js/GLTFLoader.js"></script>
    <script src="js/script.js"></script>
</body>
</html>

I believe your problem stems from not initializing your souris variable as a THREE.Vector2() . Your code boils down to this:

var souris;
souris.x = xxx;
souris.y = yyy;

var rayon = new THREE.Raycaster();
rayon.setFromCamera( souris, camera );

Raycaster.setFromCamera requires you use a Vector2 as its first argument, but you never made a Vector2 !

To fix the issue, just make sure you're declaring souris = THREE.Vector2() before assigning its values:

var souris = THREE.Vector2();
souris.x = xxx;
souris.y = yyy;

var rayon = new THREE.Raycaster();
rayon.setFromCamera( souris, camera );

After initializing souris as a Vector2 , you'll cast the ray, then check if it actually intersects with an object before attempting to change anything.

Set a new material onto the first intersected object to avoid changing the color of all the objects in the scene as they share the same material.

function raycast ( e ) {
    //1. sets the mouse position with a coordinate system where the center
    // of the screen is the origin
    souris = new THREE.Vector2();
    souris.x = ( e.clientX / window.innerWidth ) * 2 - 1;
    souris.y = - ( e.clientY / window.innerHeight ) * 2 + 1;
    //2. set the picking ray from the camera position and mouse coordinates
    rayon.setFromCamera( souris, camera );
    //3. compute intersections
    var intersections = rayon.intersectObjects( scene.children );

    if (intersections.length) {
        intersections[0].object.material = 
            new THREE.MeshBasicMaterial({ 
                color: 0xff0000, 
                side: THREE.DoubleSide 
            });

        if (intersections[0].object.url) { window.open(intersections[0].object.url, '_blank'); }
    }
}

I hope this resolves your issue...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM