【问题标题】:Know the name of the selected object at runtime在运行时知道所选对象的名称
【发布时间】:2013-01-21 22:21:22
【问题描述】:

对不起我的英语:我加载了几个对象。 Json 和我使用 Ray 选择它们并相交以使它们改变颜色。但我想知道在运行时选择了哪个加载的对象。当前并使用下面的代码,输出为:[object Object] $ var intersects = ray.intersectObjects(scene.children);

            if ( intersects.length > 0 ) {

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


                    var aux = intersects[ 0 ].object;

                    if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );


                    INTERSECTED = intersects[ 0 ].object;

                    INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
                    INTERSECTED.material.emissive.setHex( 0xffff00 );


                        console.debug("name:,"+aux);

                }

            } else {

                if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );

                INTERSECTED = null;


            }

【问题讨论】:

    标签: json three.js


    【解决方案1】:

    您可以在创建每个网格时为其添加唯一的id

    mesh = new THREE.Mesh( geometry, material );
    mesh.id = some_unique_id;
    scene.add( mesh );
    

    然后在你的相交逻辑中:

    if ( intersects.length > 0 ) {
    
        alert( intersects[0].object.id );
    
    }
    

    【讨论】:

    • 很遗憾没有使用此代码。当我使用命令 some_unique_id 时,3D 模型没有加载到浏览器中。
    • 不,不。您必须为 some_unique_id 设置合理的值。示例是 mesh.id = 2,或 mesh.id = j 如果 j 是变量,或 mesh.id = "Paula"。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2021-12-19
    • 2011-02-17
    相关资源
    最近更新 更多