Skip to content

Commit

Permalink
Merge pull request #38 from B3zaleel/implement-MSFT_texture_dds-exten…
Browse files Browse the repository at this point in the history
…sion

Implement MSFT_texture_dds Gltf extension
  • Loading branch information
B3zaleel authored May 28, 2024
2 parents a3353f1 + 3276691 commit dc4c2d0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/ThreeDModels/Format/Gltf/Extensions/MSFT_texture_dds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 the DirectDraw Surface file format (DDS).
/// </summary>
public class MSFT_texture_dds : IGltfProperty
{
/// <summary>
/// The index of the images node which points to a DDS texture file."
/// </summary>
public required int Source { get; set; }
public Dictionary<string, object?>? Extensions { get; set; }
public object? Extras { get; set; }
}

public class MsftTextureDdsExtension : IGltfExtension
{
public string Name => nameof(MSFT_texture_dds);

public object? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context, Type parentType)
{
if (parentType != typeof(Gltf))
{
throw new InvalidDataException("MSFT_texture_dds must be used in a Gltf root.");
}
int? source = 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(source))
{
source = ReadInteger(ref jsonReader);
}
else if (propertyName == nameof(extensions))
{
extensions = ExtensionsSerialization.Read<MSFT_texture_dds>(ref jsonReader, context);
}
else if (propertyName == nameof(extras))
{
extras = JsonSerialization.Read(ref jsonReader, context);
}
else
{
throw new InvalidDataException($"Unknown property: {propertyName}");
}
}
if (source == null)
{
throw new InvalidDataException("MSFT_texture_dds.source is a required property.");
}
return new MSFT_texture_dds()
{
Source = (int)source!,
Extensions = extensions,
Extras = extras,
};
}
}

0 comments on commit dc4c2d0

Please sign in to comment.