本文介绍了threejs raycast点击检测无法加载3dObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载.obj文件:

var loader = new THREE.OBJMTLLoader();
loader.load( "../obj/machine.obj", '../obj/machine.mtl',  this.loadObject);

并尝试检测点击:

 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");

        }
    },

这在网格上工作正常但是当它在this.scene.childeren中可见时,不在加载的3DObject上:

This works fine on meshes but not on the loaded 3DObject while it is visible in the this.scene.childeren:

[THREE.Mesh, THREE.Line, THREE.PointLight, --> THREE.Object3D <-- ]0: THREE.Mesh__webglActive: true__webglInit: true_listeners: Object_modelViewMatrix: THREE.Matrix4_normalMatrix: THREE.Matrix3castShadow: falsechildren: Array[0]eulerOrder: (...)frustumCulled: truegeometry: THREE.IcosahedronGeometryid: 4material: THREE.MeshBasicMaterialmatrix: THREE.Matrix4matrixAutoUpdate: truematrixWorld: THREE.Matrix4matrixWorldNeedsUpdate: falsename: ""parent: THREE.Sceneposition: THREE.Vector3quaternion: THREE.QuaternionreceiveShadow: falserenderDepth: nullrotation: THREE.EulerrotationAutoUpdate: truescale: THREE.Vector3type: "Mesh"up: THREE.Vector3useQuaternion: (...)userData: Objectuuid: "46D85379-A9CE-4221-A599-39D13EE4CB34"visible: true__proto__: Object1: THREE.Line2: THREE.PointLight3: THREE.Object3D

所以有些东西告诉我它需要加载3dObjects的其他内容。但我不知道那可能是什么。我的想法是光线投影寻找交叉的顶点,如果它是立方体网格或3dObject网格则无关紧要。有没有人有想法?

So something tells me it needs something else for loaded 3dObjects. But I have no clue at what that could be. My idea is that the raycast looks for vertices for intersection and it wouldn't matter if that's a cube mesh or a 3dObject mesh. Does anybody has an idea?

推荐答案

你需要像这样传递递归标志

You need to pass the recursive flag like so

var intersects = raycaster.intersectObjects( objects, true );

three.js r.69

three.js r.69

这篇关于threejs raycast点击检测无法加载3dObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 19:38