Exemplo n.º 1
0
        private void CopyImages(Gltf gltfFile, string gltfPath, string outputFolder)
        {
            for (var i = 0; i < gltfFile.Images.Length; i++)
            {
                var image = gltfFile.Images[i];

                var outputPath = Path.Combine(outputFolder, image.Uri);

                Log.WriteLine($"Copying image {outputPath}...");

                // TODO: Supported embedded image buffers
                using (var inputStream = gltfFile.OpenImageFile(i, gltfPath))
                    using (var outputStream = File.OpenWrite(outputPath))
                    {
                        inputStream.CopyTo(outputStream);
                    }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes a <code>Schema.Gltf</code> model to a writable binary writer and pack all data into the model
        /// </summary>
        /// <param name="model"><code>Schema.Gltf</code> model</param>
        /// <param name="binaryWriter">Binary Writer</param>
        /// <param name="gltfFilePath">Source file path used to load the model</param>
        /// <param name="glbBinChunck">optional GLB-stored Buffer (BIN data file). If null, then the first buffer Uri must point to a BIN file.</param>
        public static void SaveBinaryModelPacked(this Gltf model, BinaryWriter binaryWriter, string gltfFilePath, byte[] glbBinChunck = null)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            byte[] binBufferData = null;

            using (var memoryStream = new MemoryStream())
            {
                var bufferViews = new List <BufferView>();
                var bufferData  = new Dictionary <int, byte[]>();

                if (model.BufferViews != null)
                {
                    foreach (var bufferView in model.BufferViews)
                    {
                        memoryStream.Align(4);

                        var byteOffset = memoryStream.Position;

                        byte[] data;
                        if (!bufferData.TryGetValue(bufferView.Buffer, out data))
                        {
                            // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#glb-stored-buffer
                            /// "glTF Buffer referring to GLB-stored BIN chunk, must have buffer.uri
                            /// property undefined, and it must be the first element of buffers array"
                            data = bufferView.Buffer == 0 && glbBinChunck != null
                                        ? glbBinChunck
                                        : model.LoadBinaryBuffer(bufferView.Buffer, gltfFilePath, bufferView.ByteOffset);

                            bufferData.Add(bufferView.Buffer, data);
                        }

                        memoryStream.Write(data, bufferView.ByteOffset, bufferView.ByteLength);

                        bufferView.Buffer     = 0;
                        bufferView.ByteOffset = (int)byteOffset;
                        bufferViews.Add(bufferView);
                    }
                }

                if (model.Images != null)
                {
                    for (var index = 0; index < model.Images.Length; index++)
                    {
                        var byteOffset = memoryStream.Position;

                        var data = model.OpenImageFile(index, gltfFilePath);
                        data.CopyTo(memoryStream);

                        var image = model.Images[index];
                        image.BufferView = bufferViews.Count;
                        image.MimeType   = GetMimeType(image.Uri);
                        image.Uri        = null;

                        bufferViews.Add(new BufferView
                        {
                            Buffer     = 0,
                            ByteOffset = (int)byteOffset,
                            ByteLength = (int)data.Length,
                        });
                    }
                }

                if (bufferViews.Any())
                {
                    model.BufferViews = bufferViews.ToArray();

                    model.Buffers = new[]
                    {
                        new glTFLoader.Schema.Buffer
                        {
                            ByteLength = (int)memoryStream.Length
                        }
                    };

                    binBufferData = memoryStream.ToArray();
                }
            }

            Interface.SaveBinaryModel(model, binBufferData, binaryWriter);
        }