Three.js - Arranging cubes in a grid

左心房为你撑大大i 提交于 2019-12-12 03:59:27

问题


I would like to position cubes in a rectangular/square like grid. I'm having trouble trying to create some methodology in depending on what I pick through an HTML form input (checkboxes) to have it arrange left to right and up to down, a series of cubes, in a prearranged grid all on the same plane.

What measurement units is three.js in? Right now, I'm setting up my shapes using the built-in geometries, for instance.

var planeGeometry = new THREE.PlaneGeometry(4, 1, 1, 1);

The 4 and 1; I'm unsure what that measures up to in pixels, although I do see it rendered. I'm resorting to eyeballing it (guess and checking) every time so that it looks acceptable.


回答1:


Without a fair bit of extra math THREE is not measured in pixels.

To make a simple grid (I leave optimizations, colors, etc for future refinements) try something like:

var hCount = from_my_web_form('horiz'),
    vCount = from_my_web_form('vert'),
    size = 1,
    spacing = 1.3;
var grid = new THREE.Object3d(); // just to hold them all together
for (var h=0; h<hCount; h+=1) {
    for (var v=0; v<vCount; v+=1) {
        var box = new THREE.Mesh(new THREE.BoxGeometry(size,size,size),
                      new THREE.MeshBasicMaterial());
        box.position.x = (h-hCount/2) * spacing;
        box.position.y = (v-vCount/2) * spacing;
        grid.add(box);
    }
}
scene.add(grid);


来源:https://stackoverflow.com/questions/32848707/three-js-arranging-cubes-in-a-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!