加载的对象的父对象名称

加载的对象的父对象名称

本文介绍了THREE.js - 对象选取不会报告使用 OBJMTLLoader 加载的对象的父对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 OBJMTLLoader 加载了一个带有 MTL 文件纹理的 OBJ 文件.我从 http://threejs.org/examples/webgl_loader_obj_mtl.html 复制了示例.

I have loaded an OBJ file with MTL file textures using OBJMTLLoader.I copied the example from http://threejs.org/examples/webgl_loader_obj_mtl.html.

主要对象(穿着西装、头发、手和鞋子的男人)显示正常,纹理正确(例如眼睛、嘴巴、领带、纽扣).

The main object (man in business suit with hair, hands and shoes) displays OK with correct textures (e.g. eyes, mouth, tie, buttons).

加载的对象是一个包含 10 个子对象的 THREE.Group,每个子对象是一个 THREE.Object3D,其中还有 3、5 或 7 个子对象 THREE.Mesh 对象.

The loaded object is a THREE.Group with 10 children, each child being a THREE.Object3D which has further 3, 5 or 7 child THREE.Mesh objects.

这里是加载OBJ和MTL的js代码...

Here is the js code for loading the OBJ and MTL...

//====================================================

//==================================================

function SOW_F_Load_OBJMTL_Model ( givenFilespec, mtlFilespec, givenName, givenScene, givenHexColorStr, posX, posY, posZ,  rotX, rotY, rotZ, scaleX, scaleY, scaleZ )
    {

          THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

          var ObjMtl_loader = new THREE.OBJMTLLoader();

          ObjMtl_loader.load( givenFilespec, mtlFilespec, SOW_F_make_LoadedOBJ_Handler ( givenName, givenScene, givenHexColorStr, posX, posY, posZ,  rotX, rotY, rotZ, scaleX, scaleY, scaleZ ) );
        }

    }



//=============================================

function SOW_F_make_LoadedOBJMTL_Handler( givenName, givenScene, givenHexColorStr, posX, posY, posZ, rotX, rotY, rotZ, scaleX, scaleY, scaleZ )
{

    return function ( object )
    {
        object.position.set( posX, posY, posZ );
        object.rotation.set( rotX, rotY, rotZ );
        object.name = givenName;
        object.scale.set( scaleX, scaleY, scaleZ );
        givenScene.add( object );

        object.traverse (  function ( child )
                {
                    if ( child instanceof THREE.Mesh )
                    {
                        child.userData.rootObject = object;
                        //... following are for when material doesn't load
                        child.geometry.computeFaceNormals();
                        child.geometry.computeVertexNormals();
                        child.geometry.normalsNeedUpdate = true;
                    }
                }
             )

        object.updateMatrix(); //... without this the next command is not effective.
        xxx = SOW_F_grob_Add_to_Target_Set( object );
    };

}

我的问题是对象拾取不报告用 OBJMTLLoader 加载的相交对象的名称.它要么报告纹理材质的名称,要么报告空白.

My problem is that object picking does not report the name of the intersected object loaded with OBJMTLLoader. It either reports the name of a texture material or a blank.

对象选取在我在我的 THREE.js 代码中创建的网格对象上工作正常.

Object picking works OK on mesh objects which I create in my THREE.js code.

我已经尝试了 选择通过 OBJMTLLoader 加载的 Object3D 中建议的修复,包括(在交点拣货代码):

I have tried the fixes suggested in Picking Object3D loaded via OBJMTLLoader including (in the intersection picking code):

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

和(在对象子处理代码中):

and (in the object child processing code):

child.userData.rootObject = object;

但他们没有修复它.

请有人建议我需要做什么才能使对象拾取报告使用 OBJMTLLoader 加载的对象的父对象?

Please can somebody suggest what I need to do to make object picking report the parent object for an object loaded with OBJMTLLoader?

推荐答案

啊,傻我,我只需要查找所选相交对象的 rootObject 的名称!

Ah, silly me, I just need to look up the name of the rootObject for the selected intersected object!

当对象A相交时选择引用对象B(要报告名称的对象或其他):-

To select the referred object B ( the object whose name is to be reported or whatever) when an object A is intersected:-

var intersected_object_A = intersects[ 0 ].object; //... nearest object

那么如果相交对象 A 具有属性 userData.rootObject,您可以选择 rootObject 作为引用对象 B.

Then if the intersected object A has a property userData.rootObject you can select the rootObject as the referred object B.

if ( intersected_object.userData.rootObject )
    { var referred_Object_B = intersected_object_A.userData.rootObject }

否则选择相交对象 A 本身.

Otherwise select the intersected object A itself.

else
    { var referred_Object_B = intersected_object_A }

alert ("You clicked on:" + referred_Object_B.name );

这篇关于THREE.js - 对象选取不会报告使用 OBJMTLLoader 加载的对象的父对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:41