Skip to content

Instantly share code, notes, and snippets.

@rms80
Created July 14, 2017 02:02
Show Gist options
  • Save rms80/56d0ae10081a7938bcf223a0ffe3f40f to your computer and use it in GitHub Desktop.
Save rms80/56d0ae10081a7938bcf223a0ffe3f40f to your computer and use it in GitHub Desktop.
float voxelSize = 1.0f;
DMeshSO meshSO = CotangentUI.ActiveScene.FindSceneObjectsOfType<DMeshSO>()[0];
AxisAlignedBox3f bounds = meshSO.GetTransformedBoundingBox();
bounds.Expand(2*voxelSize);
Vector3i gridSize = (Vector3i)(bounds.Diagonal / voxelSize);
Bitmap3d bitmap = new Bitmap3d(gridSize);
foreach ( Vector3i idx in bitmap.Indices() ) {
Vector3f v = (Vector3f)idx * voxelSize;
v += bounds.Min;
if (meshSO.Spatial.IsInside(v))
bitmap.Set(idx, true);
}
VoxelSurfaceGenerator gen = new VoxelSurfaceGenerator() {
Voxels = bitmap, Clockwise = false
};
gen.Generate();
DMesh3 mesh = gen.Mesh;
MeshTransforms.Scale(mesh, voxelSize);
MeshTransforms.Translate(mesh, bounds.Min);
mesh.EnableVertexColors(Colorf.White);
AxisAlignedBox3d meshBox = mesh.CachedBounds;
foreach (int vid in mesh.VertexIndices()) {
Vector3d v = mesh.GetVertex(vid);
double t = (v.y - meshBox.Min.y) / meshBox.Height;
mesh.SetVertexColor(vid, Colorf.Lerp(Colorf.White, Colorf.Red, (float)t));
}
DMeshSO voxelSO = new DMeshSO();
voxelSO.Create(mesh, CotangentUI.ActiveScene.DefaultMeshSOMaterial);
CotangentUI.ActiveScene.AddSceneObject(voxelSO, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment