public static AccessorSparseIndices Deserialize(GLTFRoot root, JsonReader reader) { var indices = new AccessorSparseIndices(); while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var curProp = reader.Value.ToString(); switch (curProp) { case "bufferView": indices.BufferView = BufferViewId.Deserialize(root, reader); break; case "byteOffset": indices.ByteOffset = reader.ReadAsInt32().Value; break; case "componentType": indices.ComponentType = (GLTFComponentType)reader.ReadAsInt32().Value; break; default: indices.DefaultPropertyDeserializer(root, reader); break; } } return(indices); }
public static Image Deserialize(GLTFRoot root, JsonReader reader) { var image = new Image(); while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var curProp = reader.Value.ToString(); switch (curProp) { case "uri": image.Uri = reader.ReadAsString(); break; case "mimeType": image.MimeType = reader.ReadAsString(); break; case "bufferView": image.BufferView = BufferViewId.Deserialize(root, reader); break; default: image.DefaultPropertyDeserializer(root, reader); break; } } return(image); }
public static Accessor Deserialize(GLTFRoot root, JsonReader reader) { var accessor = new Accessor(); while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var curProp = reader.Value.ToString(); switch (curProp) { case "bufferView": accessor.BufferView = BufferViewId.Deserialize(root, reader); break; case "byteOffset": accessor.ByteOffset = reader.ReadAsInt32().Value; break; case "componentType": accessor.ComponentType = (GLTFComponentType)reader.ReadAsInt32().Value; break; case "normalized": accessor.Normalized = reader.ReadAsBoolean().Value; break; case "count": accessor.Count = reader.ReadAsInt32().Value; break; case "type": accessor.Type = reader.ReadStringEnum <GLTFAccessorAttributeType>(); break; case "max": accessor.Max = reader.ReadDoubleList(); break; case "min": accessor.Min = reader.ReadDoubleList(); break; case "sparse": accessor.Sparse = AccessorSparse.Deserialize(root, reader); break; default: accessor.DefaultPropertyDeserializer(root, reader); break; } } return(accessor); }
private BufferViewId ExportBufferView(int byteOffset, int byteLength) { var bufferView = new BufferView { Buffer = _bufferId, ByteOffset = byteOffset, ByteLength = byteLength, }; var id = new BufferViewId { Id = _root.BufferViews.Count, Root = _root }; _root.BufferViews.Add(bufferView); return(id); }
private static void MergeMaterialsImagesTexturesAndSamplers(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes) { if (mergeFromRoot.Samplers != null) { if (mergeToRoot.Samplers == null) { mergeToRoot.Samplers = new List <Sampler>(mergeFromRoot.Samplers.Count); } mergeToRoot.Samplers.AddRange(mergeFromRoot.Samplers); } if (mergeFromRoot.Images != null) { if (mergeToRoot.Images == null) { mergeToRoot.Images = new List <Image>(mergeFromRoot.Images.Count); } mergeToRoot.Images.AddRange(mergeFromRoot.Images); for (int i = previousGLTFSizes.PreviousImageCount; i < mergeToRoot.Images.Count; ++i) { Image image = mergeToRoot.Images[i]; if (image.BufferView != null) { BufferViewId bufferViewId = image.BufferView; bufferViewId.Id += previousGLTFSizes.PreviousBufferViewCount; bufferViewId.Root = mergeToRoot; } } } if (mergeFromRoot.Textures != null) { if (mergeToRoot.Textures == null) { mergeToRoot.Textures = new List <Texture>(mergeFromRoot.Textures.Count); } mergeToRoot.Textures.AddRange(mergeFromRoot.Textures); for (int i = previousGLTFSizes.PreviousTextureCount; i < mergeToRoot.Textures.Count; ++i) { Texture texture = mergeToRoot.Textures[i]; if (texture.Sampler != null) { SamplerId samplerId = texture.Sampler; samplerId.Id += previousGLTFSizes.PreviousSamplerCount; samplerId.Root = mergeToRoot; } if (texture.Source != null) { ImageId samplerId = texture.Source; samplerId.Id += previousGLTFSizes.PreviousImageCount; samplerId.Root = mergeToRoot; } } } if (mergeFromRoot.Materials != null) { if (mergeToRoot.Materials == null) { mergeToRoot.Materials = new List <Material>(mergeFromRoot.Materials.Count); } mergeToRoot.Materials.AddRange(mergeFromRoot.Materials); for (int i = previousGLTFSizes.PreviousMaterialCount; i < mergeToRoot.Materials.Count; ++i) { Material material = mergeToRoot.Materials[i]; PbrMetallicRoughness pbrMetallicRoughness = material.PbrMetallicRoughness; if (pbrMetallicRoughness != null) { if (pbrMetallicRoughness.BaseColorTexture != null) { TextureId textureId = pbrMetallicRoughness.BaseColorTexture.Index; textureId.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } if (pbrMetallicRoughness.MetallicRoughnessTexture != null) { TextureId textureId = pbrMetallicRoughness.MetallicRoughnessTexture.Index; textureId.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } } MaterialCommonConstant commonConstant = material.CommonConstant; if (commonConstant?.LightmapTexture != null) { TextureId textureId = material.CommonConstant.LightmapTexture.Index; textureId.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } if (material.EmissiveTexture != null) { TextureId textureId = material.EmissiveTexture.Index; material.EmissiveTexture.Index.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } if (material.NormalTexture != null) { TextureId textureId = material.NormalTexture.Index; textureId.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } if (material.OcclusionTexture != null) { TextureId textureId = material.OcclusionTexture.Index; textureId.Id += previousGLTFSizes.PreviousTextureCount; textureId.Root = mergeToRoot; } } } }
private static void MergeAccessorsBufferViewsAndBuffers(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes) { if (mergeFromRoot.Buffers != null) { if (mergeToRoot.Buffers == null) { mergeToRoot.Buffers = new List <Buffer>(mergeFromRoot.Buffers.Count); } mergeToRoot.Buffers.AddRange(mergeFromRoot.Buffers); } if (mergeFromRoot.BufferViews != null) { if (mergeToRoot.BufferViews == null) { mergeToRoot.BufferViews = new List <BufferView>(mergeFromRoot.BufferViews.Count); } mergeToRoot.BufferViews.AddRange(mergeFromRoot.BufferViews); for (int i = previousGLTFSizes.PreviousBufferViewCount; i < mergeToRoot.BufferViews.Count; ++i) { GLTFId <Buffer> bufferId = mergeToRoot.BufferViews[i].Buffer; bufferId.Id += previousGLTFSizes.PreviousBufferCount; bufferId.Root = mergeToRoot; } } if (mergeFromRoot.Accessors != null) { if (mergeToRoot.Accessors == null) { mergeToRoot.Accessors = new List <Accessor>(mergeFromRoot.Accessors.Count); } mergeToRoot.Accessors.AddRange(mergeFromRoot.Accessors); for (int i = previousGLTFSizes.PreviousAccessorCount; i < mergeToRoot.Accessors.Count; ++i) { Accessor accessor = mergeToRoot.Accessors[i]; if (accessor.BufferView != null) { BufferViewId bufferViewId = accessor.BufferView; bufferViewId.Id += previousGLTFSizes.PreviousBufferViewCount; bufferViewId.Root = mergeToRoot; } AccessorSparse accessorSparse = accessor.Sparse; if (accessorSparse != null) { BufferViewId indicesId = accessorSparse.Indices.BufferView; indicesId.Id += previousGLTFSizes.PreviousBufferViewCount; indicesId.Root = mergeToRoot; BufferViewId valuesId = accessorSparse.Values.BufferView; valuesId.Id += previousGLTFSizes.PreviousBufferViewCount; valuesId.Root = mergeToRoot; } } } }
/// <summary> /// Removes a blob from the GLB at the given BufferView /// Updates accessors and images to have correct new bufferview index /// This function can invalidate BufferViewId's returned by previous function /// </summary> /// <param name="glb">The glb to remove from</param> /// <param name="bufferViewId">The buffer to remove</param> public static void RemoveBinaryData(GLBObject glb, BufferViewId bufferViewId) { if (glb == null) { throw new ArgumentNullException(nameof(glb)); } if (bufferViewId == null) { throw new ArgumentNullException(nameof(bufferViewId)); } BufferView bufferViewToRemove = bufferViewId.Value; int id = bufferViewId.Id; if (bufferViewToRemove.ByteOffset + bufferViewToRemove.ByteLength == glb.BinaryChunkInfo.Length) { uint bufferViewLengthAsUint = bufferViewToRemove.ByteLength; glb.SetFileLength(glb.Header.FileLength - bufferViewLengthAsUint); glb.SetBinaryChunkLength(glb.BinaryChunkInfo.Length - bufferViewLengthAsUint); if (glb.BinaryChunkInfo.Length == 0) { glb.Root.Buffers.RemoveAt(0); foreach (BufferView bufferView in glb.Root.BufferViews) // other buffers may still exist, and their index has now changed { --bufferView.Buffer.Id; } glb.SetFileLength(glb.Header.FileLength - GLTFParser.CHUNK_HEADER_SIZE); } else { glb.Root.Buffers[0].ByteLength = glb.BinaryChunkInfo.Length; // write binary chunk header WriteChunkHeader(glb.Stream, glb.BinaryChunkInfo); } // trim the end glb.Stream.SetLength(glb.Header.FileLength); // write glb header WriteHeader(glb.Stream, glb.Header, glb.StreamStartPosition); } glb.Root.BufferViews.RemoveAt(id); if (glb.Root.Accessors != null) { foreach (Accessor accessor in glb.Root.Accessors) // shift over all accessors { if (accessor.BufferView != null && accessor.BufferView.Id >= id) { --accessor.BufferView.Id; } if (accessor.Sparse != null) { if (accessor.Sparse.Indices?.BufferView.Id >= id) { --accessor.Sparse.Indices.BufferView.Id; } if (accessor.Sparse.Values?.BufferView.Id >= id) { --accessor.Sparse.Values.BufferView.Id; } } } } if (glb.Root.Images != null) { foreach (GLTFImage image in glb.Root.Images) { if (image.BufferView != null && image.BufferView.Id >= id) { --image.BufferView.Id; } } } }
private static void MergeAccessorsBufferViewsAndBuffers(GLTFRoot mergeToRoot, GLTFRoot mergeFromRoot, PreviousGLTFSizes previousGLTFSizes) { bool isGLB = false; if (mergeFromRoot.Buffers != null) { if (mergeToRoot.Buffers == null) { mergeToRoot.Buffers = new List <GLTFBuffer>(mergeFromRoot.Buffers.Count); } foreach (GLTFBuffer buffer in mergeFromRoot.Buffers) { if (buffer.Uri != null) { mergeToRoot.Buffers.Add(buffer); } else { isGLB = true; // assume glb is a uri is null } } } if (mergeFromRoot.BufferViews != null) { if (mergeToRoot.BufferViews == null) { mergeToRoot.BufferViews = new List <BufferView>(mergeFromRoot.BufferViews.Count); } mergeToRoot.BufferViews.AddRange(mergeFromRoot.BufferViews); for (int i = previousGLTFSizes.PreviousBufferViewCount; i < mergeToRoot.BufferViews.Count; ++i) { GLTFId <GLTFBuffer> bufferId = mergeToRoot.BufferViews[i].Buffer; if (!(isGLB && bufferId.Id == 0)) // if it is pointing a the special glb buffer (index 0 of a glb) then we dont want to adjust the buffer view, otherwise we do { // adjusting bufferview id based on merge amount bufferId.Id += previousGLTFSizes.PreviousBufferCount; bufferId.Root = mergeToRoot; } } } if (mergeFromRoot.Accessors != null) { if (mergeToRoot.Accessors == null) { mergeToRoot.Accessors = new List <Accessor>(mergeFromRoot.Accessors.Count); } mergeToRoot.Accessors.AddRange(mergeFromRoot.Accessors); for (int i = previousGLTFSizes.PreviousAccessorCount; i < mergeToRoot.Accessors.Count; ++i) { Accessor accessor = mergeToRoot.Accessors[i]; if (accessor.BufferView != null) { BufferViewId bufferViewId = accessor.BufferView; bufferViewId.Id += previousGLTFSizes.PreviousBufferViewCount; bufferViewId.Root = mergeToRoot; } AccessorSparse accessorSparse = accessor.Sparse; if (accessorSparse != null) { BufferViewId indicesId = accessorSparse.Indices.BufferView; indicesId.Id += previousGLTFSizes.PreviousBufferViewCount; indicesId.Root = mergeToRoot; BufferViewId valuesId = accessorSparse.Values.BufferView; valuesId.Id += previousGLTFSizes.PreviousBufferViewCount; valuesId.Root = mergeToRoot; } } } }