three.js通过自定义属性获取Object3D



我正在创建这样的LineSegments对象:

const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
const edges = new THREE.EdgesGeometry(geometry);
const line = new THREE.LineSegments(edges2, new THREE.LineBasicMaterial({ color: 0xffffff }));
scene.add(line);

然后我在我的行中添加了自定义属性:

line.prop1 = "hello";
line.prop2 = "world";

如何获取例如prop1"hello"的所有对象?

对于这个用例,不能使用内置方法。只有当对象具有唯一的属性值时,Object3D.getObjectByProperty((才有效。这个功能应该可以做到:

function getObjectsByProperty( object, property, value, result = [] ) {
// check the current object
if ( object[ property ] === value ) result.push( object );

// check children
for ( let i = 0, l = object.children.length; i < l; i ++ ) {
const child = object.children[ i ];
getObjectsByProperty( child, property, value, result );
}

return result;
}
//
const objects = getObjectsByProperty( scene, 'prop1', 'hello' );

最新更新