pmneila / PyMCubes

Marching cubes (and related tools) for Python
BSD 3-Clause "New" or "Revised" License
692 stars 87 forks source link

Complex Shapes? #42

Closed ajh123 closed 1 year ago

ajh123 commented 1 year ago

I know we can use simple shapes like spheres

X, Y, Z = np.mgrid[:30, :30, :30]
world = (X-15)**2 + (Y-15)**2 + (Z-15)**2 - 8**2
vertices, triangles = mcubes.marching_cubes(mcubes.smooth(world), 0.5)

But I'm struggling to figure out how to make more advance shape (like terrain). A good idea for games / terrain editors. I thought making a 3d array (grid) of voxels which is given to mcubes.smooth, but my implementation doesn't get anything rendered.

Currently my implementation looks like:

n = 30
voxels = [[[0 for k in range(n)] for j in range(n)] for i in range(n)]
for vx in range(0, 30):
    for vy in range(0, 30):
        for vz in range(0, 30):
            voxels[vx][vy][vz] = 1

def makeMesh(x, y, z):
    result = 1
    if voxels[x][y][z] == 1:
        result = -1
    return result

vMakeMesh = np.vectorize(makeMesh)

X, Y, Z = np.mgrid[:30, :30, :30]
world = vMakeMesh(X, Y, Z)

vertices, triangles = mcubes.marching_cubes(mcubes.smooth(world), 0.5)

I think my problem is that world does not contain correct data. I think the problem comes from the makeMesh method, or how I fill my voxels array.