Skip to content

Commit 3ec7719

Browse files
committed
Added new material extensions to MaterialBuilder
1 parent 8626713 commit 3ec7719

File tree

6 files changed

+304
-222
lines changed

6 files changed

+304
-222
lines changed

src/SharpGLTF.Core/Schema2/gltf.MaterialParameter.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ internal enum _MaterialParameterKey
4545
RGB,
4646
RGBA,
4747

48-
NormalScale,
49-
OcclusionStrength,
50-
EmissiveStrength,
51-
5248
Minimum, Maximum,
5349

50+
NormalScale,
51+
5452
IndexOfRefraction,
5553

54+
AnisotropyRotation,
55+
56+
OcclusionStrength,
57+
EmissiveStrength,
58+
AnisotropyStrength,
59+
5660
MetallicFactor,
5761
RoughnessFactor,
5862
SpecularFactor,
@@ -61,11 +65,8 @@ internal enum _MaterialParameterKey
6165
ThicknessFactor,
6266
TransmissionFactor,
6367
IridescenceFactor,
64-
AttenuationDistance,
65-
AnisotropyStrength,
66-
AnisotropyRotation,
67-
DiffuseTransmissionFactor,
68-
DiffuseTransmissionColor,
68+
AttenuationDistance,
69+
DiffuseTransmissionFactor,
6970
}
7071

7172
[System.Diagnostics.DebuggerDisplay("{_Key} = {Value}")]

src/SharpGLTF.Core/Schema2/gltf.MaterialsFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void InitializePBRMetallicRoughness(params string[] extensionNames)
5757
foreach (var extn in extensionNames)
5858
{
5959
if (extn == "Sheen") this.UseExtension<MaterialSheen>();
60+
if (extn == "Volume") this.UseExtension<MaterialVolume>();
6061
if (extn == "Specular") this.UseExtension<MaterialSpecular>();
61-
if (extn == "ClearCoat") this.UseExtension<MaterialClearCoat>();
62+
if (extn == "ClearCoat") this.UseExtension<MaterialClearCoat>();
63+
if (extn == "Anisotropy") this.UseExtension<MaterialAnisotropy>();
64+
if (extn == "Iridescence") this.UseExtension<MaterialIridescence>();
6265
if (extn == "Transmission") this.UseExtension<MaterialTransmission>();
6366
if (extn == "DiffuseTransmission") this.UseExtension<MaterialDiffuseTransmission>();
64-
if (extn == "Volume") this.UseExtension<MaterialVolume>();
65-
if (extn == "Anisotropy") this.UseExtension<MaterialAnisotropy>();
66-
if (extn == "Iridiscence") this.UseExtension<MaterialIridescence>();
6767
}
6868
}
6969

@@ -670,10 +670,10 @@ public float IridescenceThicknessMaximum
670670

671671
public IEnumerable<MaterialChannel> GetChannels(Material material)
672672
{
673-
var iridisTexture = new _MaterialTexture(() => _iridescenceTexture, () => SetProperty(this, ref _iridescenceTexture, new TextureInfo()));
673+
var iridesTexture = new _MaterialTexture(() => _iridescenceTexture, () => SetProperty(this, ref _iridescenceTexture, new TextureInfo()));
674674
var factor = new _MaterialParameter<float>(_MaterialParameterKey.IridescenceFactor, (float)_iridescenceFactorDefault, () => IridescenceFactor, v => IridescenceFactor = v);
675675
var idxRef = new _MaterialParameter<float>(_MaterialParameterKey.IndexOfRefraction, (float)_iridescenceIorDefault, () => IridescenceIndexOfRefraction, v => IridescenceIndexOfRefraction = v);
676-
yield return new MaterialChannel(material, "Iridescence", iridisTexture, factor, idxRef);
676+
yield return new MaterialChannel(material, "Iridescence", iridesTexture, factor, idxRef);
677677

678678
var thicknessTexture = new _MaterialTexture(() => _iridescenceThicknessTexture, () => SetProperty(this, ref _iridescenceThicknessTexture, new TextureInfo()));
679679
var thkMin = new _MaterialParameter<float>(_MaterialParameterKey.Minimum, (float)_iridescenceThicknessMinimumDefault, () => IridescenceThicknessMinimum, v => IridescenceThicknessMinimum = v);
@@ -754,7 +754,7 @@ public IEnumerable<MaterialChannel> GetChannels(Material material)
754754
yield return new MaterialChannel(material, "DiffuseTransmissionFactor", factorTexture, factorParameter);
755755

756756
var colorTexture = new _MaterialTexture(() => _diffuseTransmissionColorTexture, () => SetProperty(this, ref _diffuseTransmissionColorTexture, new TextureInfo()));
757-
var colorParameter = new _MaterialParameter<Vector3>(_MaterialParameterKey.DiffuseTransmissionColor, _diffuseTransmissionColorFactorDefault, () => DiffuseTransmissionColorFactor, v => DiffuseTransmissionColorFactor = v);
757+
var colorParameter = new _MaterialParameter<Vector3>(_MaterialParameterKey.RGB, _diffuseTransmissionColorFactorDefault, () => DiffuseTransmissionColorFactor, v => DiffuseTransmissionColorFactor = v);
758758
yield return new MaterialChannel(material, "DiffuseTransmissionColor", colorTexture, colorParameter);
759759
}
760760
}

