예제 #1
0
        /// <summary>
        /// write model to filePath
        /// </summary>
        /// <param name="model"></param>
        /// <param name="filePath"></param>
        public void WriteModel(Model model, string filePath)
        {
            if (UriUtil.IsValidUri(model.Uri))
            {
                model.WriteUriFiles(filePath);
            }
            var ext = Path.GetExtension(filePath).TrimStart('.').ToUpper();

            if (ext == "GLB")
            {
                using (var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                    using (var bw = new BinaryWriter(fs))
                    {
                        var binCount   = 0;
                        var byteLength = 0;
                        if (model.BinBuffers != null)
                        {
                            byteLength = model.BinBuffers.Count;
                            Padding(model.BinBuffers, 4);
                            binCount += model.BinBuffers.Count;
                            if (model.Buffers.Count == 0)
                            {
                                var buffer = new Json.Buffer();
                                model.Buffers.Add(buffer);
                            }
                            model.Buffers[0].ByteLength = byteLength;
                        }
                        var json      = JsonUtil.ToJson(model);
                        var jsonBytes = new List <byte>(JsonUtil.StrEncoding.GetBytes(json));
                        PaddingSpace(jsonBytes, 4);
                        var len    = GlbHeader.ByteCount + jsonBytes.Count + binCount + 8 + (binCount > 0 ? 8 : 0);
                        var header = GlbHeader.GetGlbHeader((uint)len);
                        header.Write(bw);
                        var jsonChunk = new ChunkHeader
                        {
                            ChunkType = ChunkType.Json,
                            Length    = (uint)jsonBytes.Count
                        };
                        jsonChunk.Write(bw);
                        for (var i = 0; i < jsonBytes.Count; i++)
                        {
                            bw.Write(jsonBytes[i]);
                        }
                        if (model.BinBuffers != null)
                        {
                            var binChunk = new ChunkHeader
                            {
                                ChunkType = ChunkType.Bin,
                                Length    = (uint)model.BinBuffers.Count
                            };
                            binChunk.Write(bw);
                            for (var i = 0; i < model.BinBuffers.Count; i++)
                            {
                                bw.Write(model.BinBuffers[i]);
                            }
                        }
                        bw.Flush();
                    }
            }
            else
            {
                JsonUtil.Serialize(model, filePath);
            }
        }