我加载一个 .obj 文件:
var loader = new THREE.OBJMTLLoader();
loader.load( "../obj/machine.obj", '../obj/machine.mtl', this.loadObject);
Run Code Online (Sandbox Code Playgroud)
并尝试检测对其的点击:
click: function(event){
this.mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
this.mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
var vector = new THREE.Vector3( this.mouse.x, this.mouse.y, 1 ).unproject( this.camera );
this.raycaster.set( this.camera.position, vector.sub( this.camera.position ).normalize() );
console.log(this.scene.children);
var intersects = this.raycaster.intersectObjects( this.scene.children );
if ( intersects.length > 0 ) {
console.log("hitting something");
}
},
Run Code Online (Sandbox Code Playgroud)
这在网格上效果很好,但在加载的 3DObject 上效果不佳,因为它在 this.scene.childeren 中可见:
[THREE.Mesh, THREE.Line, …Run Code Online (Sandbox Code Playgroud)