E
E
Evgeny Popov2015-03-12 11:08:01
JavaScript
Evgeny Popov, 2015-03-12 11:08:01

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;
}

As a result, we get a cube consisting of 8 cubes. This function, as I understand it, will need to be somehow used in a cycle so that when dividing into more parts, it does not create a cube, but shifts the center where other 8 cubes will be created, and so on.
Any ideas? I tried to search myself, but apparently it is not possible to correctly formulate a request for a similar task. Also, a solution with one cube and many faces will not work, since separate meshes are needed.
Perhaps I would like them to be built in an orderly manner, for example from the bottom up.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yaroshevich, 2015-03-12
@qfox

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 question

Ask a Question

731 491 924 answers to any question