THREE.js跟踪模型(对象)

时间:2023-02-05 19:39:13

I am loading .obj files using THREE.OBJLoader() and pushing each object in myobj[] array after adding it to screen.

我正在使用THREE.OBJLoader()加载.obj文件,并在将其添加到屏幕后推送myobj []数组中的每个对象。

var myObjs = [];
var loader = new THREE.OBJLoader();
loader.addEventListener( 'load', function ( event ) {
var object = event.content;
object.position.x=xpos;
object.position.y = ypos;
scene.add( object );
teeth.push(object);
});
loader.load( 'obj/myobj1.obj' );
loader.load('obj/myobj2.obj');
loader.load('obj/myobj3.obj');

I can check whether an object is clicked or not using the following code:

我可以使用以下代码检查是否单击了对象:

function onDocumentMouseDown( event ) {

event.preventDefault();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - (   event.clientY / window.innerHeight ) * 2 + 1, 0.5 );


projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObjects( teeth, true );

if ( intersects.length > 0 ) {

intersects[0].object.position.z=50;
}

So far so good. Now what I can't figure out is how to track which of my object in myobjs[] array was click or should I say how can I map intersects[0].object in myobjs array.

到现在为止还挺好。现在我无法弄清楚如何跟踪我的myobjs []数组中的哪个对象是单击还是应该说我如何在myobjs数组中映射intersects [0] .object。

Regards, ZB

此致,ZB

1 个解决方案

#1


2  

The object will be exactly the same, i.e. intersects[0].object and some myobjs array member will both be references to the same instance. If you want to find the index to the myobjs array (for example in order to delete it from there), you have several choices:

对象将完全相同,即intersects [0] .object和一些myobjs数组成员都将引用同一个实例。如果要查找myobjs数组的索引(例如,为了从那里删除它),您有以下几种选择:

  1. After intersection, loop through myobjs and compare intersects[0].object.id to myobj[i].id (each three.js object has a unique id property).
  2. 在交集之后,循环通过myobjs并将intersects [0] .object.id与myobj [i] .id进行比较(每个three.js对象都有一个唯一的id属性)。
  3. You are also free to assign any suitable custom properties to the object in your load event handler, so simply add a line object.myId = myobjs.length; just before pushing it to myobjs and you can index the array later with intersects[0].object.myId.
  4. 您也可以*地为load事件处理程序中的对象分配任何合适的自定义属性,因此只需添加一行object.myId = myobjs.length;在将其推送到myobjs之前,您可以稍后使用intersects [0] .object.myId索引数组。

#1


2  

The object will be exactly the same, i.e. intersects[0].object and some myobjs array member will both be references to the same instance. If you want to find the index to the myobjs array (for example in order to delete it from there), you have several choices:

对象将完全相同,即intersects [0] .object和一些myobjs数组成员都将引用同一个实例。如果要查找myobjs数组的索引(例如,为了从那里删除它),您有以下几种选择:

  1. After intersection, loop through myobjs and compare intersects[0].object.id to myobj[i].id (each three.js object has a unique id property).
  2. 在交集之后,循环通过myobjs并将intersects [0] .object.id与myobj [i] .id进行比较(每个three.js对象都有一个唯一的id属性)。
  3. You are also free to assign any suitable custom properties to the object in your load event handler, so simply add a line object.myId = myobjs.length; just before pushing it to myobjs and you can index the array later with intersects[0].object.myId.
  4. 您也可以*地为load事件处理程序中的对象分配任何合适的自定义属性,因此只需添加一行object.myId = myobjs.length;在将其推送到myobjs之前,您可以稍后使用intersects [0] .object.myId索引数组。