THREE.js - Object Picking doesnt report parent object name on object loaded with OBJMTLLoader

China☆狼群 提交于 2020-01-17 04:57:26

问题


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).

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.

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

}

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.

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

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; 

but they dont fix it.

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


回答1:


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

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

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 }

Otherwise select the intersected object A itself.

else
    { var referred_Object_B = intersected_object_A }

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


来源:https://stackoverflow.com/questions/32865389/three-js-object-picking-doesnt-report-parent-object-name-on-object-loaded-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!