three.js:按名称或ID访问场景对象

时间:2013-10-17 12:18:37

标签: javascript three.js

我正在从一个我已经定义过的数组中生成对象(它不仅限于这三个):

var links = [['Linkedin','img/linkedin.png','-300','-230', '600'],
             ['Google+', 'img/google.png',  '0',   '-230', '600'],
             ['Twitter', 'img/twitter.png', '300', '-230', '600']];

现在它通过每个循环来创建Three.JS并将对象添加到场景中,如下所示:

$.each(links, function(i, item) {
    var thisItemTexture = THREE.ImageUtils.loadTexture(item[1]);
    thisItemGeo = new THREE.CubeGeometry(60, 60, 60,1 ,1 , 1);
    thisItemMat = new THREE.MeshBasicMaterial({map: thisItemTexture });
    thisItem =    new THREE.Mesh(thisItemGeo, thisItemMat);
    scene.add(thisItem);
    thisItem.position.x = item[2];
    thisItem.position.y = item[3];
    thisItem.position.z = item[4];
    thisItem.castShadow = true;
    thisItem.receiveShadow = true;          
});

问题是:
如何访问我在上面每个循环中创建的对象?

1 个答案:

答案 0 :(得分:51)

你可以这样做:

myObject.name = "objectName";
...
var object = scene.getObjectByName( "objectName" );

或以递归方式搜索场景图

var object = scene.getObjectByName( "objectName", true );

或者,您可以按ID搜索。

var object = scene.getObjectById( 4, true );

three.js r.61