Example #1
0
        private int AddBuffer(Model model, IList <List <byte> > buffers, IList <int> accessors, string name,
                              int byteOffset, int?byteStride, TargetType?target, int boundarySize = 4)
        {
            var bufferIndex  = model.Buffers.Count;
            var bufferList   = buffers.SelectMany(c => c).ToList();
            var bufferLength = bufferList.Count;

            if (!_options.GLB)
            {
                Document.Padding(bufferList, boundarySize);
                var buffer = new NetGltf.Json.Buffer
                {
                    ByteLength = bufferLength
                };
                model.Buffers.Add(buffer);
                if (_options.SeparateBinary)
                {
                    var binName = name + ".bin";
                    File.WriteAllBytes(Path.Combine(_gltfFolder, binName), bufferList.ToArray());
                    buffer.Uri = binName;
                }
                else
                {
                    buffer.Uri = "data:application/octet-stream;base64," + Convert.ToBase64String(bufferList.ToArray());
                }
            }
            else
            {
                _buffers.ByteOffset += bufferLength;
            }

            AddBufferView(model, buffers, accessors,
                          bufferIndex, byteOffset, byteStride, target);
            byteOffset += bufferLength;
            return(byteOffset);
        }
Example #2
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);
            }
        }
Example #3
0
        private int ResolveTexture(Model gltf, string textureFile)
        {
            var txtFile = GetObjTextureFullpath(textureFile);

            if (!File.Exists(txtFile))
            {
                return(-1);
            }
            if (_textureDict.TryGetValue(txtFile, out var index))
            {
                return(index);
            }
            var name       = Path.GetFileNameWithoutExtension(txtFile);
            int imageIndex = -1;

            if (_options.GLB)
            {
                var bytes = File.ReadAllBytes(txtFile);
                _buffers.ImageBuffers.Add(bytes);
                var bufferIndex = 0;
                var bvIndex     = gltf.BufferViews.Count;
                var bufferView  = new BufferView
                {
                    Name       = name,
                    Buffer     = bufferIndex,
                    ByteLength = bytes.Length,
                    ByteOffset = _buffers.ByteOffset
                };
                gltf.BufferViews.Add(bufferView);
                var image = new Image
                {
                    Name       = name,
                    BufferView = bvIndex,
                    MimeType   = GetMimeType(txtFile)
                };
                imageIndex = gltf.Images.Count;
                gltf.Images.Add(image);
                _buffers.ByteOffset += bytes.Length;
            }
            else if (_options.SeparateTextures)
            {
                var uri   = CopyTextureFile(txtFile);
                var image = new Image {
                    Name     = name,
                    Uri      = uri,
                    MimeType = GetMimeType(txtFile)
                };
                imageIndex = gltf.Images.Count;
                gltf.Images.Add(image);
            }
            else
            {
                var imageBytes = new List <byte>(File.ReadAllBytes(txtFile));
                var byteLength = imageBytes.Count;
                Document.Padding(imageBytes, 4);
                var buffer = new NetGltf.Json.Buffer
                {
                    ByteLength = imageBytes.Count
                };
                buffer.Uri = "data:application/octet-stream;base64," + Convert.ToBase64String(imageBytes.ToArray());
                var bufferIndex = gltf.Buffers.Count;
                gltf.Buffers.Add(buffer);
                var bufferView = new BufferView
                {
                    Name       = name,
                    Buffer     = bufferIndex,
                    ByteLength = byteLength,
                    ByteOffset = 0
                };
                var bvIndex = gltf.BufferViews.Count;
                gltf.BufferViews.Add(bufferView);
                var image = new Image
                {
                    Name       = name,
                    BufferView = bvIndex,
                    MimeType   = GetMimeType(txtFile)
                };
                imageIndex = gltf.Images.Count;
                gltf.Images.Add(image);
            }
            var tIndex = gltf.Textures.Count;

            gltf.Textures.Add(new Texture
            {
                Source  = imageIndex,
                Sampler = 0
            });
            _textureDict.Add(txtFile, tIndex);
            return(gltf.Textures.Count - 1);
        }