Browse Source

refactoring/testing the 3D tiles extensions

Bert Temme 1 year ago
parent
commit
d69e1075c3

+ 1 - 93
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.FeatureID.cs

@@ -1,91 +1,10 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Linq;
 
 namespace SharpGLTF.Schema2.Tiles3D
 {
     using Collections;
 
-    /// <summary>
-    /// Mesh Feature Ids
-    /// </summary>
-    /// <remarks>
-    /// Implemented by <see cref="MeshExtInstanceFeatureID"/> and <see cref="MeshExtMeshFeatureID"/>
-    /// </remarks>
-    public interface IMeshFeatureIDInfo
-    {
-        /// <summary>
-        /// The number of unique features in the attribute or texture.
-        /// </summary>
-        public int FeatureCount { get; set; }
-
-        /// <summary>
-        /// A value that indicates that no feature is associated with this vertex or texel.
-        /// </summary>
-        public int? NullFeatureId { get; set; }
-
-        /// <summary>
-        /// An attribute containing feature IDs. When `attribute` and `texture` are omitted the 
-        /// feature IDs are assigned to vertices by their index.
-        /// </summary>
-        public int? Attribute { get; set; }
-
-        /// <summary>
-        /// A label assigned to this feature ID set. Labels must be alphanumeric identifiers 
-        /// matching the regular expression `^[a-zA-Z_][a-zA-Z0-9_]*$`.
-        /// </summary>
-        public string Label { get; set; }
-
-        /// <summary>
-        /// The index of the property table containing per-feature property values. Only applicable when using the `EXT_structural_metadata` extension.
-        /// </summary>
-        public int? PropertyTableIndex { get; set; }        
-    }
-
-    public sealed class FeatureIDBuilder : IMeshFeatureIDInfo , IEquatable<IMeshFeatureIDInfo>
-    {
-        public FeatureIDBuilder(int featureCount, int? attribute = null, string label = null, int? nullFeatureId = null)
-        {
-            FeatureCount = featureCount;
-            Label = label;
-            Attribute = attribute;
-            NullFeatureId = nullFeatureId;
-        }
-
-        public FeatureIDBuilder(PropertyTable table, int? attribute = null, string label = null, int? nullFeatureId = null)
-        {
-            FeatureCount = table.Count;
-            Attribute = attribute;
-            _root = table.LogicalParent;
-            PropertyTableIndex = table.LogicalIndex;
-            Label = label;
-            NullFeatureId = nullFeatureId;
-        }
-
-        private readonly EXTStructuralMetadataRoot _root;
-
-        public int FeatureCount { get; set; }
-        public int? NullFeatureId { get; set; }
-        public int? Attribute { get; set; }
-        public string Label { get; set; }
-        public int? PropertyTableIndex { get; set; }
-
-        public Texture Texture { get; set; }
-        public IReadOnlyList<int> Channels { get; set; }
-
-        public bool Equals(IMeshFeatureIDInfo other)
-        {
-            if (other == null) return false;
-            if (this.FeatureCount != other.FeatureCount) return false;
-            if (this.NullFeatureId != other.NullFeatureId) return false;
-            if (this.Attribute != other.Attribute) return false;
-            if (this.Label != other.Label) return false;
-            if (this.PropertyTableIndex != other.PropertyTableIndex) return false;
-
-            return true;
-        }
-    }
-
     /// <remarks>
     /// Use <see cref="MeshExtInstanceFeatures.CreateFeatureId"/> to create an instance of this class.
     /// </remarks>    
@@ -93,17 +12,6 @@ namespace SharpGLTF.Schema2.Tiles3D
     {
         #region lifecycle
 
-        /*
-        public MeshExtInstanceFeatureID(int featureCount, int? attribute = null, int? propertyTable = null, string label = null, int? nullFeatureId = null)
-        {
-            FeatureCount = featureCount;
-            Attribute = attribute;
-            Label = label;
-            PropertyTableIndex = propertyTable;
-            NullFeatureId = nullFeatureId;
-        }*/
-
-
         internal MeshExtInstanceFeatureID() { }
 
         #endregion

+ 52 - 0
src/SharpGLTF.Ext.3DTiles/Schema2/FeatureIDBuilder.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+
+namespace SharpGLTF.Schema2.Tiles3D
+{
+    public sealed class FeatureIDBuilder : IMeshFeatureIDInfo , IEquatable<IMeshFeatureIDInfo>
+    {
+        public FeatureIDBuilder(int featureCount, int? attribute = null, string label = null, int? nullFeatureId = null)
+        {
+            FeatureCount = featureCount;
+            Label = label;
+            Attribute = attribute;
+            NullFeatureId = nullFeatureId;
+        }
+
+        public FeatureIDBuilder(PropertyTable table, int? attribute = null, string label = null, int? nullFeatureId = null):
+            this(table.Count, attribute, label, nullFeatureId)
+        {
+            _root = table.LogicalParent;
+            PropertyTableIndex = table.LogicalIndex;
+        }
+
+        private readonly EXTStructuralMetadataRoot _root;
+
+        public int FeatureCount { get; set; }
+        public int? NullFeatureId { get; set; }
+        public int? Attribute { get; set; }
+        public string Label { get; set; }
+        public int? PropertyTableIndex { get; set; }
+
+        public Texture Texture { get; set; }
+        public IReadOnlyList<int> Channels { get; set; }
+
+        public bool Equals(IMeshFeatureIDInfo other)
+        {
+            if (other == null) return false;
+            if (FeatureCount != other.FeatureCount) return false;
+            if (NullFeatureId != other.NullFeatureId) return false;
+            if (Attribute != other.Attribute) return false;
+            if (Label != other.Label) return false;
+            if (PropertyTableIndex != other.PropertyTableIndex) return false;
+
+            if(other is FeatureIDBuilder featureIdBuilder)
+            {
+                if (Texture != featureIdBuilder.Texture) return false;
+                if (Channels != featureIdBuilder.Channels) return false;
+            }
+
+            return true;
+        }
+    }
+}

+ 38 - 0
src/SharpGLTF.Ext.3DTiles/Schema2/IMeshFeatureIDInfo.cs

@@ -0,0 +1,38 @@
+namespace SharpGLTF.Schema2.Tiles3D
+{
+    /// <summary>
+    /// Mesh Feature Ids
+    /// </summary>
+    /// <remarks>
+    /// Implemented by <see cref="MeshExtInstanceFeatureID"/> and <see cref="MeshExtMeshFeatureID"/>
+    /// </remarks>
+    public interface IMeshFeatureIDInfo
+    {
+        /// <summary>
+        /// The number of unique features in the attribute or texture.
+        /// </summary>
+        public int FeatureCount { get; set; }
+
+        /// <summary>
+        /// A value that indicates that no feature is associated with this vertex or texel.
+        /// </summary>
+        public int? NullFeatureId { get; set; }
+
+        /// <summary>
+        /// An attribute containing feature IDs. When `attribute` and `texture` are omitted the 
+        /// feature IDs are assigned to vertices by their index.
+        /// </summary>
+        public int? Attribute { get; set; }
+
+        /// <summary>
+        /// A label assigned to this feature ID set. Labels must be alphanumeric identifiers 
+        /// matching the regular expression `^[a-zA-Z_][a-zA-Z0-9_]*$`.
+        /// </summary>
+        public string Label { get; set; }
+
+        /// <summary>
+        /// The index of the property table containing per-feature property values. Only applicable when using the `EXT_structural_metadata` extension.
+        /// </summary>
+        public int? PropertyTableIndex { get; set; }        
+    }
+}

+ 1 - 0
tests/SharpGLTF.Cesium.Tests/ExtInstanceFeaturesTests.cs

@@ -17,6 +17,7 @@ namespace SharpGLTF.Schema2.Tiles3D
         }
 
         [Test(Description = "Creates a gpu_instancing glTF from a tree with Cesium EXT_Instance_Features")]
+        // Sample model structure is from https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_instance_features
         public void AddExtGpuInstanceFeatures()
         {
             var settings = SceneBuilderSchema2Settings.WithGpuInstancing;

+ 3 - 1
tests/SharpGLTF.Cesium.Tests/ExtMeshFeaturesTests.cs

@@ -23,12 +23,13 @@ namespace SharpGLTF.Schema2.Tiles3D
         }
 
         [Test(Description = "Test for settting the FeatureIds with vertex attributes. See sample https://github.com/CesiumGS/3d-tiles-samples/blob/main/glTF/EXT_mesh_features/FeatureIdAttribute")]
+        // In the sample html code, there is a shader that uses the feature ID to color the triangle
         public void FeaturesIdAttributeTest()
         {
             TestContext.CurrentContext.AttachGltfValidatorLinks();
 
             // Create a triangle with feature ID custom vertex attribute
-            var featureId = 1;
+            var featureId = 2;
             var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);
 
             var mesh = new MeshBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>("mesh");
@@ -70,6 +71,7 @@ namespace SharpGLTF.Schema2.Tiles3D
         }
 
         [Test(Description = "Test for settting the FeatureIds with a texture. See sample https://github.com/CesiumGS/3d-tiles-samples/blob/main/glTF/EXT_mesh_features/FeatureIdTexture")]
+        // In the sample html code, there is a shader that uses the feature'ID from the texture to color the 2 triangles
         public void FeaturesIdTextureTest()
         {
             TestContext.CurrentContext.AttachGltfValidatorLinks();