public static byte[] TileToVox(VTile tile) { List <byte> data = new List <byte>(); Add(data, (byte)'V', (byte)'O', (byte)'X', (byte)' '); Add(data, System.BitConverter.GetBytes(150)); Add(data, (byte)'M', (byte)'A', (byte)'I', (byte)'N'); Add(data, System.BitConverter.GetBytes(0)); int mainChildSizeIndex = data.Count; Add(data, (byte)'S', (byte)'I', (byte)'Z', (byte)'E'); Add(data, System.BitConverter.GetBytes(12)); Add(data, System.BitConverter.GetBytes(0)); Add(data, System.BitConverter.GetBytes(tile.GetWidth())); Add(data, System.BitConverter.GetBytes(tile.GetHeight())); Add(data, System.BitConverter.GetBytes(tile.GetDepth())); Add(data, (byte)'X', (byte)'Y', (byte)'Z', (byte)'I'); int xyziContentSizeIndex = data.Count; Add(data, System.BitConverter.GetBytes(0)); int numVoxelsIndex = data.Count; int numVoxels = 0; for (int x = 0; x < tile.GetWidth(); x++) { for (int y = 0; y < tile.GetHeight(); y++) { for (int z = 0; z < tile.GetDepth(); z++) { int index = GetIndex(tile, x, y, z); if (index != 0) { Add(data, (byte)x, (byte)(tile.GetHeight() - z - 1), (byte)y, (byte)(255 - index + 1)); numVoxels++; } } } } data.InsertRange(numVoxelsIndex, System.BitConverter.GetBytes(numVoxels)); data.InsertRange(xyziContentSizeIndex, System.BitConverter.GetBytes(data.Count - xyziContentSizeIndex - 4)); Add(data, (byte)'R', (byte)'G', (byte)'B', (byte)'A'); Add(data, System.BitConverter.GetBytes(256 * 4)); Add(data, System.BitConverter.GetBytes(0)); for (int i = 255; i >= 0; i--) { VColor c; if (i >= tile.GetPalette().GetCount()) { c = new VColor(255, 0, 255, 255, 0, 0, 255, 0); } else { c = tile.GetPalette().GetColor(i); } Add(data, c.r, c.g, c.b, c.a); } data.InsertRange(mainChildSizeIndex, System.BitConverter.GetBytes(data.Count - mainChildSizeIndex)); return(data.ToArray()); }
static VColor GetColor(VTile tile, int x, int y, int z) { colorIndexTemp = GetIndex(tile, x, y, z); if (colorIndexTemp >= tile.GetPalette().GetCount()) { return(new VColor(255, 0, 255, 255, 0, 0, 255, 0)); } else { return(tile.GetPalette().GetColor(colorIndexTemp)); } }
void LateUpdate() { VTile t = tile.GetTile(); VLayer l = t.GetLayer(layerIndex); VAnimation a = t.GetAnimation(animationIndex); bool active = layerIndex == t.GetLayerIndex() && animationIndex == t.GetAnimationIndex() && frameIndex == t.GetFrameIndex(); cachedVChunk = t.GetChunk(layerIndex, animationIndex, frameIndex); if (GetChunk().IsDirty() || t.GetPalette().IsDirty() || l.IsDirty() || a.IsDirty()) { Refresh(); } bool visible = l.GetVisible() && animationIndex == t.GetAnimationIndex() && frameIndex == t.GetFrameIndex(); gameObject.layer = (visible && (active || (!l.GetOutline() && !l.GetTransparent()))) ? 10 : 0; if (Tool.editing && active) { visible = false; } if (mr) { mr.enabled = visible; } }
public void RefreshPalette() { VTile tile = GetTile(); Color32[] cs0 = new Color32[256]; Color32[] cs1 = new Color32[256]; for (int i = 0; i < tile.GetPalette().GetCount(); i++) { VColor vc = tile.GetPalette().GetColor(i); cs0[i] = new Color32(vc.r, vc.g, vc.b, vc.a); cs1[i] = new Color32(vc.m, vc.s, vc.e, vc.u); } for (int i = tile.GetPalette().GetCount(); i < 256; i++) { cs0[i] = new Color32(255, 0, 255, 255); cs1[i] = new Color32(0, 0, 255, 0); } tex0.SetPixels32(cs0); tex0.Apply(); tex1.SetPixels32(cs1); tex1.Apply(); }
void LateUpdate() { VTile t = tile.GetTile(); cachedVChunk = chunk; if (chunk.GetWidth() != t.GetWidth() || chunk.GetHeight() != t.GetHeight() || chunk.GetDepth() != t.GetDepth()) { chunk.Resize(t.GetWidth(), t.GetHeight(), t.GetDepth()); } if (GetChunk().IsDirty() || t.GetPalette().IsDirty()) { Refresh(); } mr.enabled = Tool.editing; }
public static VTile VoxToTile(byte[] vox) { VTile tile = new VTile(); VPalette pal = tile.GetPalette(); for (int j = 0; j < 256; j++) { byte[] bits = System.BitConverter.GetBytes(vox_default_palette[j]); VColor c = new VColor(bits[0], bits[1], bits[2], bits[3]); if (j >= pal.GetCount()) { pal.AddColor(c); } else { pal.SetColor(j, c); } } int i = 0; string chunk = "" + (char)vox[i + 0] + (char)vox[i + 1] + (char)vox[i + 2] + (char)vox[i + 3]; i += 4; if (chunk != "VOX ") { throw new System.Exception("Invalid VOX file"); } int version = System.BitConverter.ToInt32(vox, i); i += 4; if (version != 150) { throw new System.Exception("Unsupported VOX version (expected 150, got " + version + ")"); } while (i < vox.Length) { chunk = "" + (char)vox[i + 0] + (char)vox[i + 1] + (char)vox[i + 2] + (char)vox[i + 3]; i += 4; int contentLength = System.BitConverter.ToInt32(vox, i); i += 4; //int childrenLength = System.BitConverter.ToInt32(vox, i); i += 4; if (chunk == "MAIN") { } else if (chunk == "PACK") { int numModels = System.BitConverter.ToInt32(vox, i); i += 4; if (numModels > 1) { throw new System.Exception("Unsupported VOX feature (cannot read multi-model pack files)"); } } else if (chunk == "SIZE") { int x = System.BitConverter.ToInt32(vox, i); i += 4; int y = System.BitConverter.ToInt32(vox, i); i += 4; int z = System.BitConverter.ToInt32(vox, i); i += 4; tile.Resize(x, z, y); } else if (chunk == "XYZI") { int count = System.BitConverter.ToInt32(vox, i); i += 4; for (int j = 0; j < count; j++) { byte x = vox[i + 0]; byte y = vox[i + 1]; byte z = vox[i + 2]; byte c = vox[i + 3]; tile.GetChunk(0, 0, 0).SetPaletteIndexAt(x, z, tile.GetDepth() - y - 1, (byte)(255 - c + 1)); i += 4; } } else if (chunk == "RGBA") { for (int j = 0; j < 256; j++) { VColor c = new VColor(vox[i + 0], vox[i + 1], vox[i + 2], vox[i + 3]); i += 4; // Palette color 0 is always transparent if (j == 255) { c.a = 0; } if (j >= pal.GetCount()) { pal.AddColor(c); } else { pal.SetColor(255 - j, c); } } } else { i += contentLength; } } return(tile); }