Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Three.js Raycast can';我找不到目标_Javascript_Three.js - Fatal编程技术网

Javascript Three.js Raycast can';我找不到目标

Javascript Three.js Raycast can';我找不到目标,javascript,three.js,Javascript,Three.js,当我点击一个三维模型时,我想获得对象id、名称或对象,以便能够删除这个对象 function onDocumentMouseDown( event ) { if (enableRaycasting){ event.preventDefault(); mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 ); raycas

当我点击一个三维模型时,我想获得对象id、名称或对象,以便能够删除这个对象

    function onDocumentMouseDown( event ) {
if (enableRaycasting){
event.preventDefault();
mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( 
    event.clientY / window.innerHeight ) * 2 + 1 );
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children, true );
console.log(intersects);    
if ( intersects.length > 0 ) {


scene.remove(intersects[0]); //doesnt work   
    scene.remove(intersects[0].object); //doesnt work
    scene.remove(intersects[1]); //doesnt work
scene.remove(intersects[1].object); //doesnt work                
    console.log(intersects[0].object.name) //Bot_Skinned_0
    console.log(intersects[0].object.id)// output: 72



}

    render();
}
}
问题:创建对象时,ID为21,模型名称为“BotSkinned”(因为我编码了
object.name=“BotSkinned”
)。然后,我将对象添加到场景中:

    scene.add( object );
    console.log(scene.getObjectByName("BotSkinned").id); //output: 21
使用光线投射,我希望获得该对象并将其f.e.移除,但这些移除命令都不起作用,并且名称和ID也不相同。有人知道如何获得正确的
对象.id
?这将是最简单的解决办法。
编辑:当然我可以说
scene.remove(scene.getObjectByName(“BotSkinned”))
,但是光线投射应该能给我准确的对象,因为我将来会有很多这样的对象。

这是正确的id:intersects[0]。object.parent.parent.parent.id; 因此现在很容易删除:scene.remove(scene.getObjectById(与[0].object.parent.parent.parent.id相交))。 希望它与gltf、collada等类似