Hey there,
I’ve managed to draw some cubes with a for loop. However my animation is only animating one of the cubes, not all.
This is a link to the codepen: https://codepen.io/charlieyin/pen/VwvoBGY
And here is the code in my init() function:
//cube
var geometry = new THREE.BoxGeometry();
var material = new THREE.MeshLambertMaterial({
color: 0x00ff00,
wireframe: true,
});
for (let i = 0; i < 3; i++) {
cube = new THREE.Mesh(geometry, material);
cube.position.x = i;
scene.add(cube);
}
and here is the code in my render function:
function render() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
I’m sure it’s quite a simple solution that I’m overlooking. Any help would be appreciated, thanks!
hofk
May 31, 2020, 8:21am
2
You keep overwriting your cube in the loop. Use an array and you can address each cube via the index.
var cube = [];
...
for (let i = 0; i < 3; i++) {
cube[i] = new THREE.Mesh(geometry, material);
cube[i].position.x = i;
scene.add(cube[i]);
}
...
function render() {
cube[0].rotation.x += 0.01;
cube[0].rotation.y += 0.01;
// use other indexes....
renderer.render(scene, camera);
}
Check this out too:
https://hofk.de/main/discourse.threejs/2019/Carousel/Carousel.html
from
The answers to some questions contain some very interesting and instructive basic examples. These are difficult to find in the large number of questions on discourse.There are also good examples in resources and showcase.
Therefore I collected them and put them on one page, clearly arranged by time.
The collection may be a help for beginners.
UPDATE 2019
http://discourse.threejs.hofk.de/ is now redirected
to the safe side * discourse.threejs.hofk.de
UPDATE 202…
1 Like