src/SharpGLTF.Toolkit/Materials/MaterialBuilder.cs

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Globalization;
43
using System.Linq;
54
using System.Numerics;
65
using System.Runtime.CompilerServices;
7-
using System.Text;
86

97
namespace SharpGLTF.Materials
108
{
@@ -90,6 +88,7 @@ public MaterialBuilder(MaterialBuilder other)
9088
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
9189
private string _ShaderStyle = SHADERPBRMETALLICROUGHNESS;
9290

91+
9392
public AlphaMode AlphaMode { get; set; } = AlphaMode.OPAQUE;
9493

9594
public Single AlphaCutoff { get; set; } = 0.5f;
@@ -130,7 +129,8 @@ public static bool AreEqualByContent(MaterialBuilder x, MaterialBuilder y)
130129
if (!BaseBuilder.AreEqualByContent(x, y)) return false;
131130

132131
if (x.AlphaMode != y.AlphaMode) return false;
133-
if (x.AlphaCutoff != y.AlphaCutoff) return false;
132+
// AlphaCutoff only has meaning when AlphaMode = Mask
133+
if (x.AlphaMode == AlphaMode.MASK && x.AlphaCutoff != y.AlphaCutoff) return false;
134134
if (x.DoubleSided != y.DoubleSided) return false;
135135
if (x.IndexOfRefraction != y.IndexOfRefraction) return false;
136136
if (x._ShaderStyle != y._ShaderStyle) return false;
@@ -218,24 +218,7 @@ private void _SetShader(string shader)
218218
}
219219
}
220220

221-
public ChannelBuilder GetChannel(KnownChannel channelKey)
222-
{
223-
return _Channels.FirstOrDefault(item => item.Key == channelKey);
224-
}
225-
226-
public ChannelBuilder UseChannel(KnownChannel channelKey)
227-
{
228-
Guard.IsTrue(_GetValidChannels().Contains(channelKey), nameof(channelKey));
229-
230-
var ch = GetChannel(channelKey);
231-
if (ch != null) return ch;
232-
233-
ch = new ChannelBuilder(this, channelKey);
234-
_Channels.Add(ch);
235-
236-
return ch;
237-
}
238-
221+
[Obsolete("Use GetChannel with KnownChannel whenever possible")]
239222
public ChannelBuilder GetChannel(string channelKey)
240223
{
241224
Guard.NotNullOrEmpty(channelKey, nameof(channelKey));
@@ -244,6 +227,7 @@ public ChannelBuilder GetChannel(string channelKey)
244227
return GetChannel(key);
245228
}
246229

230+
[Obsolete("Use UseChannel with KnownChannel whenever possible")]
247231
public ChannelBuilder UseChannel(string channelKey)
248232
{
249233
Guard.NotNullOrEmpty(channelKey, nameof(channelKey));
@@ -252,6 +236,24 @@ public ChannelBuilder UseChannel(string channelKey)
252236
return UseChannel(key);
253237
}
254238

