Browse Source

code cleanup

Vicente Penades 3 years ago
parent
commit
e605a89b43

+ 2 - 2
src/SharpGLTF.Core/IO/JsonContent.Impl.cs

@@ -29,7 +29,7 @@ namespace SharpGLTF.IO
                 options = new JSONOPTIONS
                 {
                     PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
-                    IgnoreNullValues = true,
+                    DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
                     WriteIndented = true
                 };
             }
@@ -59,7 +59,7 @@ namespace SharpGLTF.IO
                 options = new JSONOPTIONS
                 {
                     PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
-                    IgnoreNullValues = true,
+                    DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
                     WriteIndented = true
                 };
             }

+ 1 - 1
src/SharpGLTF.Core/IO/JsonContent.cs

@@ -134,7 +134,7 @@ namespace SharpGLTF.IO
                 options = new JSONOPTIONS
                 {
                     PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
-                    IgnoreNullValues = true,
+                    DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
                     WriteIndented = true
                 };
             }

+ 3 - 0
src/SharpGLTF.Core/Memory/MemoryAccessor.cs

@@ -163,6 +163,9 @@ namespace SharpGLTF.Memory
             }
         }
 
+        /// <summary>
+        /// returns true if this type can be used as a joint index.
+        /// </summary>
         public Boolean IsValidIndexer
         {
             get

+ 45 - 37
src/SharpGLTF.Core/Runtime/RuntimeOptions.cs

@@ -1,45 +1,53 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace SharpGLTF.Runtime
-{
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SharpGLTF.Runtime
+{
     public enum MeshInstancing
     {
         Discard,
         Enabled,
         SingleMesh,
         // TODO: add options to trim the number of instances
-    }
-
-    public class RuntimeOptions
-    {
-        /// <summary>
-        /// True if we want to copy buffers data instead of sharing it.
-        /// </summary>
-        public bool IsolateMemory { get; set; }
-
+    }
+
+    public class RuntimeOptions
+    {
+        /// <summary>
+        /// True if we want to copy buffers data instead of sharing it.
+        /// </summary>
+        /// <remarks>
+        /// If we want to create a runtime representation of the model, so the garbage collector will release the source model,
+        /// we have to set this to true, so we will not use any reference to the source model.
+        /// </remarks>
+        public bool IsolateMemory { get; set; }
+
         /// <summary>
         /// Gets or sets a value indicating whether GPU instancing is enabled or disabled.
-        /// </summary>
-        /// <remarks>
-        /// When true, if a gltf mesh has gpu instancing elements, they will be converted<br/>
-        /// internally to the runtime as <see cref="InstancedDrawableTemplate"/> elements.
-        /// </remarks>
-        public MeshInstancing GpuMeshInstancing { get; set; } = MeshInstancing.Enabled;
-
-        /// <summary>
-        /// Gets or sets the custom extras converter.
-        /// </summary>
-        public Converter<Schema2.ExtraProperties, Object> ExtrasConverterCallback { get; set; }
-
-        internal static Object ConvertExtras(Schema2.ExtraProperties source, RuntimeOptions options)
-        {
-            if (source.Extras.Content == null) return null;
-
-            var callback = options?.ExtrasConverterCallback;
-
-            return callback != null ? callback(source) : source.Extras.DeepClone();
-        }
-    }
-}
+        /// </summary>
+        /// <remarks>
+        /// When true, if a gltf mesh has gpu instancing elements, they will be converted<br/>
+        /// internally to the runtime as <see cref="InstancedDrawableTemplate"/> elements.
+        /// </remarks>
+        public MeshInstancing GpuMeshInstancing { get; set; } = MeshInstancing.Enabled;
+
+        /// <summary>
+        /// Gets or sets the custom extras converter.
+        /// </summary>
+        public Converter<Schema2.ExtraProperties, Object> ExtrasConverterCallback { get; set; }
+
+        internal static Object ConvertExtras(Schema2.ExtraProperties source, RuntimeOptions options)
+        {
+            if (source.Extras.Content == null) return null;
+
+            if (options == null) return source.Extras;
+
+            var callback = options.ExtrasConverterCallback;
+
+            return callback != null
+                ? callback(source)
+                : (options.IsolateMemory ? source.Extras.DeepClone() : source.Extras);
+        }
+    }
+}

+ 1 - 0
src/SharpGLTF.Core/Schema2/gltf.MaterialChannel.cs

@@ -280,6 +280,7 @@ namespace SharpGLTF.Schema2
 
             return new Vector4(tmp[0], tmp[1], tmp[2], tmp[3]);
         }
