Answer the question
In order to leave comments, you need to log in
How to divide a cube into an arbitrary number of cubes divisible by 8 in three.js?
There is a cube, we know the coordinates of its center and its dimensions (x, y, z), it is necessary to be able to divide its arbitrary number of cubes divisible by 8.
I can’t think of a cycle for this operation at the moment there is a simple function for dividing the "first level" (half size for each of the coordinates + offset):
function setCubePartPosition(shift,geometry,material){
var cubes = [];
for(var i=0; i < 8; i++){
var cube = new THREE.Mesh(geometry, material);
var x = y = z = 0;
switch(i){
case 0:
x = shift;
y = shift;
z = shift;
break;
case 1:
x = -shift;
y = shift;
z = shift;
break;
case 2:
x = shift;
y = -shift;
z = shift;
break;
case 3:
x = shift;
y = shift;
z = -shift;
break;
case 4:
x = -shift;
y = -shift;
z = shift;
break;
case 5:
x = -shift;
y = -shift;
z = -shift;
break;
case 6:
x = -shift;
y = shift;
z = -shift;
break;
case 7:
x = shift;
y = -shift;
z = -shift;
break;
}
cube.position.set(x,y,z);
cubes.push(cube);
}
return cubes;
}
Answer the question
In order to leave comments, you need to log in
If at the input: coordinates, number of cubes (through a coefficient or a multiple of 8) and dimensions, geometry, material.
Then: I see a nesting cycle of 3, which can be optimized, and which will calculate the coordinates for cubes for each dimension, well, run your function there (only slightly modified, because x, y, z should not be 0).
Do the calculation of the coordinates separately, display them, check that everything is correct, and then substitute the drawing there in the desired coordinates of your cube. The simplest thing is to divide the task into several simple ones and then use them in the correct order.
Sorry, no code ;-)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question