最新消息: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 Create multiple objects - Stack Overflow

matteradmin3PV0评论

I want to create multiple objects.

    var distance = 10;
    var geometry = new THREE.BoxGeometry(10,10,10);
    var material = new THREE.MeshBasicMaterial({color:0x00ff44});

    for(var i = 0; i < 4;i++){
        var mesh  = new THREE.Mesh(geometry, material);
        mesh.position.z = distance;
        scene.add(mesh);
        distance += 5;
    };`

With this code i create it, but only in one row. I want to create more rows on the back of the first row. Like this image:

What i want to create - Image

I want more cubes on the Red X position.

I want to create multiple objects.

    var distance = 10;
    var geometry = new THREE.BoxGeometry(10,10,10);
    var material = new THREE.MeshBasicMaterial({color:0x00ff44});

    for(var i = 0; i < 4;i++){
        var mesh  = new THREE.Mesh(geometry, material);
        mesh.position.z = distance;
        scene.add(mesh);
        distance += 5;
    };`

With this code i create it, but only in one row. I want to create more rows on the back of the first row. Like this image:

What i want to create - Image

I want more cubes on the Red X position.

Share Improve this question asked Dec 27, 2016 at 3:00 PedroPedro 511 gold badge2 silver badges8 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2
var xDistance = 50;
var zDistance = 30;
var geometry = new THREE.BoxGeometry(10,10,10);
var material = new THREE.MeshBasicMaterial({color:0x00ff44});

//initial offset so does not start in middle.
var xOffset = -80;

for(var i = 0; i < 4; i++){
    for(var j = 0; j < 3; j++){
            var mesh  = new THREE.Mesh(geometry, material);
            mesh.position.x = (xDistance * i) + xOffset;
            mesh.position.z = (zDistance * j);
            scene.add(mesh);
    }
};

See this fiddle

Post a comment

comment list (0)

  1. No comments so far