Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

javascript - three.js: Access Scene Objects by Name or ID

I'm generating objects from an array which I've defined like this (It's not limited to these three):

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

Now it goes through the each loop to create and add the objects to the scene by Three.JS like this:

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

The question is:
How can I access the objects that I've made in the each loop above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this:

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

or to recursively search the scene graph

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

Alternatively, you can search by ID.

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

three.js r.61


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...