-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from B3zaleel/implement-MPEG_texture_video-ext…
…ension Implement MPEG_texture_video Gltf extension
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/ThreeDModels/Format/Gltf/Extensions/MPEG_texture_video.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |