Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript three.js:按名称或ID访问场景对象_Javascript_Three.js - Fatal编程技术网

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

Javascript three.js:按名称或ID访问场景对象,javascript,three.js,Javascript,Three.js,我从一个数组生成对象,我定义如下(不限于这三个): 现在它通过each循环创建对象,并通过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 = ne

我从一个数组生成对象,我定义如下(不限于这三个):

现在它通过each循环创建对象,并通过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;          
});
问题是:
如何访问我在上述每个循环中创建的对象?

您可以这样做:

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

对不起,我误解了你的问题。您要做的是保留对每个
此项的引用,对吗?是否有可能从
场景
中获取项目列表?@tobi我想以某种方式访问
场景
中的对象列表。似乎
场景.儿童
包含场景中的对象列表。@tobi我想,这不是一种合适的方法。也许我可以给循环中的每个对象一些参数。然后可以通过该参数访问该对象。类似于id。为了进行对象更改,您是否建议OP存储对象id数组?@LucianNovo如果您有问题,请发表新帖子。没有,但是如果对象名称不唯一,则
getObjectByName()
可能对您没有帮助。请记住,您还有
Object3D.userData
,这是一个可以自由添加自定义属性的对象。例如,
myObject.userData.name=“foo”
var object = scene.getObjectById( 4, true );