Browse Source

added basic material edition support

Vicente Penades 6 years ago
parent
commit
0aaafc0e31

+ 2 - 2
src/glTF2Sharp.CodeGen/glTF2Sharp.CodeGen.csproj

@@ -8,8 +8,8 @@
 
   <ItemGroup>
     <PackageReference Include="LibGit2Sharp" Version="0.26.0" />
-    <PackageReference Include="NJsonSchema.CodeGeneration" Version="9.13.18" />
-    <PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.13.18" />
+    <PackageReference Include="NJsonSchema.CodeGeneration" Version="9.13.19" />
+    <PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.13.19" />
   </ItemGroup>
 
 </Project>

+ 10 - 0
src/glTF2Sharp.DOM/Geometry/MeshPrimitive.cs

@@ -241,6 +241,12 @@ namespace glTF2Sharp.Geometry
 
         public IndicesAccessor Indices => _Indices;
 
+        public int? MaterialLogicalIndex
+        {
+            get => _MaterialIndex;
+            set => _MaterialIndex = value;
+        }
+
         #endregion
 
         #region API
@@ -268,6 +274,10 @@ namespace glTF2Sharp.Geometry
             if (this._Indices != null) this._Indices.AssignToSchema(dstPrim);
 
             dstPrim.DrawPrimitiveType = this._PrimitiveDrawType;
+
+            var material = _MaterialIndex.HasValue ? dstPrim.LogicalParent.LogicalParent.LogicalMaterials[_MaterialIndex.Value] : null;
+
+            dstPrim.Material = material;
         }
 
         #endregion

+ 42 - 19
src/glTF2Sharp.DOM/Schema2/gltf.Materials.cs

@@ -72,6 +72,9 @@ namespace glTF2Sharp.Schema2
 
         public Boolean Unlit => this.GetExtension<MaterialUnlit_KHR>() != null;
 
