public static void CreateCubeMesh16x16(VOXCruncher it, ref Vector3[] vertices, ref Vector3[] normals, ref Vector2[] uv, ref int[] triangles, ref int index, float scaling) { Vector3 pos; pos.x = (it.begin.x + it.end.x + 1) * 0.5f * scaling; pos.y = (it.begin.y + it.end.y + 1) * 0.5f * scaling; pos.z = (it.begin.z + it.end.z + 1) * 0.5f * scaling; Vector3 scale; scale.x = (it.end.x + 1 - it.begin.x) * scaling; scale.y = (it.end.y + 1 - it.begin.y) * scaling; scale.z = (it.end.z + 1 - it.begin.z) * scaling; VOXModel.CreateCubeMesh16x16(ref vertices, ref normals, ref uv, ref triangles, ref index, it.faces, pos, scale, (uint)it.material); }
public static GameObject CreateGameObject(string name, VoxData data, Texture2D texture, Color32[] colors, float scale) { 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) { VOXModel.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>(); var meshRenderer = model.AddComponent <MeshRenderer>(); #if UNITY_EDITOR MeshUtility.Optimize(mesh); meshFilter.sharedMesh = mesh; meshRenderer.sharedMaterial = new Material(Shader.Find("Mobile/Diffuse")); meshRenderer.sharedMaterial.name = "material"; meshRenderer.sharedMaterial.mainTexture = texture; #else meshFilter.mesh = mesh; meshRenderer.material = new Material(Shader.Find("Mobile/Diffuse")); meshRenderer.material.mainTexture = texture; #endif } } return(model); }