-
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 #37 from B3zaleel/implement-MSFT_packing_occlusion…
…RoughnessMetallic-extension Implement MSFT_packing_occlusionRoughnessMetallic Gltf extension
- Loading branch information
Showing
1 changed file
with
143 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
src/ThreeDModels/Format/Gltf/Extensions/MSFT_packing_occlusionRoughnessMetallic.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,143 @@ | ||
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 a packing of occlusion, roughness, and metallic in a single texture and a two channel normal map. | ||
/// </summary> | ||
public class MSFT_packing_occlusionRoughnessMetallic : IGltfProperty | ||
{ | ||
/// <summary> | ||
/// A texture with packing Occlusion (R), Roughness (G), Metallic (B). | ||
/// </summary> | ||
public MsftTextureIndex? OcclusionRoughnessMetallicTexture { get; set; } | ||
/// <summary> | ||
/// A texture with packing Roughness (R), Metallic (G), Occlusion (B). | ||
/// </summary> | ||
public MsftTextureIndex? RoughnessMetallicOcclusionTexture { get; set; } | ||
/// <summary> | ||
/// A texture which contains two channel (RG) normal map. | ||
/// </summary> | ||
public MsftTextureIndex? NormalTexture { get; set; } | ||
public Dictionary<string, object?>? Extensions { get; set; } | ||
public object? Extras { get; set; } | ||
} | ||
|
||
public class MsftTextureIndex | ||
{ | ||
/// <summary> | ||
/// The index of the texture. | ||
/// </summary> | ||
public int? Index { get; set; } | ||
} | ||
|
||
public class MsftPackingOcclusionRoughnessMetallicExtension : IGltfExtension | ||
{ | ||
public string Name => nameof(MSFT_packing_occlusionRoughnessMetallic); | ||
|
||
public object? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context, Type parentType) | ||
{ | ||
if (parentType != typeof(Gltf)) | ||
{ | ||
throw new InvalidDataException("MSFT_packing_occlusionRoughnessMetallic must be used in a Gltf root."); | ||
} | ||
MsftTextureIndex? occlusionRoughnessMetallicTexture = null; | ||
MsftTextureIndex? roughnessMetallicOcclusionTexture = null; | ||
MsftTextureIndex? normalTexture = 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(occlusionRoughnessMetallicTexture)) | ||
{ | ||
occlusionRoughnessMetallicTexture = MsftTextureIndexSerialization.Read(ref jsonReader, context); | ||
} | ||
else if (propertyName == nameof(roughnessMetallicOcclusionTexture)) | ||
{ | ||
roughnessMetallicOcclusionTexture = MsftTextureIndexSerialization.Read(ref jsonReader, context); | ||
} | ||
else if (propertyName == nameof(normalTexture)) | ||
{ | ||
normalTexture = MsftTextureIndexSerialization.Read(ref jsonReader, context); | ||
} | ||
else if (propertyName == nameof(extensions)) | ||
{ | ||
extensions = ExtensionsSerialization.Read<MSFT_packing_occlusionRoughnessMetallic>(ref jsonReader, context); | ||
} | ||
else if (propertyName == nameof(extras)) | ||
{ | ||
extras = JsonSerialization.Read(ref jsonReader, context); | ||
} | ||
else | ||
{ | ||
throw new InvalidDataException($"Unknown property: {propertyName}"); | ||
} | ||
} | ||
return new MSFT_packing_occlusionRoughnessMetallic() | ||
{ | ||
OcclusionRoughnessMetallicTexture = occlusionRoughnessMetallicTexture, | ||
RoughnessMetallicOcclusionTexture = roughnessMetallicOcclusionTexture, | ||
NormalTexture = normalTexture, | ||
Extensions = extensions, | ||
Extras = extras, | ||
}; | ||
} | ||
} | ||
|
||
public class MsftTextureIndexSerialization | ||
{ | ||
public static MsftTextureIndex? Read(ref Utf8JsonReader jsonReader, GltfReaderContext context) | ||
{ | ||
int? index = 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(index)) | ||
{ | ||
index = ReadInteger(ref jsonReader); | ||
} | ||
else | ||
{ | ||
throw new InvalidDataException($"Unknown property: {propertyName}"); | ||
} | ||
} | ||
return new MsftTextureIndex() | ||
{ | ||
Index = index, | ||
}; | ||
} | ||
} |