Пример #1
0
        public override PackedScene Import(Stream stream, string filePath)
        {
            var info = new GltfInfo(filePath);

            if (filePath.EndsWith(".gltf"))
            {
                byte[] textBytes = new byte[stream.Length - stream.Position];

                stream.Read(textBytes, 0, textBytes.Length);

                HandleGltf(info, textBytes);
            }
            else
            {
                HandleGlb(info, stream);
            }

            //TODO: Add extension support.
            if (info.json.extensionsRequired != null)
            {
                foreach (string requiredExtension in info.json.extensionsRequired)
                {
                    throw new FileLoadException($"glTF Error: Required extension '{requiredExtension}' is not supported.");
                }
            }

            LoadMeshes(info);

            return(info.Scene);
        }
Пример #2
0
        protected static void HandleGltf(GltfInfo info, byte[] textBytes)
        {
            info.json = JsonConvert.DeserializeObject <GltfJson>(Encoding.UTF8.GetString(textBytes));

#if DEBUG
            Directory.CreateDirectory("DebugInfo");
            File.WriteAllText(
                Path.Combine("DebugInfo", "Gltf.json"),
                JsonConvert.SerializeObject(info.json, Formatting.Indented, new JsonSerializerSettings()
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            })
                );
#endif
        }
Пример #3
0
        protected static void HandleGlb(GltfInfo info, Stream stream)
        {
            using var reader = new BinaryReader(stream);

            ReadHeader(reader, out _, out uint length);

            int chunkId = 0;

            while (reader.BaseStream.Position < length)
            {
                //Read Chunk
                uint   chunkLength = reader.ReadUInt32();
                uint   chunkType   = reader.ReadUInt32();
                byte[] chunkData   = reader.ReadBytes((int)chunkLength);

                if (chunkId == 0 && chunkType != ChunkJson)
                {