Sunday, May 20, 2018

c# - Understanding marching cubes and voxel data relationships



in case it matters i'm doing all this in unity with C# ...


I think I missed something or maybe don't understand the logic correctly. I have an existing voxel engine that looks very "minecrafty" at the moment, and I want to step it up a bit so it actually looks reasonably good!


So lets say I have a "chunk", and common sense says that a chunk can either be on or off in a voxel world so its basically a "point cloud" (you either render it or you dont). This means my game engine has something like this ...


public class Chunk
{
public Block[,,] Blocks;
... other stuff
}

public class Block

{
public bool Active;
... other stuff
}

So bring in the marching cubes and part 1 of Paul Bourke's guide (http://paulbourke.net/geometry/polygonise/) says ....



The first part of the algorithm uses a table (edgeTable) which maps the vertices under the isosurface to the intersecting edges. An 8 bit index is formed where each bit corresponds to a vertex.



   cubeindex = 0;

if (grid.val[0] < isolevel) cubeindex |= 1;
if (grid.val[1] < isolevel) cubeindex |= 2;
if (grid.val[2] < isolevel) cubeindex |= 4;
if (grid.val[3] < isolevel) cubeindex |= 8;
if (grid.val[4] < isolevel) cubeindex |= 16;
if (grid.val[5] < isolevel) cubeindex |= 32;
if (grid.val[6] < isolevel) cubeindex |= 64;
if (grid.val[7] < isolevel) cubeindex |= 128;

Now here's what throws me ... I'm comparing my "grid data" (so basically my block data) to some "isolevel" value. Every implementation i've seen (the easiest being here: http://nucleardevs.wordpress.com/2011/11/17/marching-cubes-sourcecode/) does exactly that but it creates a load of float values using something like simplex or perlin noise and runs through this logic.



floats? not bools?


So ...


Given a Block[,,] if i check each blocks state (Active property) and return a simple true or false ... why do i need a float / double value or should I say ... what is the purpose of the float value that requires it to be a float and not a simple boolean?


I gather they are referred to as "densities" which implies that I missed something because i've been thinking of them as the point cloud data to be evaluated and a voxel (to my knowledge) isn't a "density" value its more like a switch (on off).



Answer



Using float values allows you to use linear interpolation to enable you to make a mesh that's more accurate to your data. With many of the blocky type terrains you see in games these days, they just decide at what density a voxel is "solid". With noise values ranging between 0 and 1, they decide a value to split the float and round to 1 or 0.


However, many of the blocky voxel games aren't using marching cubes to generate their terrain meshes. Keeping the float values allows your marching cubes more accuracy. As I'm sure you're familiar with the marching cubes algorithm, having the float values allows you to shift the vertices of your triangles along the edges of the cubes depending on the adjacent floating point values.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...