public void VoxelizeMesh() { if (mesh[0] == null) { return; } if (!objectHasChanged) { return; } Transform[] ts = gameObject.GetComponentsInChildren <Transform>(); for (int i = 1; i < ts.Length; ++i) { Destroy(ts[i].gameObject); } STLLoader.Vec3[] direction = new STLLoader.Vec3[3]; direction[0] = new STLLoader.Vec3(1, 0, 0); direction[1] = new STLLoader.Vec3(0, 1, 0); direction[2] = new STLLoader.Vec3(0, 0, 1); voxelizer = new STLLoader.Voxelizer(xResolution, yResolution, zResolution); voxelizer.MultiyDirectionsVoxelize(mesh[0], direction); List <STLLoader.Vec3> localCorners = voxelizer.CalculateLocalCorners(); STLLoader.Quickhull quickhull = new STLLoader.Quickhull(); List <int> convexFaces = new List <int>(); quickhull.CalculateQuickhull(localCorners, ref convexFaces); convexHullOutline.Clear(); quickhull.CalculatePolygonEdges(ref convexHullOutline); STLLoader.Vec3 centerOfMass = voxelizer.GetCenterOfMass(); //voxelizer.SafeLayer(fileName, 0); voxelMesh = new STLLoader.Mesh[1]; voxelMesh[0] = voxelizer.GenerateMesh(0, resolution); float voxelSize = voxelizer.GetVoxelSize(); STLLoader.Vec3 offset = voxelizer.GetMinOffset(); convexHull.ConfigureConvexHull(ref localCorners, ref convexFaces, voxelSize, new Vector3(offset.x, offset.y, offset.z), ref convexHullOutline); CreateIndexedMesh(ref voxelMesh, (int)resolution); transform.localScale = new Vector3(voxelSize, voxelSize, voxelSize); transform.position = new Vector3(offset.x, offset.y, offset.z); objectHasChanged = false; voxelMeshHasChanged = false; }
public static void LoadVoxelization(string fileName, ref Voxelizer voxelizer) { using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { uint xResolution = reader.ReadUInt32(); uint yResolution = reader.ReadUInt32(); uint zResolution = reader.ReadUInt32(); voxelizer = new Voxelizer(xResolution, yResolution, zResolution); Voxel[,,] grid = voxelizer.grid; for (int i = 0; i < xResolution; ++i) { for (int j = 0; j < yResolution; ++j) { for (int k = 0; k < zResolution; ++k) { grid[i, j, k].layer = reader.ReadInt32(); grid[i, j, k].set = reader.ReadBoolean(); } } } } }