簡體   English   中英

Threejs中3D對象的“onClick”事件?

[英]"onClick" event for 3D objects in threejs?

Threejs中的對象是否有一個簡單的onClick事件? 我無法找到任何過時的幫助。

您可以使用THREE.Raycaster檢查 onclick 事件,

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

function onMouseDown( event ) {

    // calculate mouse position in normalized device coordinates
    // (-1 to +1) for both components

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

}

function render() {

    // update the picking ray with the camera and mouse position
    raycaster.setFromCamera( mouse, camera );

    // calculate objects intersecting the picking ray
    const intersects = raycaster.intersectObjects( scene.children );

    for ( let i = 0; i < intersects.length; i ++ ) {

        intersects[ i ].object.material.color.set( 0xff0000 ); //Sets the object selected to red

    }

    renderer.render( scene, camera );

}

window.addEventListener( 'mousedown', onMouseDown, false );

window.requestAnimationFrame(render);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM