How to make many cubes using a for (i) loop

Hello: (g72)
I’m trying to use the for ( i ) loop to make many cubes at once.
But for some reason, is not working.

I saw another example and I notice that the Geometry was outside the loop…

I don’t really get it. Please can you help…

I have this page: http://songnes.com/g/g72.html
and this code: http://songnes.com/g/g72.txt

thank you

I don’t see a single instance of a for() loop in your code text file. Maybe that’s why it’s not working :grin:?

You don’t see it, because I don’t know how to do it…
That’s why I’m asking for help…
I don’t know how to do it.

Imagine the cube, and I want to create 10, or 100 cubes
and I don’t want to create one at a time…

I don’t know how to do it…

Oh, you don’t know how to write a for loop? I strongly recommend you take an intro to JavaScript course. Youtube has a bunch of free videos. I feel like this is such an elemental building block to becoming a developer that it would be doing you a disservice to write one for you without learning how to do it for yourself!

Hi marquizzo…
I appreciate your comment, but yes I do know how to make a for i loop.

I will try to be more specific in my question. and I hope you, or someone else can help me.
it has to do in how to put objects and elements in order, to make them work with threeJS

In this example: http://songnes.com/g/DraggingObjects.html

you can see many cubes. It creates the geometry outside the loop

var geometry = new THREE.BoxBufferGeometry( 40, 40, 40 );

for ( var i = 0; i < 200; i ++ ) {

	var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );

	object.position.x = Math.random() * 1000 - 500;
	object.position.y = Math.random() * 600 - 300;
	object.position.z = Math.random() * 800 - 400;

	object.rotation.x = Math.random() * 2 * Math.PI;
	object.rotation.y = Math.random() * 2 * Math.PI;
	object.rotation.z = Math.random() * 2 * Math.PI;

	object.scale.x = Math.random() * 2 + 1;
	object.scale.y = Math.random() * 2 + 1;
	object.scale.z = Math.random() * 2 + 1;

	object.castShadow = true;
	object.receiveShadow = true;

	scene.add( object );

	objects.push( object );

}

But I’m having trouble making mine work.

int i = 0;
for (i = 0; i < 10; i++)
// create 10 cubes here

I do hope someone can help me

thank you

for ( let i = 0; i < 10; i ++ ){

mesh = new THREE.Mesh( geometry, material );
mesh.position.x = i/3; 
scene.add( mesh );

}

https://jsfiddle.net/Dragon3DGraff/9zo87k2b/

1 Like

int? that’s not js.

A single geometry object can be used by multiple meshes that’s why they make it outside the loop to save some memory.

Thank you Dragon3DGraff:
just what I need it… I got it to work fine. Thank you.