Javascript Three.js,如何访问场景中的项目?我应该使用document.getElementById()吗? for(变量i=0;i
我试图访问场景中的粒子,所以在将它们添加到场景中之前我预定义了ID。当我打印scene.children时,我可以看到它们的ID类似于“q0”、“q1”、“q2”……但是,document.getElementById()不允许我访问这些项目。在这种情况下,我应该如何操作?而不是执行以下操作:Javascript Three.js,如何访问场景中的项目?我应该使用document.getElementById()吗? for(变量i=0;i,javascript,three.js,Javascript,Three.js,我试图访问场景中的粒子,所以在将它们添加到场景中之前我预定义了ID。当我打印scene.children时,我可以看到它们的ID类似于“q0”、“q1”、“q2”……但是,document.getElementById()不允许我访问这些项目。在这种情况下,我应该如何操作?而不是执行以下操作: for ( var i = 0; i < 100; i ++ ) { var particle = new THREE.Particle( new THREE.ParticleCanvas
for ( var i = 0; i < 100; i ++ ) {
var particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x666666, program: programStroke } ) );
particle.position.x = Math.random() * 800 - 400;
particle.position.y = Math.random() * 800 - 400;
particle.position.z = Math.random() * 800 - 400;
particle.scale.x = particle.scale.y = Math.random() * 10 + 10;
scene.add(particle);
scene.children[i].id = "q"+i; // to select item using document.getElementById();
}
projector = new THREE.Projector();
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
if(showStats == true){
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
}
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
window.addEventListener( 'resize', onWindowResize, false );
console.log("1 -- "+scene);
console.log("2 -- "+scene.children);
console.log("3 -- "+document.getElementById('q1');
做:
然后
particle.name = "q"+i;
我不会使用
.id
。我会使用.name
,然后使用scene.getObjectByName('q10',true)
。是的,但如果要搜索多个对象,则getObjectByName()最终将遍历场景多次(即更昂贵)。
particle.name = "q"+i;
scene.traverse (function (object)
{
if (object instanceof THREE.Particle)
{
if (object.name === 'q10')
// do what you want with it.
}
});