public override void ReadVolxel(MVChunk chunk, int x, int y, int z, byte index) { Block block = chunk.GetBlock(x, y, z); if (index == 0) { block.Color.a = 255; } else { block.Color.a = index; } chunk.SetBlock(x, y, z, block); }
public static void LoadVOXFromFile(string path, MVChunk chunk, MVLayer layer) { byte[] bytes = File.ReadAllBytes(path); if (bytes[0] != 'V' || bytes[1] != 'O' || bytes[2] != 'X' || bytes[3] != ' ') { Debug.LogError("Invalid VOX file, magic number mismatch"); return; } LoadVOXFromData(chunk, layer, bytes); }
public override void LoadChunk(Chunk chunk) { MVChunk mvChunk = chunk as MVChunk; if (mvChunk == null) { Debug.LogError("chunk is not a MVChunk, cannot load from a vox file"); return; } for (int i = 0; i < Layers.Count; i++) { LoadVOXFromFile(Layers[i].FilePath, mvChunk, Layers[i]); } }
public override void ReadVolxel(MVChunk chunk, int x, int y, int z, byte index) { Block block = chunk.GetBlock(x, y, z); block.Type = Block.BlockTypes.Solid; if (chunk.Palette.Length > index - 1) { block.Color = chunk.Palette[index - 1]; } else { block.Color = Color.magenta; Debug.LogWarningFormat("MVChunk \"{0}\" palette has no color at index {1} (Array index {2})", chunk.Name, index, index - 1); } chunk.UpdateColorIndex(x, y, z, index); chunk.SetBlock(x, y, z, block); }
private static int ReadPalette(BinaryReader br, MVChunk chunk, MVLayer layer) { int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); for (int i = 0; i < 256; ++i) { layer.ReadPalette(chunk, i, (float)br.ReadByte() / 255.0f, (float)br.ReadByte() / 255.0f, (float)br.ReadByte() / 255.0f, (float)br.ReadByte() / 255.0f); } if (childrenSize > 0) { br.ReadBytes(childrenSize); Debug.LogWarning("[VoxLoader] Nested chunk not supported"); } return(chunkSize + childrenSize + 4 * 3); }
private static int ReadSizeChunk(BinaryReader br, MVChunk chunk, MVLayer layer) { int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); int sizeX = br.ReadInt32(); int sizeZ = br.ReadInt32(); int sizeY = br.ReadInt32(); layer.ReadSize(chunk, sizeX, sizeY, sizeZ); if (childrenSize > 0) { br.ReadBytes(childrenSize); Debug.LogWarning("[VoxLoader] Nested chunk not supported"); } return(chunkSize + childrenSize + 4 * 3); }
private void Start() { pool = GetComponent <ObjectPool>(); MVLoader loader = new MVLoader(); loader.Layers.Add(new MVModelLayer(@"Z:\Fab\Programmation\Voxel\MagicaVoxel\vox\Head3.vox")); loader.Layers.Add(new MVAlphaLayer(@"Z:\Fab\Programmation\Voxel\MagicaVoxel\vox\Head3_AM.vox")); //Texture2D tex = new Texture2D(256, 1); //tex.LoadImage(System.IO.File.ReadAllBytes(@"Z:\Fab\Programmation\Voxel\MagicaVoxel\export\AlternatePalette.png")); ChunkMeshBuilder builder = new GreedyMeshBuilder(); _Loader = loader; _Unloader = new SimpleUnloader(); _MeshBuilder = builder; chunk = new MVChunk(this); Load(chunk); //chunk.LoadPalette(tex); Build(chunk); List <GameObject> GOs = new List <GameObject>(); for (int i = 0; i < chunk.MeshData.Length; i++) { GameObject go = pool.NextObject(); if (go == null) { Debug.LogError("Pool has no GameObject available"); break; } AttachMesh(go, chunk, chunk.MeshData[i]); go.transform.localPosition = Vector3.zero; GOs.Add(go); } chunk.GameObjects = GOs.ToArray(); }
private static int ReadVoxelChunk(BinaryReader br, MVChunk chunk, MVLayer layer) { int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); int numVoxels = br.ReadInt32(); for (int i = 0; i < numVoxels; ++i) { int x = (int)br.ReadByte(); int z = (int)br.ReadByte(); int y = (int)br.ReadByte(); layer.ReadVolxel(chunk, x, y, z, br.ReadByte()); } if (childrenSize > 0) { br.ReadBytes(childrenSize); Debug.LogWarning("[VoxLoader] Nested chunk not supported"); } return(chunkSize + childrenSize + 4 * 3); }
public override void ReadVersion(MVChunk chunk, byte[] version) { chunk.Version = version; }
public override void InitDefaultPalette(MVChunk chunk) { chunk.Palette = MVChunk.DefaultPalette; }
public override void InitPalette(MVChunk chunk) { chunk.Palette = new Color[256]; }
public override void ReadSize(MVChunk chunk, int sizeX, int sizeY, int sizeZ) { }
public abstract void InitDefaultPalette(MVChunk chunk);
public abstract void ReadSize(MVChunk chunk, int sizeX, int sizeY, int sizeZ);
public override void InitDefaultPalette(MVChunk chunk) { }
private static void LoadVOXFromData(MVChunk chunk, MVLayer layer, byte[] data) { if (chunk.Palette == null) { layer.InitDefaultPalette(chunk); } using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { // "VOX " br.ReadInt32(); // "VERSION " layer.ReadVersion(chunk, br.ReadBytes(4)); byte[] chunkId = br.ReadBytes(4); if (chunkId[0] != 'M' || chunkId[1] != 'A' || chunkId[2] != 'I' || chunkId[3] != 'N') { Debug.LogError("[VoxLoader] Invalid MainChunk ID, main chunk expected"); return; } 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, chunk, layer); } else if (chunkId[0] == 'X' && chunkId[1] == 'Y' && chunkId[2] == 'Z' && chunkId[3] == 'I') { readSize += ReadVoxelChunk(br, chunk, layer); } else if (chunkId[0] == 'R' && chunkId[1] == 'G' && chunkId[2] == 'B' && chunkId[3] == 'A') { layer.InitPalette(chunk); readSize += ReadPalette(br, chunk, layer); } else { Debug.LogError("[VoxLoader] Chunk ID not recognized, got " + System.Text.Encoding.ASCII.GetString(chunkId)); return; } } } } }
public abstract void ReadVersion(MVChunk chunk, byte[] version);
public override void ReadPalette(MVChunk chunk, int index, float r, float g, float b, float a) { chunk.Palette[index] = new Color(r, g, b, a); }
public abstract void ReadVolxel(MVChunk chunk, int x, int y, int z, byte index);
public override void ReadSize(MVChunk chunk, int sizeX, int sizeY, int sizeZ) { chunk.InitBlocks(sizeX, sizeY, sizeZ); }
public abstract void ReadPalette(MVChunk chunk, int index, float r, float g, float b, float a);
public override void ReadPalette(MVChunk chunk, int index, float r, float g, float b, float a) { }