Exemplo n.º 1
0
        public static void LoadVoxelFileAsGameObject(GameObject parent, VoxFileData voxel, int lodLevel, Shader shader)
        {
            try
            {
                var colors  = CreateColor32FromPelatte(voxel.palette.values);
                var texture = CreateTextureFromColor16x16(colors);

                if (lodLevel <= 1)
                {
                    foreach (var chunk in voxel.chunkChild)
                    {
                        var submesh = CreateGameObject("model", chunk.xyzi.voxels, texture, colors, 1, shader);
                        submesh.transform.parent        = parent.transform;
                        submesh.transform.localPosition = Vector3.zero;
                        submesh.transform.localRotation = Quaternion.identity;
                        submesh.transform.localScale    = Vector3.one;
                    }
                }
                else
                {
                    foreach (var chunk in voxel.chunkChild)
                    {
                        for (int lod = 1; lod < lodLevel + 1; lod++)
                        {
                            var submesh = CreateGameObject("lod" + (lod - 1), chunk.xyzi.voxels.GetVoxelDataLOD(lod), texture, colors, lod, shader);
                            submesh.transform.parent = parent.transform;
                        }

                        var lodgroup = parent.AddComponent <LODGroup>();
                        var lods     = lodgroup.GetLODs();

                        for (int i = 0; i < parent.transform.childCount; i++)
                        {
                            lods[i].renderers = new Renderer[] { parent.transform.GetChild(i).GetComponent <MeshRenderer>() }
                        }
                        ;

                        lodgroup.SetLODs(lods);
                    }
                }
            }
            catch (SystemException e)
            {
                Debug.LogException(e);

                throw e;
            }
        }
Exemplo n.º 2
0
        public static VoxFileData Load(byte[] _data)
        {
            Debug.Assert(_data.Length > 0);

            using (MemoryStream stream = new MemoryStream(_data))
            {
                using (var reader = new BinaryReader(stream))
                {
                    VoxFileData voxel = new VoxFileData();
                    voxel.hdr.header  = reader.ReadBytes(4);
                    voxel.hdr.version = reader.ReadInt32();

                    if (voxel.hdr.header[0] != 'V' || voxel.hdr.header[1] != 'O' || voxel.hdr.header[2] != 'X' || voxel.hdr.header[3] != ' ')
                    {
                        throw new System.Exception("Bad Token: magic number is not VOX.");
                    }

                    if (voxel.hdr.version != 150)
                    {
                        throw new System.Exception("The version of file isn't 150 that version of vox, tihs version of file is " + voxel.hdr.version + ".");
                    }

                    voxel.main.name         = reader.ReadBytes(4);
                    voxel.main.chunkContent = reader.ReadInt32();
                    voxel.main.chunkNums    = reader.ReadInt32();

                    if (voxel.main.name[0] != 'M' || voxel.main.name[1] != 'A' || voxel.main.name[2] != 'I' || voxel.main.name[3] != 'N')
                    {
                        throw new System.Exception("Bad Token: token is not MAIN.");
                    }

                    if (voxel.main.chunkContent != 0)
                    {
                        throw new System.Exception("Bad Token: chunk content is " + voxel.main.chunkContent + ", it should be 0.");
                    }

                    if (reader.PeekChar() == 'P')
                    {
                        voxel.pack.name = reader.ReadBytes(4);
                        if (voxel.pack.name[0] != 'P' || voxel.pack.name[1] != 'A' || voxel.pack.name[2] != 'C' || voxel.pack.name[3] != 'K')
                        {
                            throw new System.Exception("Bad Token: token is not PACK");
                        }

                        voxel.pack.chunkContent = reader.ReadInt32();
                        voxel.pack.chunkNums    = reader.ReadInt32();
                        voxel.pack.modelNums    = reader.ReadInt32();

                        if (voxel.pack.modelNums == 0)
                        {
                            throw new System.Exception("Bad Token: model nums must be greater than zero.");
                        }
                    }
                    else
                    {
                        voxel.pack.chunkContent = 0;
                        voxel.pack.chunkNums    = 0;
                        voxel.pack.modelNums    = 1;
                    }

                    voxel.chunkChild = new VoxFileChunkChild[voxel.pack.modelNums];

                    for (int i = 0; i < voxel.pack.modelNums; i++)
                    {
                        var chunk = new VoxFileChunkChild();

                        chunk.size.name         = reader.ReadBytes(4);
                        chunk.size.chunkContent = reader.ReadInt32();
                        chunk.size.chunkNums    = reader.ReadInt32();
                        chunk.size.x            = reader.ReadInt32();
                        chunk.size.y            = reader.ReadInt32();
                        chunk.size.z            = reader.ReadInt32();

                        if (chunk.size.name[0] != 'S' || chunk.size.name[1] != 'I' || chunk.size.name[2] != 'Z' || chunk.size.name[3] != 'E')
                        {
                            throw new System.Exception("Bad Token: token is not SIZE");
                        }

                        if (chunk.size.chunkContent != 12)
                        {
                            throw new System.Exception("Bad Token: chunk content is " + chunk.size.chunkContent + ", it should be 12.");
                        }

                        chunk.xyzi.name = reader.ReadBytes(4);
                        if (chunk.xyzi.name[0] != 'X' || chunk.xyzi.name[1] != 'Y' || chunk.xyzi.name[2] != 'Z' || chunk.xyzi.name[3] != 'I')
                        {
                            throw new System.Exception("Bad Token: token is not XYZI");
                        }

                        chunk.xyzi.chunkContent = reader.ReadInt32();
                        chunk.xyzi.chunkNums    = reader.ReadInt32();
                        if (chunk.xyzi.chunkNums != 0)
                        {
                            throw new System.Exception("Bad Token: chunk nums is " + chunk.xyzi.chunkNums + ", it should be 0.");
                        }

                        var voxelNums = reader.ReadInt32();
                        var voxels    = new byte[voxelNums * 4];
                        if (reader.Read(voxels, 0, voxels.Length) != voxels.Length)
                        {
                            throw new System.Exception("Failed to read voxels");
                        }

                        chunk.xyzi.voxels = new VoxData(voxels, chunk.size.x, chunk.size.y, chunk.size.z);

                        voxel.chunkChild[i] = chunk;
                    }

                    if (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        byte[] palette = reader.ReadBytes(4);
                        if (palette[0] != 'R' || palette[1] != 'G' || palette[2] != 'B' || palette[3] != 'A')
                        {
                            throw new System.Exception("Bad Token: token is not RGBA");
                        }

                        voxel.palette.chunkContent = reader.ReadInt32();
                        voxel.palette.chunkNums    = reader.ReadInt32();

                        var bytePalette = new byte[voxel.palette.chunkContent];
                        reader.Read(bytePalette, 0, voxel.palette.chunkContent);

                        voxel.palette.values = new uint[voxel.palette.chunkContent / 4];

                        for (int i = 4; i < bytePalette.Length; i += 4)
                        {
                            voxel.palette.values[i / 4] = BitConverter.ToUInt32(bytePalette, i - 4);
                        }
                    }
                    else
                    {
                        voxel.palette.values = new uint[_paletteDefault.Length];
                        _paletteDefault.CopyTo(voxel.palette.values, 0);
                    }

                    return(voxel);
                }
            }
        }