简体   繁体   English

THREE.JS 光线投射器

[英]THREE JS Raycaster

How to select only a specific object by clicking the mouse in THREE.JS如何通过在THREE.JS中点击鼠标来select只显示一个特定的object

Now the function is executed by clicking on any point, although the condition states the name mesh现在通过单击任意点执行 function,尽管条件声明名称为 mesh

var cubeGeometry = new THREE.BoxGeometry(100, 100, 100);
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x9370DB});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.y = 50;
cube.name = "cube";
scene.add(cube);

const sizeBox = 30;
const geo = new THREE.BoxGeometry(sizeBox, sizeBox, sizeBox);
const mat = new THREE.MeshPhongMaterial({
  side: THREE.DoubleSide,
});
const earth = new THREE.Mesh(geo, mat);

earth.position.set(-300, sizeBox/2, 10);

scene.add(earth);

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

function onMouseClick( event ) { function onMouseClick( 事件 ) {

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

raycaster.setFromCamera( mouse, camera );


const intersects = raycaster.intersectObjects( scene.children );

console.log(intersects);

for ( var i = 0; i < intersects.length; i++ ) {
  console.log(intersects[ i ].name)
    
    if(intersects[ i ].name = 'cube') {
        gsap.to(camera.position, {z: 100, duration: 10})
    }
    

}

} }

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

if (intersects[ i ].name = 'cube') { 
    gsap.to(camera.position, {z: 100, duration: 10})
} 

You're not testing anything in that "if" statement.您没有在该“if”语句中测试任何内容。 It should be "===" instead of "=".它应该是“===”而不是“=”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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