239+
public ChannelBuilder GetChannel(KnownChannel channelKey)
240+
{
241+
return _Channels.FirstOrDefault(item => item.Key == channelKey);
242+
}
243+
244+
public ChannelBuilder UseChannel(KnownChannel channelKey)
245+
{
246+
Guard.IsTrue(_GetValidChannels().Contains(channelKey), nameof(channelKey));
247+
248+
var ch = GetChannel(channelKey);
249+
if (ch != null) return ch;
250+
251+
ch = new ChannelBuilder(this, channelKey);
252+
_Channels.Add(ch);
253+
254+
return ch;
255+
}
256+
255257
public void RemoveChannel(KnownChannel key)
256258
{
257259
var idx = _Channels.IndexOf(item => item.Key == key);
@@ -261,11 +263,11 @@ public void RemoveChannel(KnownChannel key)
261263

262264
internal void ValidateForSchema2()
263265
{
264-
var hasClearCoat = this.GetChannel("ClearCoat") != null
265-
|| this.GetChannel("ClearCoatRoughness") != null
266-
|| this.GetChannel("ClearCoatNormal") != null;
266+
var hasClearCoat = this.GetChannel(KnownChannel.ClearCoat) != null
267+
|| this.GetChannel(KnownChannel.ClearCoatNormal) != null
268+
|| this.GetChannel(KnownChannel.ClearCoatRoughness) != null;
267269

268-
var hasTransmission = this.GetChannel("Transmission") != null;
270+
var hasTransmission = this.GetChannel(KnownChannel.Transmission) != null;
269271

270272
if (this.ShaderStyle == SHADERPBRSPECULARGLOSSINESS)
271273
{
@@ -365,11 +367,21 @@ public MaterialBuilder WithChannelParam(string channelKey, Vector4 parameter)
365367
return this;
366368
}
367369

370+
[Obsolete("Use WithChannelImage(KnownChannel channelKey, ImageBuilder primaryImage)")]
371+
public MaterialBuilder WithChannelImage(string channelKey, ImageBuilder primaryImage)
372+
{
373+
this.UseChannel(channelKey)
374+
.UseTexture()
375+
.WithPrimaryImage(primaryImage);
376+
377+
return this;
378+
}
379+
368380
public MaterialBuilder WithChannelParam(KnownChannel channelKey, KnownProperty propertyName, Object parameter)
369381
{
370382
this.UseChannel(channelKey).Parameters[propertyName] = MaterialValue.CreateFrom(parameter);
371383
return this;
372-
}
384+
}
373385

374386
public MaterialBuilder WithChannelImage(KnownChannel channelKey, ImageBuilder primaryImage)
375387
{
@@ -386,14 +398,7 @@ public MaterialBuilder WithChannelImage(KnownChannel channelKey, ImageBuilder pr
386398
return this;
387399
}
388400

389-
public MaterialBuilder WithChannelImage(string channelKey, ImageBuilder primaryImage)
390-
{
391-
this.UseChannel(channelKey)
392-
.UseTexture()
393-
.WithPrimaryImage(primaryImage);
394-
395-
return this;
396-
}
401+
397402

398403
/// <summary>
399404
/// Defines a fallback <see cref="MaterialBuilder"/> instance for the current <see cref="MaterialBuilder"/>.
@@ -490,76 +495,91 @@ public MaterialBuilder WithClearCoatNormal(ImageBuilder imageFile)
490495

491496
public MaterialBuilder WithClearCoat(ImageBuilder imageFile, float intensity)
492497
{
493-
WithChannelImage(KnownChannel.ClearCoat, imageFile);
494-
// WithChannelParam(KnownChannel.ClearCoat, new Vector4(intensity, 0, 0, 0));
498+
WithChannelImage(KnownChannel.ClearCoat, imageFile);
495499
WithChannelParam(KnownChannel.ClearCoat, KnownProperty.ClearCoatFactor, intensity);
496500
return this;
497501
}
498502

499503
public MaterialBuilder WithClearCoatRoughness(ImageBuilder imageFile, float roughness)
500504
{
501-
WithChannelImage(KnownChannel.ClearCoatRoughness, imageFile);
502-
// WithChannelParam(KnownChannel.ClearCoatRoughness, new Vector4(roughness, 0, 0, 0));
505+
WithChannelImage(KnownChannel.ClearCoatRoughness, imageFile);
503506
WithChannelParam(KnownChannel.ClearCoatRoughness, KnownProperty.RoughnessFactor, roughness);
504507
return this;
505508
}
506509

507510
public MaterialBuilder WithTransmission(ImageBuilder imageFile, float intensity)
508511
{
509-
WithChannelImage(KnownChannel.Transmission, imageFile);
510-
// WithChannelParam(KnownChannel.Transmission, new Vector4(intensity, 0, 0, 0));
512+
WithChannelImage(KnownChannel.Transmission, imageFile);
511513
WithChannelParam(KnownChannel.Transmission, KnownProperty.TransmissionFactor, intensity);
512514
return this;
513515
}
514516

517+
public MaterialBuilder WithDiffuseTransmissionFactor(ImageBuilder imageFile, float factor)
518+
{
519+
WithChannelImage(KnownChannel.DiffuseTransmissionFactor, imageFile);
520+
WithChannelParam(KnownChannel.DiffuseTransmissionFactor, KnownProperty.DiffuseTransmissionFactor, factor);
521+
return this;
522+
}
523+
524+
public MaterialBuilder WithDiffuseTransmissionColor(ImageBuilder imageFile, Vector3? rgb = null)
525+
{
526+
WithChannelImage(KnownChannel.DiffuseTransmissionColor, imageFile);
527+
if (rgb.HasValue) WithChannelParam(KnownChannel.DiffuseTransmissionColor, KnownProperty.RGB, rgb.Value);
528+
return this;
529+
}
530+
515531
public MaterialBuilder WithSpecularColor(ImageBuilder imageFile, Vector3? rgb = null)
516532
{
517-
WithChannelImage(KnownChannel.SpecularColor, imageFile);
518-
// if (rgb.HasValue) WithChannelParam(KnownChannel.SpecularColor, new Vector4(rgb.Value, 0));
533+
WithChannelImage(KnownChannel.SpecularColor, imageFile);
519534
if (rgb.HasValue) WithChannelParam(KnownChannel.SpecularColor, KnownProperty.RGB, rgb.Value);
520535
return this;
521536
}
522537

523538
public MaterialBuilder WithSpecularFactor(ImageBuilder imageFile, float factor)
524539
{
525-
WithChannelImage(KnownChannel.SpecularFactor, imageFile);
526-
// WithChannelParam(KnownChannel.SpecularFactor, new Vector4(factor, 0, 0, 0));
540+
WithChannelImage(KnownChannel.SpecularFactor, imageFile);
527541
WithChannelParam(KnownChannel.SpecularFactor, KnownProperty.SpecularFactor, factor);
528542
return this;
529543
}
530544

531545
public MaterialBuilder WithVolumeThickness(ImageBuilder imageFile, float factor)
532546
{
533-
WithChannelImage(KnownChannel.VolumeThickness, imageFile);
534-
// WithChannelParam(KnownChannel.VolumeThickness, new Vector4(factor, 0, 0, 0));
547+
WithChannelImage(KnownChannel.VolumeThickness, imageFile);
535548
WithChannelParam(KnownChannel.VolumeThickness, KnownProperty.ThicknessFactor, factor);
536549
return this;
537550
}
538551

539552
public MaterialBuilder WithVolumeAttenuation(Vector3 color, float distance)
540-
{
541-
// WithChannelParam(KnownChannel.VolumeAttenuation, new Vector4(color, distance));
553+
{
542554
WithChannelParam(KnownChannel.VolumeAttenuation, KnownProperty.RGB, color);
543555
WithChannelParam(KnownChannel.VolumeAttenuation, KnownProperty.AttenuationDistance, distance);
544556
return this;
545557
}
546558

547-
public MaterialBuilder WithIridiscence(ImageBuilder imageFile, float factor = 0f, float ior = 1.3f)
559+
public MaterialBuilder WithIridescence(ImageBuilder imageFile, float factor = 0f, float ior = 1.3f)
548560
{
549561
WithChannelImage(KnownChannel.Iridescence, imageFile);
550562
WithChannelParam(KnownChannel.Iridescence, KnownProperty.IridescenceFactor, factor);
551563
WithChannelParam(KnownChannel.Iridescence, KnownProperty.IndexOfRefraction, ior);
552564
return this;
553565
}
554566

555-
public MaterialBuilder WithIridiscenceThickness(ImageBuilder imageFile, float min = 100f, float max = 400f)
567+
public MaterialBuilder WithIridescenceThickness(ImageBuilder imageFile, float min = 100f, float max = 400f)
556568
{
557569
WithChannelImage(KnownChannel.IridescenceThickness, imageFile);
558570
WithChannelParam(KnownChannel.IridescenceThickness, KnownProperty.Minimum, min);
559571
WithChannelParam(KnownChannel.IridescenceThickness, KnownProperty.Maximum, max);
560572
return this;
561573
}
562574

575+
public MaterialBuilder WithAnisotropy(ImageBuilder imageFile, float strength = 0f, float rotation = 0f)
576+
{
577+
WithChannelImage(KnownChannel.Anisotropy, imageFile);
578+
WithChannelParam(KnownChannel.Anisotropy, KnownProperty.AnisotropyStrength, strength);
579+
WithChannelParam(KnownChannel.Anisotropy, KnownProperty.AnisotropyRotation, rotation);
580+
return this;
581+
}
582+
563583
#endregion
564584

565585
#region API - OBSOLETE

0 commit comments

Comments
 (0)