public void LoadVOXData(byte[] data, bool asIndividualVoxels) { ClearVoxMeshes(); MVMainChunk v = MVImporter.LoadVOXFromData(data); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (paletteToTex) { mat.mainTexture = v.PaletteToTexture(); } if (!asIndividualVoxels) { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } else { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } this.vox = v; } }
public void LoadVOXData(byte[] data, bool asIndividualVoxels, byte[] alphaMaskData = null) { ClearVoxMeshes(); MVVoxelChunk alphaMask = null; if (alphaMaskData != null) { MVMainChunk mc = MVImporter.LoadVOXFromData(alphaMaskData, generateFaces: false); if (mc != null) { alphaMask = mc.voxelChunk; } } MVMainChunk v = MVImporter.LoadVOXFromData(data, alphaMask); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (!asIndividualVoxels) { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } else { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } this.vox = v; } }
public void DrawVoxel() { MVMainChunk chunk = LoadVOX($"Assets\\{path}", false); var texture = new Texture3D(chunk.sizeX, chunk.sizeY, chunk.sizeZ, TextureFormat.RGBAFloat, false); texture.wrapMode = TextureWrapMode.Clamp; texture.filterMode = FilterMode.Point; for (int x = 0; x < chunk.sizeX; x++) { for (int y = 0; y < chunk.sizeY; y++) { for (int z = 0; z < chunk.sizeZ; z++) { var voxel = chunk.voxelChunk.voxels[x, y, z]; var color = chunk.palatte[voxel]; color.a = voxel == 0 ? 0f : 2f; texture.SetPixel(x, y, z, color); // Debug.Log($"{x},{y},{z}: {voxel} - {color}"); } } } texture.Apply(); _vfx.SetTexture("Voxel Texture", texture); _vfx.SetVector3("Voxel Texture Size", new Vector3(texture.width, texture.height, texture.depth)); }
public void DrawVoxel(string filePath) { MVMainChunk chunk = LoadVOX(filePath, false); var paletteTexture = new Texture2D(chunk.palatte.Length, 1, TextureFormat.RGBAFloat, false); paletteTexture.wrapMode = TextureWrapMode.Clamp; paletteTexture.filterMode = FilterMode.Point; for (var i = 0; i < chunk.palatte.Length; i++) { var color = chunk.palatte[i]; paletteTexture.SetPixel(i, 1, color); } paletteTexture.Apply(); var colorArray = new List <Color>(); for (int x = 0; x < chunk.sizeX; x++) { for (int y = 0; y < chunk.sizeY; y++) { for (int z = 0; z < chunk.sizeZ; z++) { var voxel = chunk.voxelChunk.voxels[x, y, z]; if (voxel != 0) { colorArray.Add(new Color(x, y, z, voxel)); } } } } int voxelCount = colorArray.Count; Vector2 voxelTextureDimensions = unpackTexIndex(voxelCount); voxelTextureDimensions.x = voxelTextureDimensions.y < 1?voxelTextureDimensions.x + 1:MAX_TEXTURE_WIDTH; voxelTextureDimensions.y = (int)voxelTextureDimensions.y + 1; var voxelTexture = new Texture2D((int)voxelTextureDimensions.x, (int)voxelTextureDimensions.y, TextureFormat.RGBAFloat, false); voxelTexture.wrapMode = TextureWrapMode.Clamp; voxelTexture.filterMode = FilterMode.Point; for (var i = 0; i < colorArray.Count; i++) { var color = colorArray[i]; Vector2 voxelTextureCoords = unpackTexIndex(i); voxelTexture.SetPixel((int)voxelTextureCoords.x, (int)voxelTextureCoords.y, color); } voxelTexture.Apply(); _vfx.SetTexture("Voxel Texture", voxelTexture); _vfx.SetTexture("Palette Texture", paletteTexture); _vfx.SetInt("Voxel Count", voxelCount); _vfx.SetVector3("Voxel Scene Dimensions", new Vector3(chunk.sizeX, chunk.sizeY, chunk.sizeZ)); _vfx.SetVector2("Voxel Texture Dimensions", voxelTextureDimensions); _vfx.SetInt("Palette Size", paletteTexture.width); }
public void LoadVOXFile(string path, string alphaMaskPath, bool asIndividualVoxels) { ClearVoxMeshes(); if (path != null && path.Length > 0) { MVVoxelChunk alphaChunk = null; if (!string.IsNullOrEmpty(alphaMaskPath)) { MVMainChunk mc = MVImporter.LoadVOX(alphaMaskPath, generateFaces: false); if (mc != null) { alphaChunk = mc.voxelChunk; } } MVMainChunk v = MVImporter.LoadVOX(path, alphaChunk); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (!asIndividualVoxels) { if (meshOrigin != null) { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); } else { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } } else { if (meshOrigin != null) { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); } else { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } } this.vox = v; } } else { Debug.LogError("[MVVoxModel] Invalid file path"); } }
public void LoadVOXFile(string path, bool asIndividualVoxels) { ClearVoxMeshes(); if (path != null && path.Length > 0) { MVMainChunk v = MVImporter.LoadVOX(path); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (paletteToTex) { mat.mainTexture = v.PaletteToTexture(); } if (!asIndividualVoxels) { if (meshOrigin != null) { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); } else { MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } } else { if (meshOrigin != null) { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); } else { MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox); } } this.vox = v; } } else { Debug.LogError("[MVVoxModel] Invalid file path"); } }
public void LoadVOXFile(string path, string alphaMaskPath, bool asIndividualVoxels) { ClearVoxMeshes (); if (path != null && path.Length > 0) { MVVoxelChunk alphaChunk = null; if (!string.IsNullOrEmpty(alphaMaskPath)) { MVMainChunk mc = MVImporter.LoadVOX(alphaMaskPath, generateFaces: false); if(mc != null) alphaChunk = mc.voxelChunk; } MVMainChunk v = MVImporter.LoadVOX (path, alphaChunk); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (!asIndividualVoxels) { if (meshOrigin != null) MVImporter.CreateVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); else MVImporter.CreateVoxelGameObjects (v, this.gameObject.transform, mat, sizePerVox); } else { if (meshOrigin != null) MVImporter.CreateIndividualVoxelGameObjects(v, this.gameObject.transform, mat, sizePerVox, meshOrigin.localPosition); else MVImporter.CreateIndividualVoxelGameObjects (v, this.gameObject.transform, mat, sizePerVox); } this.vox = v; } } else { Debug.LogError ("[MVVoxModel] Invalid file path"); } }
static int ReadSizeChunk(BinaryReader br, MVMainChunk mainChunk) { int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); mainChunk.sizeX = br.ReadInt32(); mainChunk.sizeZ = br.ReadInt32(); mainChunk.sizeY = br.ReadInt32(); mainChunk.voxelChunk = new MVVoxelChunk(); mainChunk.voxelChunk.voxels = new byte[mainChunk.sizeX, mainChunk.sizeY, mainChunk.sizeZ]; Debug.Log(string.Format("[MVImporter] Voxel Size {0}x{1}x{2}", mainChunk.sizeX, mainChunk.sizeY, mainChunk.sizeZ)); if (childrenSize > 0) { br.ReadBytes(childrenSize); Debug.LogWarning("[MVImporter] Nested chunk not supported"); } return(chunkSize + childrenSize + 4 * 3); }
public static GameObject[] CreateIndividualVoxelGameObjects(MVMainChunk chunk, Transform parent, Material mat, float sizePerVox, Vector3 origin) { return(CreateIndividualVoxelGameObjectsForChunk(chunk.voxelChunk, chunk.palatte, parent, mat, sizePerVox, chunk.alphaMaskChunk, origin)); }
public static Mesh[] CreateMeshes(MVMainChunk chunk, float sizePerVox) { return(CreateMeshesFromChunk(chunk.voxelChunk, chunk.palatte, sizePerVox, chunk.alphaMaskChunk)); }
public static MVMainChunk LoadVOXFromData(byte[] data, MVVoxelChunk alphaMask = null, bool generateFaces = true) { using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { MVMainChunk mainChunk = new MVMainChunk(); // "VOX " br.ReadInt32(); // "VERSION " mainChunk.version = br.ReadBytes(4); byte[] chunkId = br.ReadBytes(4); if (chunkId [0] != 'M' || chunkId [1] != 'A' || chunkId [2] != 'I' || chunkId [3] != 'N') { Debug.LogError("[MVImport] Invalid MainChunk ID, main chunk expected"); return(null); } int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); // main chunk should have nothing... skip br.ReadBytes(chunkSize); int readSize = 0; while (readSize < childrenSize) { chunkId = br.ReadBytes(4); if (chunkId [0] == 'S' && chunkId [1] == 'I' && chunkId [2] == 'Z' && chunkId [3] == 'E') { readSize += ReadSizeChunk(br, mainChunk); } else if (chunkId [0] == 'X' && chunkId [1] == 'Y' && chunkId [2] == 'Z' && chunkId [3] == 'I') { readSize += ReadVoxelChunk(br, mainChunk.voxelChunk); } else if (chunkId [0] == 'R' && chunkId [1] == 'G' && chunkId [2] == 'B' && chunkId [3] == 'A') { mainChunk.palatte = new Color[256]; readSize += ReadPalattee(br, mainChunk.palatte); } else { Debug.LogError("[MVImport] Chunk ID not recognized, got " + System.Text.Encoding.ASCII.GetString(chunkId)); return(null); } } mainChunk.alphaMaskChunk = alphaMask; if (generateFaces) { GenerateFaces(mainChunk.voxelChunk, alphaMask); } if (mainChunk.palatte == null) { mainChunk.palatte = MVMainChunk.defaultPalatte; } return(mainChunk); } } }
public static MVMainChunk LoadVOXFromData(byte[] data, MVVoxelChunk alphaMask = null, bool generateFaces = true) { using (MemoryStream ms = new MemoryStream (data)) { using (BinaryReader br = new BinaryReader (ms)) { MVMainChunk mainChunk = new MVMainChunk (); // "VOX " br.ReadInt32 (); // "VERSION " mainChunk.version = br.ReadBytes(4); byte[] chunkId = br.ReadBytes (4); if (chunkId [0] != 'M' || chunkId [1] != 'A' || chunkId [2] != 'I' || chunkId [3] != 'N') { Debug.LogError ("[MVImport] Invalid MainChunk ID, main chunk expected"); return null; } int chunkSize = br.ReadInt32 (); int childrenSize = br.ReadInt32 (); // main chunk should have nothing... skip br.ReadBytes (chunkSize); int readSize = 0; while (readSize < childrenSize) { chunkId = br.ReadBytes (4); if (chunkId [0] == 'S' && chunkId [1] == 'I' && chunkId [2] == 'Z' && chunkId [3] == 'E') { readSize += ReadSizeChunk (br, mainChunk); } else if (chunkId [0] == 'X' && chunkId [1] == 'Y' && chunkId [2] == 'Z' && chunkId [3] == 'I') { readSize += ReadVoxelChunk (br, mainChunk.voxelChunk); } else if (chunkId [0] == 'R' && chunkId [1] == 'G' && chunkId [2] == 'B' && chunkId [3] == 'A') { mainChunk.palatte = new Color[256]; readSize += ReadPalattee (br, mainChunk.palatte); } else { Debug.LogError ("[MVImport] Chunk ID not recognized, got " + System.Text.Encoding.ASCII.GetString(chunkId)); return null; } } mainChunk.alphaMaskChunk = alphaMask; if (generateFaces) GenerateFaces(mainChunk.voxelChunk, alphaMask); if (mainChunk.palatte == null) mainChunk.palatte = MVMainChunk.defaultPalatte; return mainChunk; } } }
public static GameObject[] CreateVoxelGameObjects(MVMainChunk chunk, Transform parent, Material mat, float sizePerVox) { return(CreateVoxelGameObjectsForChunk(chunk.voxelChunk, chunk.palatte, parent, mat, sizePerVox)); }
static int ReadSizeChunk(BinaryReader br, MVMainChunk mainChunk) { int chunkSize = br.ReadInt32 (); int childrenSize = br.ReadInt32 (); mainChunk.sizeX = br.ReadInt32 (); mainChunk.sizeZ = br.ReadInt32 (); mainChunk.sizeY = br.ReadInt32 (); mainChunk.voxelChunk = new MVVoxelChunk (); mainChunk.voxelChunk.voxels = new byte[mainChunk.sizeX, mainChunk.sizeY, mainChunk.sizeZ]; Debug.Log (string.Format ("[MVImporter] Voxel Size {0}x{1}x{2}", mainChunk.sizeX, mainChunk.sizeY, mainChunk.sizeZ)); if (childrenSize > 0) { br.ReadBytes (childrenSize); Debug.LogWarning ("[MVImporter] Nested chunk not supported"); } return chunkSize + childrenSize + 4 * 3; }
public void LoadVOXData(byte[] data, bool asIndividualVoxels, byte[] alphaMaskData = null) { ClearVoxMeshes (); MVVoxelChunk alphaMask = null; if(alphaMaskData != null) { MVMainChunk mc = MVImporter.LoadVOXFromData(alphaMaskData, generateFaces: false); if (mc != null) alphaMask = mc.voxelChunk; } MVMainChunk v = MVImporter.LoadVOXFromData(data, alphaMask); if (v != null) { Material mat = (this.voxMaterial != null) ? this.voxMaterial : MVImporter.DefaultMaterial; if (!asIndividualVoxels) { MVImporter.CreateVoxelGameObjects (v, this.gameObject.transform, mat, sizePerVox); } else { MVImporter.CreateIndividualVoxelGameObjects (v, this.gameObject.transform, mat, sizePerVox); } this.vox = v; } }
public static GameObject[] CreateIndividualVoxelGameObjects(MVMainChunk chunk, Transform parent, Material mat, float sizePerVox, Vector3 origin) { return CreateIndividualVoxelGameObjectsForChunk(chunk.voxelChunk, chunk.palatte, parent, mat, sizePerVox, chunk.alphaMaskChunk, origin); }
public static Mesh[] CreateMeshes(MVMainChunk chunk, float sizePerVox) { return CreateMeshesFromChunk(chunk.voxelChunk, chunk.palatte, sizePerVox, chunk.alphaMaskChunk); }