最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Three.js, how to access items inside scene? Should I use document.getElementById()? - Stack Overflow

matteradmin9PV0评论
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');

I tried to access the particles inside scene, so I pre-defined ids before adding them into scene. When I printed out scene.children, I could see their ids are like 'q0', 'q1', 'q2'.... However, document.getElementById() does not allow me to access those items.. In this case, how should I do it?

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');

I tried to access the particles inside scene, so I pre-defined ids before adding them into scene. When I printed out scene.children, I could see their ids are like 'q0', 'q1', 'q2'.... However, document.getElementById() does not allow me to access those items.. In this case, how should I do it?

Share Improve this question asked May 21, 2013 at 15:36 QriousQrious 531 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

Instead of doing:

scene.children[i].id = "q"+i;

do:

particle.name = "q"+i;

and then

scene.traverse (function (object)
{
    if (object instanceof THREE.Particle)
    {
        if (object.name === 'q10')
            // do what you want with it.
    }
});
Post a comment

comment list (0)

  1. No comments so far