From cf0e65924b339cb6a6fedf1b2a2d25f46aa1a0c2 Mon Sep 17 00:00:00 2001 From: Bezaleel Olakunori Date: Tue, 28 May 2024 13:09:38 +0200 Subject: [PATCH] Implement KHR_materials_transmission Gltf extension --- .../Extensions/KHR_materials_transmission.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/ThreeDModels/Format/Gltf/Extensions/KHR_materials_transmission.cs diff --git a/src/ThreeDModels/Format/Gltf/Extensions/KHR_materials_transmission.cs b/src/ThreeDModels/Format/Gltf/Extensions/KHR_materials_transmission.cs new file mode 100644 index 0000000..62f06f0 --- /dev/null +++ b/src/ThreeDModels/Format/Gltf/Extensions/KHR_materials_transmission.cs @@ -0,0 +1,91 @@ +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 defines the optical transmission of a material. +/// +public class KHR_materials_transmission : IGltfProperty +{ + /// + /// The base percentage of non-specularly reflected light that is transmitted through the surface. + /// + public float TransmissionFactor { get; set; } + /// + /// A texture that defines the transmission percentage of the surface, sampled from the R channel. These values are linear, and will be multiplied by transmissionFactor. + /// + public TextureInfo? TransmissionTexture { get; set; } + public Dictionary? Extensions { get; set; } + public object? Extras { get; set; } +} + +public class KhrMaterialsTransmissionExtension : IGltfExtension +{ + public string Name => nameof(KHR_materials_transmission); + public const float Default_TransmissionFactor = 0.0f; + + public object? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context, Type parentType) + { + if (parentType != typeof(Material)) + { + throw new InvalidDataException("KHR_materials_transmission must be used in a Material."); + } + float? transmissionFactor = null; + TextureInfo? transmissionTexture = 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(transmissionFactor)) + { + transmissionFactor = ReadFloat(ref jsonReader); + } + else if (propertyName == nameof(transmissionTexture)) + { + transmissionTexture = TextureInfoSerialization.Read(ref jsonReader, context); + } + 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 (transmissionFactor != null && (transmissionFactor < 0.0f || transmissionFactor > 1.0f)) + { + throw new InvalidDataException("KHR_materials_transmission.transmissionFactor must be between 0.0 and 1.0."); + } + return new KHR_materials_transmission() + { + TransmissionFactor = transmissionFactor ?? Default_TransmissionFactor, + TransmissionTexture = transmissionTexture, + Extensions = extensions, + Extras = extras, + }; + } +}