Example #1
0
        public Scene Read()
        {
            //The 12-byte header consists of three 4-byte entries:
            _header = new GlbHeader();
            //magic equals 0x46546C67. It is ASCII string glTF, and can be used to identify data as Binary glTF.
            _header.Magic = _stream.ReadUInt <LittleEndianConverter>();
            //version indicates the version of the Binary glTF container format. This specification defines version 2.
            _header.Version = _stream.ReadUInt <LittleEndianConverter>();
            //length is the total length of the Binary glTF, including Header and all Chunks, in bytes.
            _header.Length = _stream.ReadUInt <LittleEndianConverter>();

            if (_header.Version != 2)
            {
                throw new NotImplementedException($"Version {_header.Version} not implemented");
            }

            //Chunk 0 Json
            uint   jsonChunkLength = _stream.ReadUInt <LittleEndianConverter>();
            string jsonChunkType   = _stream.ReadString(4);

            if (jsonChunkType != "JSON")
            {
                throw new GltfReaderException("Chunk type does not match", _stream.Position);
            }

            _root = JsonConvert.DeserializeObject <GltfRoot>(_stream.ReadString((int)jsonChunkLength));

            //Chunk 1 bin
            uint   binChunkLength = _stream.ReadUInt <LittleEndianConverter>();
            string binChunkType   = _stream.ReadString(4);

            //Check the chunk type
            if (binChunkType != "BIN\0")
            {
                throw new GltfReaderException("Chunk type does not match", _stream.Position);
            }

            byte[] binChunk = _stream.ReadBytes((int)binChunkLength);
            _binaryStream = new StreamIO(binChunk);

            return(GltfBinaryReaderBase.GetBynaryReader((int)_header.Version, _root, binChunk).Read());
        }
Example #2
0
        protected List <T> readAccessor <T>(StreamIO stream, GltfAccessor.ComponentTypeEnum componentType, int count, int nargs)
        {
            List <T> vecs = new List <T>();

            for (int i = 0; i < count; i++)
            {
                List <object> args = new List <object>();

                for (int j = 0; j < nargs; j++)
                {
                    switch (componentType)
                    {
                    case GltfAccessor.ComponentTypeEnum.BYTE:
                    case GltfAccessor.ComponentTypeEnum.UNSIGNED_BYTE:
                        args.Add(stream.ReadByte());
                        break;

                    case GltfAccessor.ComponentTypeEnum.SHORT:
                        args.Add(stream.ReadShort());
                        break;

                    case GltfAccessor.ComponentTypeEnum.UNSIGNED_SHORT:
                        args.Add(stream.ReadUShort());
                        break;

                    case GltfAccessor.ComponentTypeEnum.UNSIGNED_INT:
                        args.Add(stream.ReadUInt());
                        break;

                    case GltfAccessor.ComponentTypeEnum.FLOAT:
                        args.Add(stream.ReadSingle());
                        break;

                    default:
                        throw new Exception();
                    }
                }

                vecs.Add((T)Activator.CreateInstance(typeof(T), args.ToArray()));
            }

            return(vecs);
        }