如何在从 OBJLoader 加载的 obj 文件上获取鼠标单击位置? [英] How to get the mouse clicking position on an obj file loaded from OBJLoader?

查看:31
本文介绍了如何在从 OBJLoader 加载的 obj 文件上获取鼠标单击位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OBJLoader加载模型,这里是加载obj文件的代码:

I loaded the model using OBJLoader, here is the code for loading the obj file:

        var loader = new THREE.OBJLoader();
        loader.load('obj/teeth/teeth4_5.obj', function(object) {
            model = object;
            scene.add( model );
            objects.push( model );
        });

我正在尝试使用 raycaster 来找到交点.我从 canvas_interactive_cubes 示例(http://mrdoob.github.io/三.js/examples/canvas_interactive_cubes.html) 在three.js 中.这是找到交集的代码:

And I'm trying to use raycaster to find the intersection. I implemented my code from the canvas_interactive_cubes example (http://mrdoob.github.io/three.js/examples/canvas_interactive_cubes.html) in three.js. Here is the code to find the intersection:

    function onDocumentMouseDown( event ){
        event.preventDefault();
        var mouseX = (event.clientX / window.innerWidth)*2-1;
        var mouseY = -(event.clientY /window.innerHeight)*2+1;
        var vector = new THREE.Vector3( mouseX, mouseY, 0.5 );
        projector.unprojectVector( vector, camera );
        var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
        var intersects = raycaster.intersectObjects( scene.children );
        console.log( intersects[0].point);
    }

不幸的是,我无法获得交点的 x,y,z 坐标,无论我点击哪里,它总是显示TypeError: intersects[0] is undefined".

Unfortunately I'm not able to get the x,y,z coordinates of the intersection, no matter where I clicked, it always showed "TypeError: intersects[0] is undefined".

我被困在这里好几天了.有人可以告诉我一种在加载的 obj 文件上获取交集的方法吗?感谢您的帮助.

I'm getting stuck here for several days. Can someone tell me a way to get the intersection on a loaded obj file? I appreciate your help.

推荐答案

尝试像这样添加递归标志:

Try adding the recursive flag like so:

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

three.js r.58

three.js r.58

这篇关于如何在从 OBJLoader 加载的 obj 文件上获取鼠标单击位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