Skip to content

Commit 2073cf3

Browse files
committed
Fixed MaterialBuilder's conversion to glTF not copying the IOR value.
Fixes vpenades#246
1 parent 7123ad3 commit 2073cf3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/SharpGLTF.Toolkit/Schema2/MaterialExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public static void CopyTo(this MaterialBuilder srcMaterial, Material dstMaterial
390390
dstMaterial.Alpha = srcMaterial.AlphaMode.ToSchema2();
391391
dstMaterial.AlphaCutoff = srcMaterial.AlphaCutoff;
392392
dstMaterial.DoubleSided = srcMaterial.DoubleSided;
393-
dstMaterial.IndexOfRefraction = srcMaterial.IndexOfRefraction;
393+
394394

395395
var hasClearCoat
396396
= srcMaterial.GetChannel("ClearCoat") != null
@@ -442,6 +442,10 @@ var hasVolume
442442
defMaterial = srcMaterial.CompatibilityFallback;
443443
}
444444

445+
// IOR must be set after dst material initialization,
446+
// otherwise it's erased since it's stored in an extension
447+
dstMaterial.IndexOfRefraction = srcMaterial.IndexOfRefraction;
448+
445449
if (defMaterial != null)
446450
{
447451
if (defMaterial.ShaderStyle != "PBRMetallicRoughness") throw new ArgumentException(nameof(srcMaterial.CompatibilityFallback.ShaderStyle));
@@ -452,6 +456,10 @@ var hasVolume
452456
defMaterial.CopyChannelsTo(dstMaterial, "SpecularColor", "SpecularFactor");
453457
defMaterial.CopyChannelsTo(dstMaterial, "VolumeThickness", "VolumeAttenuation");
454458
}
459+
460+
// final validation
461+
462+
System.Diagnostics.Debug.Assert(dstMaterial.IndexOfRefraction == srcMaterial.IndexOfRefraction, "set IOR after dst material initialization");
455463
}
456464

457465
public static void CopyChannelsTo(this MaterialBuilder srcMaterial, Material dstMaterial, params string[] channelKeys)

tests/SharpGLTF.Toolkit.Tests/Materials/MaterialBuilderTests.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using NUnit.Framework;
88

9+
using SharpGLTF.Scenes;
910
using SharpGLTF.Schema2;
1011
using SharpGLTF.Validation;
1112

@@ -23,8 +24,8 @@ public void TestMaterialEquality()
2324
// ... And we could use it for general equality checks, but then, since
2425
// MaterialBuilder is NOT inmutable, it can mean that two materials can be equal
2526
// at a given time, and non equal at another. Furthermore, it would imply having
26-
// a hash code that changes over time. As a consequence, it could be impossible
27-
// to use MaterialBuilder as a dictionary Key.
27+
// a hash code that changes over time. As a consequence, using MaterialBuilder as
28+
// a dictionary key is possible, but dangerous if not carefully handled.
2829

2930
var srcMaterial = _CreateUnlitMaterial();
3031

@@ -100,6 +101,18 @@ public void CreateSpecularGlossinessWithFallback()
100101
Assert.That(MaterialBuilder.AreEqualByContent(material, material.Clone()), Is.True);
101102
}
102103

104+
[Test]
105+
public void CreateIORWithFallback()
106+
{
107+
// https://github.com/vpenades/SharpGLTF/issues/246
108+
109+
var material = new MaterialBuilder("MaterialWithIOR");
110+
material.IndexOfRefraction = 7;
111+
112+
Assert.That(MaterialBuilder.AreEqualByContent(material, material.Clone()), Is.True);
113+
Assert.That(MaterialBuilder.AreEqualByContent(material, _Schema2Roundtrip(material)), Is.True);
114+
}
115+
103116
private static MaterialBuilder _CreateUnlitMaterial()
104117
{
105118
var tex1 = ResourceInfo.From("shannon.png").FilePath;
@@ -192,7 +205,7 @@ private static MaterialBuilder _CreateClearCoatMaterial()
192205
.WithClearCoatRoughness(tex1, 1);
193206

194207
return material;
195-
}
208+
}
196209

197210
[Obsolete("SpecularGlossiness has been deprecated by Khronos")]
198211
private static MaterialBuilder _CreateSpecularGlossinessMaterialWithFallback()

0 commit comments

Comments
 (0)