gpt4 book ai didi

javascript - Three.js无法使用Raycaster选择对象

转载 作者:行者123 更新时间:2023-12-02 16:55:29 25 4
gpt4 key购买 nike

我有以下函数来获取所选对象,所以我的函数在这里

function onMouseDown(event) {
console.log(event);
event.preventDefault();

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

var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );


var pLocal = new THREE.Vector3(0, 0, -1);
var pWorld = pLocal.applyMatrix4(camera.matrixWorld);
var ray = new THREE.Raycaster(pWorld, vector.sub(pWorld).normalize());




ray.set( camera.position, vector.sub( camera.position ).normalize() );
var intersects = ray.intersectObjects( scene.children );
//console.log(intersects);
console.log(scene.children);
if ( intersects.length > 0 ) {

var clickedObject = intersects[0].object;

console.log('objects '+intersects[ 0 ].object);
console.log('objects id'+intersects[ 0 ].object.id);
console.log('objects name'+intersects[ 0 ].object.name);

if ( INTERSECTED != intersects[ 0 ].object ) {

if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

INTERSECTED = intersects[ 0 ].object;
console.log('INTERSECTED '+INTERSECTED);
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
INTERSECTED.material.emissive.setHex( 0xff0000 );

}

} else {

if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

INTERSECTED = null;

}
}

但是在 intersects 上得到空数组,我尽了最大努力,但我在这里找不到任何解决方案,如果我控制台 scene.children 我可以看到场景的所有附加对象在此之前,我将对象添加到场景中,如下所示

var   loader = new THREE.JSONLoader();      
loader.load("uploads/accessories/3d/code/3dfile_"+file+".js",
function(geometry, object_material)
{

var object = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(object_material));
model = new THREE.Object3D();
model.id="Myid"+file;
model.name="Myname"+file;
model.userData ={ URL: "http://myurl.com" };
model.add(object);
model.position.set(x,y,z);
model.scale.set(obj_width,obj_height,obj_rotation);
model.opacity =2;
model.rotation.y = 600;
model.duration = 12000;
model.mirroredLoop = true;
model.castShadow = true;
model.receiveShadow = true;

console.log(model);
var smodel=model;
scene.add(smodel);
}
);

现在我想通过 onMouseDown 中的 new THREE.Object3D(); 获取添加的模型,但我不能,有人有同样的问题吗?

最佳答案

您需要将recursive标志传递给Raycaster.intersectObjects()

intersects = ray.intersectObjects( scene.children, true );

此外,请勿覆盖 Object3D.id。设置Object3D.userData.id

三.js r.68

关于javascript - Three.js无法使用Raycaster选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26218453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com