diff --git a/src/ThreeDModels/Format/Gltf/Extensions/MPEG_texture_video.cs b/src/ThreeDModels/Format/Gltf/Extensions/MPEG_texture_video.cs new file mode 100644 index 0000000..608e023 --- /dev/null +++ b/src/ThreeDModels/Format/Gltf/Extensions/MPEG_texture_video.cs @@ -0,0 +1,107 @@ +using System.Text.Json; +using ThreeDModels.Format.Gltf.Elements; +using ThreeDModels.Format.Gltf.IO; +using static ThreeDModels.Format.Gltf.IO.Utf8JsonReaderHelpers; + +namespace ThreeDModels.Format.Gltf.Extensions; + +/// +/// Represents an object that specifies textures using MPEG defined formats. +/// +public class MPEG_texture_video : IGltfProperty +{ + /// + /// Provides the accessor's index in accessors array. + /// + public required int Accessor { get; set; } + /// + /// Provides the maximum width of the texture. + /// + public required float Width { get; set; } + /// + /// Provides the maximum height of the texture. + /// + public required float Height { get; set; } + /// + /// Indicates the format of the pixel data for this video texture. + /// + public string? Format { get; set; } + public Dictionary? Extensions { get; set; } + public object? Extras { get; set; } +} + +public class MpegTextureVideoExtension : IGltfExtension +{ + public string Name => nameof(MPEG_texture_video); + public const string Default_Format = "RGB"; + + public object? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context, Type parentType) + { + int? accessor = null; + float? width = null; + float? height = null; + string? format = null; + Dictionary? extensions = null; + object? extras = null; + if (jsonReader.TokenType == JsonTokenType.PropertyName && jsonReader.Read()) + { + } + if (jsonReader.TokenType == JsonTokenType.Null) + { + return null; + } + else if (jsonReader.TokenType != JsonTokenType.StartObject) + { + throw new InvalidDataException("Failed to find start of property."); + } + while (jsonReader.Read()) + { + if (jsonReader.TokenType == JsonTokenType.EndObject) + { + break; + } + var propertyName = jsonReader.GetString(); + if (propertyName == nameof(accessor)) + { + accessor = ReadInteger(ref jsonReader); + } + else if (propertyName == nameof(width)) + { + width = ReadFloat(ref jsonReader); + } + else if (propertyName == nameof(height)) + { + height = ReadFloat(ref jsonReader); + } + else if (propertyName == nameof(format)) + { + format = ReadString(ref jsonReader); + } + else if (propertyName == nameof(extensions)) + { + extensions = ExtensionsSerialization.Read(ref jsonReader, context); + } + else if (propertyName == nameof(extras)) + { + extras = JsonSerialization.Read(ref jsonReader, context); + } + else + { + throw new InvalidDataException($"Unknown property: {propertyName}"); + } + } + if (accessor == null || width == null || height == null) + { + throw new InvalidDataException("MPEG_texture_video.accessor, MPEG_texture_video.accessor, and MPEG_texture_video.accessor are required properties."); + } + return new MPEG_texture_video() + { + Accessor = (int)accessor!, + Width = (float)width!, + Height = (float)height!, + Format = format ?? Default_Format, + Extensions = extensions, + Extras = extras, + }; + } +}