11using System ;
22using System . Collections . Generic ;
3- using System . Globalization ;
43using System . Linq ;
54using System . Numerics ;
65using System . Runtime . CompilerServices ;
7- using System . Text ;
86
97namespace 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