+
         internal static void Apply(IReadOnlyList<MaterialParameter> parameters, Vector4 value)
         {
             Span<float> tmp = stackalloc float[4];

+ 9 - 5
src/SharpGLTF.Core/Schema2/gltf.MaterialsFactory.cs

@@ -89,7 +89,7 @@ namespace SharpGLTF.Schema2
             if (channels != null) { foreach (var c in channels) yield return c; }
 
             channels = this.GetExtension<MaterialClearCoat>()?.GetChannels(this);
-            if (channels != null) {foreach (var c in channels) yield return c; }
+            if (channels != null) { foreach (var c in channels) yield return c; }
 
             channels = this.GetExtension<MaterialTransmission>()?.GetChannels(this);
             if (channels != null) { foreach (var c in channels) yield return c; }
@@ -103,17 +103,20 @@ namespace SharpGLTF.Schema2
             channels = this.GetExtension<MaterialVolume>()?.GetChannels(this);
             if (channels != null) { foreach (var c in channels) yield return c; }
 
-            var normalParam = new MaterialParameter(MaterialParameter.Key.NormalScale,
+            var normalParam = new MaterialParameter(
+                MaterialParameter.Key.NormalScale,
                 MaterialNormalTextureInfo.ScaleDefault,
                 () => _GetNormalTexture(false)?.Scale ?? MaterialNormalTextureInfo.ScaleDefault,
                 value => _GetNormalTexture(true).Scale = value);
 
-            var occlusionParam = new MaterialParameter(MaterialParameter.Key.OcclusionStrength,
+            var occlusionParam = new MaterialParameter(
+                MaterialParameter.Key.OcclusionStrength,
                 MaterialOcclusionTextureInfo.StrengthDefault,
                 () => _GetOcclusionTexture(false)?.Strength ?? MaterialOcclusionTextureInfo.StrengthDefault,
                 value => _GetOcclusionTexture(true).Strength = value);
 
-            var emissiveParam = new MaterialParameter(MaterialParameter.Key.RGB,
+            var emissiveParam = new MaterialParameter(
+                MaterialParameter.Key.RGB,
                 _emissiveFactorDefault,
                 () => this._emissiveFactor.AsValue(_emissiveFactorDefault),
                 value => this._emissiveFactor = value.AsNullable(_emissiveFactorDefault, Vector3.Zero, Vector3.One));
@@ -408,6 +411,7 @@ namespace SharpGLTF.Schema2
         {
             return base.GetLogicalChildren().ConcatElements(_sheenColorTexture, _sheenRoughnessTexture);
         }
+
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -592,7 +596,7 @@ namespace SharpGLTF.Schema2
 
         public float AttenuationDistance
         {
-            get => (float)_attenuationDistance.AsValue((double)0);
+            get => (float)_attenuationDistance.AsValue(0);
             set => _attenuationDistance = value > _attenuationDistanceExclusiveMinimum ? value : throw new ArgumentOutOfRangeException();
         }
 

+ 1 - 0
src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexMaterial.cs

@@ -543,6 +543,7 @@ namespace SharpGLTF.Geometry.VertexTypes
             this.Color += delta.Color0Delta;
             this.TexCoord += delta.TexCoord0Delta;
         }
+
         void IVertexMaterial.SetColor(int setIndex, Vector4 color) { if (setIndex == 0) this.Color = color; }
 
         void IVertexMaterial.SetTexCoord(int setIndex, Vector2 coord) { if (setIndex == 0) this.TexCoord = coord; }

+ 15 - 7
src/SharpGLTF.Toolkit/Materials/MaterialBuilder.cs

@@ -500,48 +500,56 @@ namespace SharpGLTF.Materials
         public MaterialBuilder WithClearCoat(IMAGEFILE imageFile, float intensity)
         {
             WithChannelImage(KnownChannel.ClearCoat, imageFile);
-            WithChannelParam(KnownChannel.ClearCoat, new Vector4(intensity, 0, 0, 0));
+            // WithChannelParam(KnownChannel.ClearCoat, new Vector4(intensity, 0, 0, 0));
+            WithChannelParam(KnownChannel.ClearCoat, KnownProperty.ClearCoatFactor, intensity);
             return this;
         }
 
         public MaterialBuilder WithClearCoatRoughness(IMAGEFILE imageFile, float roughness)
         {
             WithChannelImage(KnownChannel.ClearCoatRoughness, imageFile);
-            WithChannelParam(KnownChannel.ClearCoatRoughness, new Vector4(roughness, 0, 0, 0));
+            // WithChannelParam(KnownChannel.ClearCoatRoughness, new Vector4(roughness, 0, 0, 0));
+            WithChannelParam(KnownChannel.ClearCoatRoughness, KnownProperty.RoughnessFactor, roughness);
             return this;
         }
 
         public MaterialBuilder WithTransmission(IMAGEFILE imageFile, float intensity)
         {
             WithChannelImage(KnownChannel.Transmission, imageFile);
-            WithChannelParam(KnownChannel.Transmission, new Vector4(intensity, 0, 0, 0));
+            // WithChannelParam(KnownChannel.Transmission, new Vector4(intensity, 0, 0, 0));
+            WithChannelParam(KnownChannel.Transmission, KnownProperty.TransmissionFactor, intensity);
             return this;
         }
 
         public MaterialBuilder WithSpecularColor(IMAGEFILE imageFile, Vector3? rgb = null)
         {
             WithChannelImage(KnownChannel.SpecularColor, imageFile);
-            if (rgb.HasValue) WithChannelParam(KnownChannel.SpecularColor, new Vector4(rgb.Value, 0));
+            // if (rgb.HasValue) WithChannelParam(KnownChannel.SpecularColor, new Vector4(rgb.Value, 0));
+            if (rgb.HasValue) WithChannelParam(KnownChannel.SpecularColor, KnownProperty.RGB, rgb.Value);
             return this;
         }
 
         public MaterialBuilder WithSpecularFactor(IMAGEFILE imageFile, float factor)
         {
             WithChannelImage(KnownChannel.SpecularFactor, imageFile);
-            WithChannelParam(KnownChannel.SpecularFactor, new Vector4(factor, 0, 0, 0));
+            // WithChannelParam(KnownChannel.SpecularFactor, new Vector4(factor, 0, 0, 0));
+            WithChannelParam(KnownChannel.SpecularFactor, KnownProperty.SpecularFactor, factor);
             return this;
         }
 
         public MaterialBuilder WithVolumeThickness(IMAGEFILE imageFile, float factor)
         {
             WithChannelImage(KnownChannel.VolumeThickness, imageFile);
-            WithChannelParam(KnownChannel.VolumeThickness, new Vector4(factor, 0, 0, 0));
+            // WithChannelParam(KnownChannel.VolumeThickness, new Vector4(factor, 0, 0, 0));
+            WithChannelParam(KnownChannel.VolumeThickness, KnownProperty.ThicknessFactor, factor);
             return this;
         }
 
         public MaterialBuilder WithVolumeAttenuation(Vector3 color, float distance)
         {
-            WithChannelParam(KnownChannel.VolumeAttenuation, new Vector4(color, distance));
+            // WithChannelParam(KnownChannel.VolumeAttenuation, new Vector4(color, distance));
+            WithChannelParam(KnownChannel.VolumeAttenuation, KnownProperty.RGB, color);
+            WithChannelParam(KnownChannel.VolumeAttenuation, KnownProperty.AttenuationDistance, distance);
             return this;
         }
 

+ 1 - 1
src/SharpGLTF.Toolkit/Materials/MaterialEnums.cs

@@ -142,7 +142,7 @@ namespace SharpGLTF.Materials
                     yield return new _Property(KnownProperty.GlossinessFactor, 1f);
                     break;
 
-                case KnownChannel.BaseColor: yield return new _Property(KnownProperty.RGBA, Vector4.One);break;
+                case KnownChannel.BaseColor: yield return new _Property(KnownProperty.RGBA, Vector4.One); break;
                 case KnownChannel.MetallicRoughness:
                     yield return new _Property(KnownProperty.MetallicFactor, 1f);
                     yield return new _Property(KnownProperty.RoughnessFactor, 1f);

+ 1 - 1
tests/SharpGLTF.ThirdParty.Tests/EdMackeyTests.cs

@@ -20,7 +20,7 @@ namespace SharpGLTF.ThirdParty
             var material1 = new MaterialBuilder()
                 .WithDoubleSide(true)
                 .WithMetallicRoughnessShader()
-                .WithChannelParam("BaseColor", new Vector4(1, 0, 0, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 0, 1));
 
             var mesh = new MeshBuilder<VERTEX>("mesh");
 

+ 7 - 7
tests/SharpGLTF.Toolkit.Tests/Geometry/MeshBuilderAdvancedTests.cs

@@ -29,7 +29,7 @@ namespace SharpGLTF.Geometry
 
             // create a material
             var material1 = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, Vector4.One);
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, Vector4.One);
             
             // create model
             var meshBuilder = new MeshBuilder<VPOSNRM>("mesh1");
@@ -62,10 +62,10 @@ namespace SharpGLTF.Geometry
 
             // create materials
             var material1 = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1));
 
             var material2 = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1));            
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1));            
 
             // create several meshes
             var meshBuilder1 = new MeshBuilder<VPOSNRM>("mesh1");