+        /// <summary>
+        /// returns a collection of channel views available for this material.
+        /// </summary>
         public IEnumerable<MaterialChannelView> Channels
         {
             get
@@ -122,6 +125,16 @@ namespace glTF2Sharp.Schema2
 
         #region API
 
+        /// <summary>
+        /// Returns an object that allows to read and write information of a given channel of the material.
+        /// </summary>
+        /// <param name="key">the channel key</param>
+        /// <returns>the channel view</returns>
+        public MaterialChannelView GetChannel(string key)
+        {
+            return Channels.FirstOrDefault(item => item.Key == key);
+        }
+
         private MaterialNormalTextureInfo _GetNormalTexture(bool create)
         {
             if (create && _normalTexture == null) _normalTexture = new MaterialNormalTextureInfo();
@@ -143,14 +156,14 @@ namespace glTF2Sharp.Schema2
         #endregion
     }
 
-    [System.Diagnostics.DebuggerDisplay("Channel {_Semantic}")]
+    [System.Diagnostics.DebuggerDisplay("Channel {_Key}")]
     public struct MaterialChannelView
     {
         #region lifecycle
 
-        internal MaterialChannelView(Material m, string semantic, Func<Boolean, TextureInfo> texInfo, Func<Vector4> fg, Action<Vector4> fs)
+        internal MaterialChannelView(Material m, String key, Func<Boolean, TextureInfo> texInfo, Func<Vector4> fg, Action<Vector4> fs)
         {
-            _Semantic = semantic;
+            _Key = key;
             _Material = m;
             _TextureInfoGetter = texInfo;
             _FactorGetter = fg;
@@ -161,7 +174,7 @@ namespace glTF2Sharp.Schema2
 
         #region data
 
-        private readonly String _Semantic;
+        private readonly String _Key;
         private readonly Material _Material;
 
         private readonly Func<Boolean, TextureInfo> _TextureInfoGetter;
@@ -171,9 +184,9 @@ namespace glTF2Sharp.Schema2
 
         #endregion
 
-        #region API
+        #region properties
 
-        public String Semantic => _Semantic;
+        public String Key => _Key;
 
         public Texture Texture => _TextureInfoGetter?.Invoke(false) == null ? null : _Material.LogicalParent.LogicalTextures[_TextureInfoGetter(false)._LogicalTextureIndex];
 
@@ -185,27 +198,37 @@ namespace glTF2Sharp.Schema2
 
         public Vector4 Factor => _FactorGetter();
 
-        public void SetTexture(int texSet, Texture tex, Vector4 val)
-        {
-            Guard.NotNull(tex, nameof(tex));
-            Guard.MustShareLogicalParent(_Material, tex, nameof(tex));
-
-            var texInfo = _TextureInfoGetter(true);
+        #endregion
 
-            texInfo.TextureSet = texSet;
-            texInfo._LogicalTextureIndex = tex.LogicalIndex;
+        #region fluent API
 
-            _FactorSetter?.Invoke(val);
-        }
+        public void SetFactor(Vector4 value) { _FactorSetter?.Invoke(value); }
 
-        public void SetTexture(int texSet, Image texImg, Vector4 texVal, TextureInterpolationMode mag, TextureMipMapMode min, TextureWrapMode ws, TextureWrapMode wt)
+        public void SetTexture(
+            int texSet,
+            Image texImg,
+            TextureInterpolationMode mag = TextureInterpolationMode.LINEAR,
+            TextureMipMapMode min = TextureMipMapMode.LINEAR_MIPMAP_LINEAR,
+            TextureWrapMode ws = TextureWrapMode.REPEAT,
+            TextureWrapMode wt = TextureWrapMode.REPEAT)
         {
-            if (texImg == null) return; // in theory, it should completely remove the TextureInfo
+            if (texImg == null) return; // in theory, we should completely remove the TextureInfo
 
             var sampler = _Material.LogicalParent.UseLogicalSampler(mag, min, ws, wt);
             var texture = _Material.LogicalParent.UseLogicalTexture(texImg, sampler);
 
-            SetTexture(texSet, texture, texVal);
+            SetTexture(texSet, texture);
+        }
+
+        public void SetTexture(int texSet, Texture tex)
+        {
+            Guard.NotNull(tex, nameof(tex));
+            Guard.MustShareLogicalParent(_Material, tex, nameof(tex));
+
+            var texInfo = _TextureInfoGetter(true);
+
+            texInfo.TextureSet = texSet;
+            texInfo._LogicalTextureIndex = tex.LogicalIndex;
         }
 
         #endregion

+ 8 - 2
src/glTF2Sharp.Tests/Geometry/CreateMeshTests.cs

@@ -44,7 +44,8 @@ namespace glTF2Sharp.Geometry
             // assign vertices and indices
             srcPrimitive.Vertices[0].SetValues(0, positions);
             srcPrimitive.Vertices[1].SetValues(0, normals);
-            srcPrimitive.Indices.SetValues(0, indices);            
+            srcPrimitive.Indices.SetValues(0, indices);
+            srcPrimitive.MaterialLogicalIndex = 0; // using material with index 0, which will be created later
 
             // check the values we've set match the input data
             CollectionAssert.AreEqual(positions, srcPrimitive.Vertices[0].AsVector3Array());
@@ -53,12 +54,17 @@ namespace glTF2Sharp.Geometry
 
             // Notice that until now, we've been working with objects in the .Geometry namespace.
 
-            // create a new Schema2 scene:
+            // Now we switch to the .Schema2 namespace and we create a new scene:
 
             var root = Schema2.ModelRoot.CreateNew();                        
             var scene = root.UseScene("default");
             var node = scene.AddVisualNode("main scene");
 
+            var material = root.AddLogicalMaterial(typeof(Schema2.MaterialPBRMetallicRoughness));
+            material.DoubleSided = true;
+            material.GetChannel("BaseColor")
+                .SetFactor(new Vector4(1, 0, 0, 1));
+
             node.Mesh = root.CreateMesh();
 
             // this assigns the mesh we've created before to this schema mesh.

+ 1 - 1
src/glTF2Sharp.Tests/glTF2Sharp.Tests.csproj

@@ -11,7 +11,7 @@
   <ItemGroup>
     <PackageReference Include="LibGit2Sharp" Version="0.26.0" />
     <PackageReference Include="nunit" Version="3.11.0" />
-    <PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
+    <PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />    
   </ItemGroup>