public static void CreateVoxels(MVVoxel[] voxels, MVVoxModel voxModel) { MVVoxelChunk chunk = voxModel.vox.voxelChunk; float sizePerVox = voxModel.sizePerVox; float cx = sizePerVox * chunk.sizeX / 2; float cy = sizePerVox * chunk.sizeY / 2; float cz = sizePerVox * chunk.sizeZ / 2; Material mat = (voxModel.voxMaterial != null) ? voxModel.voxMaterial : MVImporter.DefaultMaterial; List <GameObject> objects = new List <GameObject> (); foreach (MVVoxel voxel in voxels) { float px = voxel.x * sizePerVox - cx, py = voxel.y * sizePerVox - cy, pz = voxel.z * sizePerVox - cz; GameObject go = MVImporter.CreateGameObject(voxModel.gameObject.gameObject.transform, new Vector3(px, py, pz), string.Format("Voxel ({0}, {1}, {2})", voxel.x, voxel.y, voxel.z), MVImporter.CubeMeshWithColor(sizePerVox, voxModel.vox.palatte [chunk.voxels [voxel.x, voxel.y, voxel.z] - 1]), mat); MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> (); v.voxel = new MVVoxel() { x = voxel.x, y = voxel.y, z = voxel.z, colorIndex = chunk.voxels [voxel.x, voxel.y, voxel.z] }; objects.Add(go); } Selection.objects = objects.ToArray(); }
public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, MVVoxelChunk alphaMask, Vector3 origin) { List <GameObject> result = new List <GameObject> (); if (alphaMask != null && (alphaMask.sizeX != chunk.sizeX || alphaMask.sizeY != chunk.sizeY || alphaMask.sizeZ != chunk.sizeZ)) { Debug.LogErrorFormat("Unable to create meshes from chunk : Chunk's size ({0},{1},{2}) differs from alphaMask chunk's size ({3},{4},{5})", chunk.sizeX, chunk.sizeY, chunk.sizeZ, alphaMask.sizeX, alphaMask.sizeY, alphaMask.sizeZ); return(result.ToArray()); } for (int x = 0; x < chunk.sizeX; ++x) { for (int y = 0; y < chunk.sizeY; ++y) { for (int z = 0; z < chunk.sizeZ; ++z) { if (chunk.voxels [x, y, z] != 0) { float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox; Color c = palatte [chunk.voxels [x, y, z] - 1]; if (alphaMask != null && alphaMask.voxels[x, y, z] != 0) { c.a = (float)(alphaMask.voxels [x, y, z] - 1) / 255; } GameObject go = CreateGameObject( parent, new Vector3(px, py, pz), string.Format("Voxel ({0}, {1}, {2})", x, y, z), MVImporter.CubeMeshWithColor(sizePerVox, c), mat); MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> (); v.voxel = new MVVoxel() { x = (byte)x, y = (byte)y, z = (byte)z, colorIndex = chunk.voxels [x, y, z] }; result.Add(go); } } } } return(result.ToArray()); }
public static void CombineVoxels(MVVoxModelVoxel[] voxels) { if (voxels != null && voxels.Length > 0) { MVVoxelChunk chunk = new MVVoxelChunk (); MVVoxModel model = voxels [0].parentVoxModel; MVVoxelChunk origChunk = model.vox.voxelChunk; chunk.voxels = new byte[origChunk.sizeX, origChunk.sizeY, origChunk.sizeZ]; foreach (MVVoxModelVoxel v in voxels) { chunk.voxels [v.voxel.x, v.voxel.y, v.voxel.z] = v.voxel.colorIndex; } MVImporter.GenerateFaces(chunk, model.vox.alphaMaskChunk); Mesh[] meshes = MVImporter.CreateMeshesFromChunk(chunk, model.vox.palatte, model.sizePerVox, model.vox.alphaMaskChunk); if (meshes.Length > 1) { Debug.LogError ("[MVCombine] Currently does not support combining voxels into multiple meshes, please reduce the number of voxels you are trying to combine"); return; } Material mat = (model.voxMaterial != null) ? model.voxMaterial : MVImporter.DefaultMaterial; int index = 0; foreach (Mesh mesh in meshes) { GameObject go = MVImporter.CreateGameObject (model.gameObject.transform, Vector3.zero, string.Format ("VoxMesh ({0})", index), mesh, mat); MVVoxModelMesh voxMesh = go.AddComponent<MVVoxModelMesh> (); voxMesh.voxels = voxels.Select( o => o.voxel ).ToArray(); Selection.activeGameObject = go; index++; } foreach (MVVoxModelVoxel v in voxels) { GameObject.DestroyImmediate (v.gameObject); } } else { Debug.LogError("[MVCombine] Invalid voxels"); } }
public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, Vector3 origin) { List <GameObject> result = new List <GameObject> (); for (int x = 0; x < chunk.sizeX; ++x) { for (int y = 0; y < chunk.sizeY; ++y) { for (int z = 0; z < chunk.sizeZ; ++z) { if (chunk.voxels [x, y, z] != 0) { float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox; int cidx = chunk.voxels[x, y, z]; GameObject go = CreateGameObject( parent, new Vector3(px, py, pz), string.Format("Voxel ({0}, {1}, {2})", x, y, z), MVImporter.CubeMeshWithColor(sizePerVox, palatte[cidx - 1], cidx), mat); #if UNITY_EDITOR MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> (); v.voxel = new MVVoxel() { x = (byte)x, y = (byte)y, z = (byte)z, colorIndex = chunk.voxels [x, y, z] }; #endif result.Add(go); } } } } return(result.ToArray()); }