@@ -126,7 +126,7 @@ namespace SharpGLTF.Geometry
             TestContext.CurrentContext.AttachGltfValidatorLinks();            
 
             // create a material
-            var material1 = new MaterialBuilder("material1").WithChannelParam(KnownChannel.BaseColor, Vector4.One);
+            var material1 = new MaterialBuilder("material1").WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, Vector4.One);
 
             // create a mesh
             var meshBuilder = new MeshBuilder<VPOSNRM>("mesh1");
@@ -175,11 +175,11 @@ namespace SharpGLTF.Geometry
 
             // create two materials
             var pink = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1))
                 .WithDoubleSide(true);
 
             var yellow = new MaterialBuilder("material2")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1))
                 .WithDoubleSide(true);
 
             // create the mesh
@@ -357,7 +357,7 @@ namespace SharpGLTF.Geometry
             var materials = Enumerable
                 .Range(0, 10)
                 .Select(idx => MaterialBuilder.CreateDefault()
-                .WithChannelParam("BaseColor", new Vector4(rnd.NextVector3(),1)))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(),1)))
                 .ToList();
 
             // create a mesh

+ 5 - 5
tests/SharpGLTF.Toolkit.Tests/Geometry/MeshBuilderTests.cs

@@ -113,11 +113,11 @@ namespace SharpGLTF.Geometry
 
             var material1 = new Materials.MaterialBuilder()
                 .WithMetallicRoughnessShader()
