public VOXModel CalcVoxelCruncher(VoxData chunk, Color32[] palette) { var crunchers = new List <VOXCruncher>(); var bound = new Vector3Int(chunk.x, chunk.y, chunk.z); for (int i = 0; i < chunk.x; ++i) { for (int j = 0; j < chunk.y; ++j) { for (int k = 0; k < chunk.z; ++k) { var c = chunk.voxels[i, j, k]; if (c != int.MaxValue) { VOXVisiableFaces faces; if (!GetVisiableFaces(chunk.voxels, bound, i, j, k, c, palette, out faces)) { continue; } crunchers.Add(new VOXCruncher((byte)i, (byte)i, (byte)j, (byte)j, (byte)k, (byte)k, faces, c)); } } } } return(new VOXModel(crunchers)); }
public static VOXModel CalcVoxelCruncher(VoxData chunk, Color32[] palette, VOXCruncherMode mode) { switch (mode) { case VOXCruncherMode.Stupid: return(new VOXCruncherStupid().CalcVoxelCruncher(chunk, palette)); case VOXCruncherMode.Culled: return(new VOXCruncherCulled().CalcVoxelCruncher(chunk, palette)); case VOXCruncherMode.Greedy: return(new VOXCruncherGreedy().CalcVoxelCruncher(chunk, palette)); default: return(null); } }
public VOXModel CalcVoxelCruncher(VoxData chunk, Color32[] palette) { var crunchers = new List <VOXCruncher>(); var faces = new VOXVisiableFaces(true, true, true, true, true, true); for (int i = 0; i < chunk.x; ++i) { for (int j = 0; j < chunk.y; ++j) { for (int k = 0; k < chunk.z; ++k) { var m = chunk.voxels[i, j, k]; if (m != int.MaxValue) { crunchers.Add(new VOXCruncher(i, i, j, j, k, k, faces, m)); } } } } return(new VOXModel(crunchers)); }
public static GameObject CreateGameObject(string name, VoxData data, Texture2D texture, Color32[] colors, float scale, Shader shader) { var cruncher = VOXPolygonCruncher.CalcVoxelCruncher(data, colors, VOXCruncherMode.Greedy); var entities = new Dictionary <string, int>(); if (CalcFaceCountAsAllocate(cruncher, colors, ref entities) == 0) { throw new System.Exception(name + ": There is no voxel for this file"); } var model = new GameObject(name); foreach (var entity in entities) { if (entity.Value == 0) { continue; } var index = 0; var allocSize = entity.Value; var vertices = new Vector3[allocSize * 4]; var normals = new Vector3[allocSize * 4]; var uv = new Vector2[allocSize * 4]; var triangles = new int[allocSize * 6]; bool isTransparent = false; foreach (var it in cruncher.voxels) { VOXModelExtensions.CreateCubeMesh16x16(it, ref vertices, ref normals, ref uv, ref triangles, ref index, scale); isTransparent |= (colors[it.material].a < 255) ? true : false; } if (triangles.Length > 0) { Mesh mesh = new Mesh(); mesh.name = "mesh"; mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uv; mesh.triangles = triangles; var meshFilter = model.AddComponent <MeshFilter>(); #if UNITY_EDITOR MeshUtility.Optimize(mesh); meshFilter.sharedMesh = mesh; if (shader != null) { var meshRenderer = model.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = new Material(shader); meshRenderer.sharedMaterial.name = "material"; meshRenderer.sharedMaterial.mainTexture = texture; } #else meshFilter.mesh = mesh; if (shader != null) { var meshRenderer = model.AddComponent <MeshRenderer>(); meshRenderer.material = new Material(shader); meshRenderer.material.name = "material"; meshRenderer.material.mainTexture = texture; } #endif } } return(model); }
public VOXModel CalcVoxelCruncher(VoxData chunk, Color32[] palette) { var crunchers = new List <VOXCruncher>(); var dims = new int[] { chunk.x, chunk.y, chunk.z }; var alloc = System.Math.Max(dims[0], System.Math.Max(dims[1], dims[2])); var mask = new int[alloc * alloc]; var map = chunk.voxels; for (var d = 0; d < 3; ++d) { var u = (d + 1) % 3; var v = (d + 2) % 3; var x = new int[3] { 0, 0, 0 }; var q = new int[3] { 0, 0, 0 }; q[d] = 1; var faces = new VOXVisiableFaces(false, false, false, false, false, false); for (x[d] = -1; x[d] < dims[d];) { var n = 0; for (x[v] = 0; x[v] < dims[v]; ++x[v]) { for (x[u] = 0; x[u] < dims[u]; ++x[u]) { var a = x[d] >= 0 ? map[x[0], x[1], x[2]] : VOXMaterial.MaxValue; var b = x[d] < dims[d] - 1 ? map[x[0] + q[0], x[1] + q[1], x[2] + q[2]] : VOXMaterial.MaxValue; if (a != b) { if (a == VOXMaterial.MaxValue) { mask[n++] = b; } else if (b == VOXMaterial.MaxValue) { mask[n++] = -a; } else { mask[n++] = -b; } } else { mask[n++] = VOXMaterial.MaxValue; } } } ++x[d]; n = 0; for (var j = 0; j < dims[v]; ++j) { for (var i = 0; i < dims[u];) { var c = mask[n]; if (c == VOXMaterial.MaxValue) { ++i; ++n; continue; } var w = 1; var h = 1; var k = 0; for (; (i + w) < dims[u] && c == mask[n + w]; ++w) { } var done = false; for (; (j + h) < dims[v]; ++h) { for (k = 0; k < w; ++k) { if (c != mask[n + k + h * dims[u]]) { done = true; break; } } if (done) { break; } } x[u] = i; x[v] = j; var du = new int[3] { 0, 0, 0 }; var dv = new int[3] { 0, 0, 0 }; du[u] = w; dv[v] = h; var v1 = new Vector3(x[0], x[1], x[2]); var v2 = new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]); v2.x = System.Math.Max(v2.x - 1, 0); v2.y = System.Math.Max(v2.y - 1, 0); v2.z = System.Math.Max(v2.z - 1, 0); if (c > 0) { faces.front = d == 2; faces.back = false; faces.left = d == 0; faces.right = false; faces.top = false; faces.bottom = d == 1; } else { c = -c; faces.front = false; faces.back = d == 2; faces.left = false; faces.right = d == 0; faces.top = d == 1; faces.bottom = false; } crunchers.Add(new VOXCruncher((byte)v1.x, (byte)(v2.x), (byte)(v1.y), (byte)(v2.y), (byte)(v1.z), (byte)(v2.z), faces, c)); for (var l = 0; l < h; ++l) { for (k = 0; k < w; ++k) { mask[n + k + l * dims[u]] = VOXMaterial.MaxValue; } } i += w; n += w; } } } } return(new VOXModel(crunchers)); }