Skip to content

Commit

Permalink
Implement KHR_materials_sheen Gltf extension
Browse files Browse the repository at this point in the history
  • Loading branch information
B3zaleel committed May 28, 2024
1 parent f3a0e95 commit d568c78
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/ThreeDModels/Format/Gltf/Extensions/KHR_materials_sheen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 defines the sheen material model.
/// </summary>
public class KHR_materials_sheen : IGltfProperty
{
/// <summary>
/// Color of the sheen layer (in linear space).
/// </summary>
public required float[] SheenColorFactor { get; set; }
/// <summary>
/// The sheen color (RGB) texture.
/// </summary>
public TextureInfo? SheenColorTexture { get; set; }
/// <summary>
/// The sheen layer roughness of the material.
/// </summary>
public float SheenRoughnessFactor { get; set; }
/// <summary>
/// The sheen roughness (Alpha) texture.
/// </summary>
public TextureInfo? SheenRoughnessTexture { get; set; }
public Dictionary<string, object?>? Extensions { get; set; }
public object? Extras { get; set; }
}

public class KhrMaterialsSheenExtension : IGltfExtension
{
public string Name => nameof(KHR_materials_sheen);
public static readonly float[] Default_SheenColorFactor = [0.0f, 0.0f, 0.0f];
public const float Default_SheenRoughnessFactor = 0.0f;

public object? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context, Type parentType)
{
if (parentType != typeof(Material))
{
throw new InvalidDataException("KHR_materials_sheen must be used in a Material.");
}
float[]? sheenColorFactor = null;
TextureInfo? sheenColorTexture = null;
float? sheenRoughnessFactor = null;
TextureInfo? sheenRoughnessTexture = 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(sheenColorFactor))
{
sheenColorFactor = ReadFloatList(ref jsonReader, context)?.ToArray();
}
else if (propertyName == nameof(sheenColorTexture))
{
sheenColorTexture = TextureInfoSerialization.Read(ref jsonReader, context);
}
else if (propertyName == nameof(sheenRoughnessFactor))
{
sheenRoughnessFactor = ReadFloat(ref jsonReader);
}
else if (propertyName == nameof(sheenRoughnessTexture))
{
sheenRoughnessTexture = TextureInfoSerialization.Read(ref jsonReader, context);
}
else if (propertyName == nameof(extensions))
{
extensions = ExtensionsSerialization.Read<KHR_materials_sheen>(ref jsonReader, context);
}
else if (propertyName == nameof(extras))
{
extras = JsonSerialization.Read(ref jsonReader, context);
}
else
{
throw new InvalidDataException($"Unknown property: {propertyName}");
}
}
if (sheenColorFactor != null && sheenColorFactor.Length != 3)
{
throw new InvalidDataException("KHR_materials_sheen.sheenColorFactor must have 3 elements.");
}
return new KHR_materials_sheen()
{
SheenColorFactor = sheenColorFactor ?? Default_SheenColorFactor,
SheenColorTexture = sheenColorTexture,
SheenRoughnessFactor = sheenRoughnessFactor ?? Default_SheenRoughnessFactor,
SheenRoughnessTexture = sheenRoughnessTexture,
Extensions = extensions,
Extras = extras,
};
}
}

0 comments on commit d568c78

Please sign in to comment.