Пример #1
0
 public static Unity3DTileIndex LoadFromGLTF(Stream stream)
 {
     using (var sr = new StreamReader(stream))
     {
         return(LoadRaw(GLTFFile.FromJson(sr.ReadToEnd()).DecodeIndex(out string mimeType), mimeType));
     }
 }
Пример #2
0
 public static Unity3DTileIndex LoadFromGLTF(Stream stream, bool compressed)
 {
     using (var sr = new StreamReader(stream))
     {
         byte[] raw = GLTFFile.FromJson(sr.ReadToEnd()).DecodeIndex(out bool overrideCompressed);
         return(LoadFromPPM(new MemoryStream(raw), compressed || overrideCompressed));
     }
 }
Пример #3
0
        public static Unity3DTileIndex LoadFromGLB(Stream stream, bool compressed)
        {
            using (var br = new BinaryReader(stream)) //always reads little endian
            {
                var startPos = br.BaseStream.Position;
                if (br.ReadByte() != 'g' || br.ReadByte() != 'l' || br.ReadByte() != 'T' || br.ReadByte() != 'F')
                {
                    throw new Exception("invalid glb magic");
                }
                UInt32 ver = br.ReadUInt32();
                if (ver != 2)
                {
                    throw new Exception("invalid glb version: " + ver);
                }
                UInt32 len       = br.ReadUInt32();
                byte[] jsonChunk = null, binChunk = null;
                while (br.BaseStream.Position - startPos < len)
                {
                    UInt32 chunkLen = br.ReadUInt32();
                    if (chunkLen > int.MaxValue)
                    {
                        throw new Exception("unsupported glb chunk length: " + chunkLen);
                    }
                    string chunkType = Encoding.ASCII.GetString(br.ReadBytes(4));
                    switch (chunkType)
                    {
                    case "JSON":
                    {
                        if (jsonChunk != null)
                        {
                            throw new Exception("more than one JSON chunk in glb");
                        }
                        jsonChunk = br.ReadBytes((int)chunkLen);
                        break;
                    }

                    case "BIN\0":
                    {
                        if (binChunk != null)
                        {
                            throw new Exception("more than one BIN chunk in glb");
                        }
                        binChunk = br.ReadBytes((int)chunkLen);
                        break;
                    }

                    default: throw new Exception("invalid glb chunk type: " + chunkType);
                    }
                }
                var gltf = GLTFFile.FromJson(Encoding.ASCII.GetString(jsonChunk));
                if (binChunk != null)
                {
                    gltf.Data = binChunk;
                }
                byte[] raw = gltf.DecodeIndex(out bool overrideCompressed);
                return(LoadFromPPM(new MemoryStream(raw), compressed || overrideCompressed));
            }
        }