Skip to content

Commit

Permalink
Merge pull request #35 from B3zaleel/implement-MPEG_texture_video-ext…
Browse files Browse the repository at this point in the history
…ension

Implement MPEG_texture_video Gltf extension
  • Loading branch information
B3zaleel authored May 28, 2024
2 parents c470c14 + ec75590 commit 0903ebf
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/ThreeDModels/Format/Gltf/Extensions/MPEG_texture_video.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Represents an object that specifies textures using MPEG defined formats.
/// </summary>
public class MPEG_texture_video : IGltfProperty
{
/// <summary>
/// Provides the accessor's index in accessors array.
/// </summary>
public required int Accessor { get; set; }
/// <summary>
/// Provides the maximum width of the texture.
/// </summary>
public required float Width { get; set; }
/// <summary>
/// Provides the maximum height of the texture.
/// </summary>
public required float Height { get; set; }
/// <summary>
/// Indicates the format of the pixel data for this video texture.
/// </summary>
public string? Format { get; set; }
public Dictionary<string, object?>? 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<string, object?>? 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<MPEG_texture_video>(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,
};
}
}

0 comments on commit 0903ebf

Please sign in to comment.