三.js:按名称或 ID 访问场景对象 [英] three.js: Access Scene Objects by Name or ID
本文介绍了三.js:按名称或 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']];
现在它通过每个循环通过 Three.JS 创建对象并将其添加到场景中,如下所示:
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?
推荐答案
您可以这样做:
myObject.name = "objectName";
...
var object = scene.getObjectByName( "objectName" );
或者递归搜索场景图
var object = scene.getObjectByName( "objectName", true );
或者,您可以通过 ID 进行搜索.
Alternatively, you can search by ID.
var object = scene.getObjectById( 4, true );
three.js r.61
three.js r.61
这篇关于三.js:按名称或 ID 访问场景对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文