javascript 三.js:按名称或 ID 访问场景对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19426559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
three.js: Access Scene Objects by Name or ID
提问by Siamak A.Motlagh
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:
现在它通过每个循环通过 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;
});
The question is:
How can I access the objects that I've made in the each loop above?
问题是:
如何访问我在上面每个循环中创建的对象?
回答by WestLangley
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.
或者,您可以按 ID 搜索。
var object = scene.getObjectById( 4, true );
three.js r.61
三.js r.61