private void ImportAssetOptionsQEObject(string aFilename) { if (Path.GetExtension(aFilename).ToUpper() != ".qb".ToUpper()) { Debug.LogError("Extension must be .qb"); return; } //Consts uint CODEFLAG = 2; uint NEXTSLICEFLAG = 6; //Parent Gameobject GameObject parentGameObject = new GameObject(); parentGameObject.name = Path.GetFileNameWithoutExtension(aFilename); BinaryReader br = new BinaryReader(File.Open(aFilename, FileMode.Open)); try { if (br == null) { Debug.Log("VoxelMax: Could not open file. Filename: " + aFilename); return; } uint fileVersion = br.ReadUInt32(); uint colorFormat = br.ReadUInt32(); uint zAxisOrientation = br.ReadUInt32(); uint compressed = br.ReadUInt32(); uint visibilityMaskEncoded = br.ReadUInt32(); uint numMatrices = br.ReadUInt32(); Debug.Log("Qubicle File Import Info\r\n" + "File Version: " + fileVersion + "\r\n" + "Color Format: " + colorFormat + "\r\n" + "zAxisOrientation: " + zAxisOrientation + "\r\n" + "visibilityMaskEncoded: " + visibilityMaskEncoded + "\r\n" + "Object Count: " + numMatrices); for (uint i = 0; i < numMatrices; i++) { // read matrix name byte nameLength = br.ReadByte(); string matrixName = new string(br.ReadChars(nameLength)); GameObject newGameObject = new GameObject(); newGameObject.transform.parent = parentGameObject.transform; newGameObject.name = matrixName; VoxelContainer newVoxelContainer = newGameObject.AddComponent <VoxelContainer>(); // read matrix size uint sizeX = br.ReadUInt32(); uint sizeY = br.ReadUInt32(); uint sizeZ = br.ReadUInt32(); // read matrix position (in this example the position is irrelevant) int posX = br.ReadInt32(); int posY = br.ReadInt32(); int posZ = br.ReadInt32(); newGameObject.transform.position = new Vector3(posX, posY, posZ); if (compressed == 0) // if uncompressd { for (uint z = 0; z < sizeZ; z++) { for (uint y = 0; y < sizeY; y++) { for (uint x = 0; x < sizeX; x++) { uint curColor = br.ReadUInt32(); if (curColor != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(curColor); newVoxelContainer.AddVoxel(newVoxel, false); } } } } } else // if compressed { uint z = 0; while (z < sizeZ) { z++; uint index = 0; while (true) { uint data = br.ReadUInt32(); if (data == NEXTSLICEFLAG) { break; } else if (data == CODEFLAG) { uint count = br.ReadUInt32(); data = br.ReadUInt32(); for (uint j = 0; j < count; j++) { uint x = index % sizeX + 1; // mod = modulo e.g. 12 mod 8 = 4 uint y = index / sizeX + 1; // div = integer division e.g. 12 div 8 = 1 index++; if (data != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(data); newVoxelContainer.AddVoxel(newVoxel, false); } } } else { uint x = index % sizeX + 1; uint y = index / sizeX + 1; index++; if (data != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(data); newVoxelContainer.AddVoxel(newVoxel, false); } } } } } newVoxelContainer.UpdateStructure(); newVoxelContainer.BuildMesh(true, true, true, true); } } finally { br.Close(); } }
private void LoadVoxFile(string aFileName) { //I dissabled compiler warning, because we want to load some values from the file even if we would not use it later. //I think it is cleaner this way #pragma warning disable //Prepare Object GameObject newGameObject = new GameObject(); newGameObject.name = "ImportedFromMagicaVoxel"; VoxelContainer newVoxelContainer = newGameObject.AddComponent <VoxelContainer>(); //Open File BinaryReader br = new BinaryReader(File.OpenRead(aFileName)); string magic = new string(br.ReadChars(4)); int version = br.ReadInt32(); if (magic == "VOX ") { int sizex = 0, sizey = 0, sizez = 0; Color[] colors = null; MagicaVoxelData[] voxelData = null; while (br.BaseStream.Position < br.BaseStream.Length) { char[] chunkId = br.ReadChars(4); int chunkSize = br.ReadInt32(); int childChunks = br.ReadInt32(); string chunkName = new string(chunkId); if (chunkName == "SIZE") { sizex = br.ReadInt32(); sizey = br.ReadInt32(); sizez = br.ReadInt32(); br.ReadBytes(chunkSize - 4 * 3); } else if (chunkName == "XYZI") { int numVoxels = br.ReadInt32(); voxelData = new MagicaVoxelData[numVoxels]; for (int i = 0; i < voxelData.Length; i++) { voxelData[i] = new MagicaVoxelData(br); } } else if (chunkName == "RGBA") { colors = new Color[256]; for (int i = 0; i < 256; i++) { byte r = br.ReadByte(); byte g = br.ReadByte(); byte b = br.ReadByte(); byte a = br.ReadByte(); colors[i].r = r / 255f; colors[i].g = g / 255f; colors[i].b = b / 255f; colors[i].a = a / 255f; } } else { br.ReadBytes(chunkSize); } } if ((voxelData == null) || (voxelData.Length == 0)) { return; } for (int i = 0; i < voxelData.Length; i++) { Vector3 position = new Vector3(voxelData[i].x, voxelData[i].z, voxelData[i].y); if (!newVoxelContainer.voxels.ContainsKey(position)) { Voxel newVoxel = new Voxel(); newVoxel.position = position; newVoxel.color = (colors == null ? UShortToColor(voxColors[voxelData[i].color - 1]) : colors[voxelData[i].color - 1]); newVoxelContainer.AddVoxel(newVoxel, false); } } newVoxelContainer.UpdateStructure(); newVoxelContainer.BuildMesh(true, true, true, true); } else { Debug.LogError("Error durring vox import. Probably this is not a .vox file."); return; } #pragma warning restore }