【问题标题】:three.js - get object name with mouse clickthree.js - 通过鼠标单击获取对象名称
【发布时间】:2014-12-02 18:05:39
【问题描述】:

我已经使用 json 加载器将 3 个带有名称的外部模型加载到我的场景中,现在我想通过单击它来获取模型/对象的名称。

下面是我用来加载模型的那个

var object_material = new THREE.MeshBasicMaterial({                 
                    color: 0xd6d6d6,                            
                    side: THREE.DoubleSide          
                });

    var   loader = new THREE.JSONLoader();
    loader.load("models/"+file,
                    function(geometry, object_material) 
                    {   

            var object = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(object_material));             

                        model = new THREE.Object3D();
                        model.id=file;
                        model.name='sample'+file;
                        model.userData.id='sampledata'+file;
                        model.add(object);    
                        model.position.set(obj_x,obj_y,obj_z);                                                  

                        model.mirroredLoop = true;
                        model.castShadow = true;
                        model.receiveShadow = true;         

                        scene.add(model);

                    }
                );  

下面是我的鼠标向下功能

function onMouseDown(event) {

    event.preventDefault();

    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
    projector.unprojectVector( vector, camera );


        var pLocal = new THREE.Vector3(0, 0, -1);
        var pWorld = pLocal.applyMatrix4(camera.matrixWorld);
        var ray = new THREE.Raycaster(pWorld, vector.sub(pWorld).normalize());
        // Get meshes from all objects
        var getMeshes = function(children) {
            var meshes = [];
            for (var i = 0; i < children.length; i++) 
            {
                if (children[i].children.length > 0) {
                    meshes = meshes.concat(getMeshes(children[i].children));
                } else if (children[i] instanceof THREE.Mesh) {
                    meshes.push(children[i]);
                }
            }
            return meshes;
        };
        function attributeValues(o) 
        {
            var out = [];
            for (var key in o) {
            if (!o.hasOwnProperty(key))
            continue;
            out.push(o[key]);
            }
            return out;
        }
        var objects = attributeValues(this.o3dByEntityId);
        var meshes = getMeshes(objects);

    var intersects = ray.intersectObjects(meshes);      
    raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
    var intersects = raycaster.intersectObjects( scene.children );

    console.log(scene);
    // this console displays all the objects under children - THREE.Object3D - name as name: "sample513.js" 

    if ( intersects.length > 0 )
    {
        // but for the clickedObject - the length is > 0 and name is empty
        var clickedObject = intersects[0].object;
        console.log(clickedObject.parent.userData.id); // return as undefined

        if ( INTERSECTED != intersects[ 0 ].object )
        {                               
            INTERSECTED= intersects[ 0 ].object;                
            name    =   INTERSECTED.name;                       
        }

    } else {

        console.log('intersects.length is 0');

    }
}

即使我在 userData 中提供了型号名称,我也无法检索它。任何人都可以指导我如何在单击对象时检索对象的名称

【问题讨论】:

    标签: javascript jquery three.js


    【解决方案1】:

    尝试通过这个example。查看控制台中的消息。

    <script src="js/controls/EventsControls.js"></script>
    
    EventsControls = new EventsControls( camera, renderer.domElement );
    
    EventsControls.attachEvent( 'onclick', function() {
    
        console.log( 'this.focused.name: ' + this.focused.name );
    
    });
    

    //如果使用拖放

    EventsControls.attachEvent( 'dragAndDrop', function () {
    
        this.container.style.cursor = 'move';
        this.focused.position.y = this.previous.y;
    
    });
    
    EventsControls.attachEvent( 'mouseOut', function () {
    
        this.container.style.cursor = 'auto';
    
    });
    
    var jsonLoader = new THREE.JSONLoader();
    jsonLoader.load( "models/Tux.js", addModelToScene ); 
    
    
    function addModelToScene( geometry, materials ) {
    
       var material = new THREE.MeshFaceMaterial( materials );
       model = new THREE.Mesh( geometry, material );
       model.scale.set( 10, 10, 10 ); model.name = 'Tux';
       model.rotation.x = -Math.PI/2; 
       model.position.set( 175, 45, 125 );
       scene.add( model ); 
       EventsControls.attach( model );
    
    }
    

    【讨论】:

    • 我将 DisplaceControl 重命名为 EventsControls,发现并纠正了一个错误,并更改了链接。如果仍然是实际的,也请更改
    【解决方案2】:

    clickedObject 的父级可能未定义。也许您可以通过控制台记录 clickedObject 并查看访问 id 所需的路径。

    【讨论】:

    • clickedObject 返回 undefined - 问题所在,无法获取点击对象的详细信息
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多