示例#1
0
        public GltfContainer IntoGlbContainer()
        {
            // Buffer
            List <Types.BufferView> views;
            var bufferBytes = BufferBuilder.BuildBytes(out views);

            if (bufferBytes.Length > 0)
            {
                Gltf.BufferViews = views;
                Gltf.Buffers     = new List <Types.Buffer> {
                    new Types.Buffer {
                        ByteLength = bufferBytes.Length,
                    }
                }; // Index0: references Glb bytes
            }

            var container = new GltfContainer(
                Gltf,
                Gltf.Buffers != null ? new Glb.StoredBuffer
            {
                Payload = new ArraySegment <byte>(bufferBytes),
            } : null);

            return(container);
        }
示例#2
0
        public Importer(GltfContainer container, IResourceLoader loader)
        {
            Container  = container;
            Cache      = new ResourcesCache <int>();
            BufferView = new ResourcesStore(container.Gltf, container.Buffer, loader);

            Nodes     = new NodeImporter(this);
            Meshes    = new MeshImporter(this);
            Materials = new MaterialImporter(this);
            Textures  = new TextureImporter(this);
            Images    = new ImageImporter(this);
        }
示例#3
0
        public void FromGlbTest(string[] modelPath, ModelTester.IModelTester tester)
        {
            var path = modelPath.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));

            using (var fs = new FileStream(path, FileMode.Open))
            {
                var c = GltfContainer.FromGlb(fs);

                var schema = VJson.Schema.JsonSchemaAttribute.CreateFromClass <Types.Gltf>();
                var ex     = schema.Validate(c.Gltf);
                Assert.Null(ex);

                var loader = new ResourceLoaderFromEmbedOnly(); // Glb files should be packed.

                var store = new ResourcesStore(c.Gltf, c.Buffer, loader);
                tester.TestModel(store);
            }
        }
示例#4
0
        public void ReadWriteTest(string[] modelPath)
        {
            var path = modelPath.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));

            Glb.Header header0;
            Glb.Chunk  c0Json;
            Glb.Chunk  c0Buffer;
            using (var fs = new FileStream(path, FileMode.Open))
                using (var r0 = new Glb.Reader(fs))
                {
                    header0  = r0.ReadHeader();
                    c0Json   = r0.ReadChunk();
                    c0Buffer = r0.ReadChunk();
                }

            // re-export glb
            byte[] output;
            using (var fs = new FileStream(path, FileMode.Open))
            {
                var c = GltfContainer.FromGlb(fs);
                using (var ms = new MemoryStream())
                {
                    GltfContainer.ToGlb(ms, c);
                    output = ms.ToArray();
                }
            }

            using (var ms = new MemoryStream(output))
                using (var r1 = new Glb.Reader(ms))
                {
                    var header1 = r1.ReadHeader();
                    Assert.AreEqual(header0.Magic, header1.Magic);
                    Assert.AreEqual(header0.Version, header1.Version);
                    // Assert.AreEqual(header0.Length, header1.Length);

                    var c1Json = r1.ReadChunk();
                    // Assert.AreEqual(c0Json.ChunkLength, c1Json.ChunkLength);
                    Assert.AreEqual(c0Json.ChunkType, c1Json.ChunkType);

                    var c1Buffer = r1.ReadChunk();
                    Assert.AreEqual(c0Buffer.ChunkLength, c1Buffer.ChunkLength);
                    Assert.AreEqual(c0Buffer.ChunkType, c1Buffer.ChunkType);
                }
        }
示例#5
0
        public void FromGltfTest(string[] modelPath, ModelTester.IModelTester tester)
        {
            var path = modelPath.Aggregate("SampleModels", (b, p) => Path.Combine(b, p));

            using (var fs = new FileStream(path, FileMode.Open))
            {
                var c = GltfContainer.FromGltf(fs);

                var schema = VJson.Schema.JsonSchemaAttribute.CreateFromClass <Types.Gltf>();
                var ex     = schema.Validate(c.Gltf);
                Assert.Null(ex);

                var storageDir = Directory.GetParent(path).ToString();
                var loader     = new ResourceLoaderFromFileStorage(storageDir);

                var store = new ResourcesStore(c.Gltf, c.Buffer, loader);
                tester.TestModel(store);
            }
        }
示例#6
0
文件: Writer.cs 项目: magicinn/VGltf
        public static void WriteFromContainer(Stream s, GltfContainer container)
        {
            const uint HeaderSize      = 12;
            const uint ChunkHeaderSize = 8;

            using (var w = new Writer(s))
            {
                uint chunksTotalSize = 0;

                // TODO: make this processes as streamable
                byte[] gltf = null;
                if (container.Gltf == null)
                {
                    throw new NotImplementedException("Json is empty"); // TODO: change type
                }
                using (var cs = new MemoryStream())
                {
                    GltfWriter.Write(cs, container.Gltf);
                    gltf = cs.ToArray();
                }
                chunksTotalSize += ChunkHeaderSize + (uint)gltf.Length;

                var padding = Align.CalcPadding(chunksTotalSize, 4); // Must be 4Bytes aligned
                chunksTotalSize += padding;

                byte[] buffer = null;
                if (container.Buffer != null)
                {
                    using (var cs = new MemoryStream())
                    {
                        var payload = container.Buffer.Payload;
                        cs.Write(payload.Array, payload.Offset, payload.Count);
                        buffer = cs.ToArray();
                    }
                    chunksTotalSize += ChunkHeaderSize + (uint)buffer.Length;
                }

                // Start writing
                w.WriteHeader(new Header
                {
                    Magic   = 0x46546C67,
                    Version = 2,
                    Length  = HeaderSize + chunksTotalSize,
                });

                w.WriteChunk(new Chunk
                {
                    ChunkLength = (uint)gltf.Length + padding,
                    ChunkType   = 0x4E4F534A, // JSON
                    ChunkData   = gltf,
                });
                w._w.Write(new byte[] { 0x20, 0x20, 0x20, 0x20 }, 0, (int)padding); // Add space padding if needed. 0x20 == ' '.

                if (buffer != null)
                {
                    w.WriteChunk(new Chunk
                    {
                        ChunkLength = (uint)buffer.Length,
                        ChunkType   = 0x004E4942, // BIN
                        ChunkData   = buffer,
                    });
                }
            }
        }
示例#7
0
 public Importer(GltfContainer container)
     : this(container, new ResourceLoaderFromEmbedOnly())
 {
 }