Add Extension parent context

This commit is contained in:
2026-10-23 19:25:46 +02:00
parent 71a8912635
commit fe9d87155c
7 changed files with 53 additions and 16 deletions
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace ReFuel.Gltf.Extensions;
struct KhrMaterialsEmissiveStrengthExtension
{
public double EmissiveStrenght;
public KhrMaterialsEmissiveStrengthExtension(double emissiveStrenght)
{
EmissiveStrenght = emissiveStrenght;
}
}
public sealed class KhrMaterialsEmissiveStrengthExtensionProvider : IGltfExtensionProvider
{
public string Name => "KHR_materials_emissive_strength";
public object ParseExtensionObject(GltfObjectKind parent, JsonElement element)
{
ArgumentOutOfRangeException.ThrowIfNotEqual(parent, GltfObjectKind.Material);
if (!element.TryGetProperty("emissiveStrength", out var property) || !property.TryGetDouble(out double d))
{
d = 1.0;
}
return new KhrMaterialsEmissiveStrengthExtension(d);
}
public void WriteExtensionObject(object obj, Utf8JsonWriter writer)
{
throw new NotImplementedException();
}
}
+7 -7
View File
@@ -16,7 +16,7 @@ namespace ReFuel.Gltf
this.extensionMode = extensionMode; this.extensionMode = extensionMode;
} }
public void Parse(JsonElement collection) public void Parse(GltfObjectKind parent, JsonElement collection)
{ {
if (collection.ValueKind != JsonValueKind.Object) if (collection.ValueKind != JsonValueKind.Object)
throw new Exception("Expected a JSON object."); throw new Exception("Expected a JSON object.");
@@ -25,7 +25,7 @@ namespace ReFuel.Gltf
{ {
if (extensionMode && GltfDocument.TryGetExtensionProvider(property.Name, out IGltfExtensionProvider? provider)) if (extensionMode && GltfDocument.TryGetExtensionProvider(property.Name, out IGltfExtensionProvider? provider))
{ {
Add(property.Name, provider.ParseExtensionObject(property.Value)); Add(property.Name, provider.ParseExtensionObject(parent, property.Value));
continue; continue;
} }
@@ -33,7 +33,7 @@ namespace ReFuel.Gltf
{ {
case JsonValueKind.Array: case JsonValueKind.Array:
GltfList list = new GltfList(); GltfList list = new GltfList();
list.Parse(property.Value); list.Parse(parent, property.Value);
Add(property.Name, list); Add(property.Name, list);
break; break;
case JsonValueKind.False: case JsonValueKind.False:
@@ -58,7 +58,7 @@ namespace ReFuel.Gltf
break; break;
case JsonValueKind.Object: case JsonValueKind.Object:
GltfCollection child = new GltfCollection(extensionMode); GltfCollection child = new GltfCollection(extensionMode);
child.Parse(property.Value); child.Parse(parent, property.Value);
Add(property.Name, child); Add(property.Name, child);
break; break;
case JsonValueKind.True: case JsonValueKind.True:
@@ -79,7 +79,7 @@ namespace ReFuel.Gltf
{ {
} }
public void Parse(JsonElement collection) public void Parse(GltfObjectKind parent, JsonElement collection)
{ {
if (collection.ValueKind != JsonValueKind.Array) if (collection.ValueKind != JsonValueKind.Array)
throw new Exception("Expected a JSON array."); throw new Exception("Expected a JSON array.");
@@ -90,7 +90,7 @@ namespace ReFuel.Gltf
{ {
case JsonValueKind.Array: case JsonValueKind.Array:
GltfList list = new GltfList(); GltfList list = new GltfList();
list.Parse(element); list.Parse(parent, element);
Add(list); Add(list);
break; break;
case JsonValueKind.False: case JsonValueKind.False:
@@ -115,7 +115,7 @@ namespace ReFuel.Gltf
break; break;
case JsonValueKind.Object: case JsonValueKind.Object:
GltfCollection child = new GltfCollection(false); GltfCollection child = new GltfCollection(false);
child.Parse(element); child.Parse(parent, element);
Add(child); Add(child);
break; break;
case JsonValueKind.True: case JsonValueKind.True:
+2 -2
View File
@@ -293,8 +293,8 @@ namespace ReFuel.Gltf
{ {
switch (name) switch (name)
{ {
case "extension": Extension.Parse(element); break; case "extension": Extension.Parse(Kind, element); break;
case "extra": Extras.Parse(element); break; case "extra": Extras.Parse(Kind, element); break;
case "scenes": case "scenes":
AssertArray(element, "scenes"); AssertArray(element, "scenes");
foreach (JsonElement child in element.EnumerateArray()) foreach (JsonElement child in element.EnumerateArray())
+1 -1
View File
@@ -14,7 +14,7 @@ namespace ReFuel.Gltf
Name = name; Name = name;
} }
public object ParseExtensionObject(JsonElement element) public object ParseExtensionObject(GltfObjectKind parent, JsonElement element)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
+2 -2
View File
@@ -47,11 +47,11 @@ namespace ReFuel.Gltf
{ {
if (element.TryGetProperty("extras", out JsonElement extras)) if (element.TryGetProperty("extras", out JsonElement extras))
{ {
Extras.Parse(extras); Extras.Parse(Kind, extras);
} }
if (element.TryGetProperty("extensions", out JsonElement extension)) if (element.TryGetProperty("extensions", out JsonElement extension))
{ {
Extension.Parse(extension); Extension.Parse(Kind, extension);
} }
} }
@@ -16,14 +16,14 @@ namespace ReFuel.Gltf
throw new Exception("This type is not JSON serializable."); throw new Exception("This type is not JSON serializable.");
} }
public T ParseExtension(JsonElement element) public T ParseExtension(GltfObjectKind parent, JsonElement element)
{ {
return JsonSerializer.Deserialize<T>(element) ?? throw new Exception("Could not deserialize extension object."); return JsonSerializer.Deserialize<T>(element) ?? throw new Exception("Could not deserialize extension object.");
} }
public object ParseExtensionObject(JsonElement element) public object ParseExtensionObject(GltfObjectKind parent, JsonElement element)
{ {
return ParseExtension(element)!; return ParseExtension(parent, element)!;
} }
public void WriteExtension(T value, Utf8JsonWriter writer) public void WriteExtension(T value, Utf8JsonWriter writer)
+1 -1
View File
@@ -7,7 +7,7 @@ namespace ReFuel.Gltf
{ {
string Name { get; } string Name { get; }
object ParseExtensionObject(JsonElement element); object ParseExtensionObject(GltfObjectKind parent, JsonElement element);
void WriteExtensionObject(object obj, Utf8JsonWriter writer); void WriteExtensionObject(object obj, Utf8JsonWriter writer);
} }
} }