-                .WithChannelParam("BaseColor", Vector4.One * 0.5f);
+                .WithChannelParam(Materials.KnownChannel.BaseColor, Materials.KnownProperty.RGBA, Vector4.One * 0.5f);
 
             var material2 = new Materials.MaterialBuilder()
                 .WithMetallicRoughnessShader()
-                .WithChannelParam("BaseColor", Vector4.One * 0.7f);
+                .WithChannelParam(Materials.KnownChannel.BaseColor, Materials.KnownProperty.RGBA, Vector4.One * 0.7f);
 
             // create a mesh with degenerated triangles
 
@@ -214,9 +214,9 @@ namespace SharpGLTF.Geometry
 
             // since Materials.MaterialBuilder is not immutable we can change the contents,
             // so now, material1, material2 and material3 no longer represent the same material
-            material1.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Vector4.One * 0.2f);
-            material2.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Vector4.One * 0.4f);
-            material3.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Vector4.One * 0.6f);
+            material1.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Materials.KnownProperty.RGBA, Vector4.One * 0.2f);
+            material2.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Materials.KnownProperty.RGBA, Vector4.One * 0.4f);
+            material3.WithMetallicRoughnessShader().WithChannelParam(Materials.KnownChannel.BaseColor, Materials.KnownProperty.RGBA, Vector4.One * 0.6f);
 
             gltfModel = scene.ToGltf2();            
             Assert.AreEqual(3, gltfModel.LogicalMaterials.Count);

+ 11 - 11
tests/SharpGLTF.Toolkit.Tests/Scenes/SceneBuilderTests.cs

@@ -154,7 +154,7 @@ namespace SharpGLTF.Scenes
             var materials = Enumerable
                 .Range(0, 10)
                 .Select(idx => new MaterialBuilder()
-                .WithChannelParam("BaseColor", new Vector4(rnd.NextVector3(), 1)))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(), 1)))
                 .ToList();
 
             // create meshes
@@ -252,11 +252,11 @@ namespace SharpGLTF.Scenes
             // create two materials
 
             var pink = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1))
+                .WithChannelParam(KnownChannel.BaseColor,KnownProperty.RGBA, new Vector4(1, 0, 1, 1))
                 .WithDoubleSide(true);
 
             var yellow = new MaterialBuilder("material2")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1))
                 .WithDoubleSide(true);
 
             // create the mesh            
@@ -339,11 +339,11 @@ namespace SharpGLTF.Scenes
             // create two materials
 
             var pink = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1))
                 .WithDoubleSide(true);
 
             var yellow = new MaterialBuilder("material2")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1))
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1))
                 .WithDoubleSide(true);
 
             // create the mesh            
@@ -450,10 +450,10 @@ namespace SharpGLTF.Scenes
             // create two materials
 
             var pink = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1));
 
             var yellow = new MaterialBuilder("material2")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1));
 
             var scene = new SceneBuilder();
 
@@ -668,8 +668,8 @@ namespace SharpGLTF.Scenes
             // create materials
             var materials = Enumerable
                 .Range(0, 10)
-                .Select(idx => new Materials.MaterialBuilder($"material{idx}")
-                .WithChannelParam("BaseColor", new Vector4(rnd.NextVector3(), 1)))
+                .Select(idx => new MaterialBuilder($"material{idx}")
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(), 1)))
                 .ToList();
 
             // create meshes            
@@ -845,10 +845,10 @@ namespace SharpGLTF.Scenes
             // create two materials
 
             var pink = new MaterialBuilder("material1")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1));
 
             var blue = new MaterialBuilder("material2")
-                .WithChannelParam(KnownChannel.BaseColor, new Vector4(0, 0, 1, 1));
+                .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(0, 0, 1, 1));
 
             var mesh1 = VPOSNRM.CreateCompatibleMesh("shape1");
             var prim1 = mesh1.UsePrimitive(pink);