Browse Source

add support for Vector3 and Matrix4x4 with noData

Bert Temme 1 year ago
parent
commit
3541263d8c

+ 47 - 3
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.StructuralMetadataRoot.cs

@@ -1435,17 +1435,31 @@ namespace SharpGLTF.Schema2
             }
 
 
-            public StructuralMetadataClassProperty WithVector3Type()
+            public StructuralMetadataClassProperty WithVector3Type(Vector3? noData = null)
             {
                 Type = ElementType.VEC3;
                 ComponentType = DataType.FLOAT32;
+
+                if (noData != null)
+                {
+                    var jsonNode = ToJsonArray(noData.Value);
+                    _noData = jsonNode;
+
+                }
                 return this;
             }
 
-            public StructuralMetadataClassProperty WithMatrix4x4Type()
+            public StructuralMetadataClassProperty WithMatrix4x4Type(Matrix4x4? noData = null)
             {
                 Type = ElementType.MAT4;
                 ComponentType = DataType.FLOAT32;
+
+                if (noData != null)
+                {
+                    var jsonNode = ToJsonArray(noData.Value);
+                    _noData = jsonNode;
+                }
+
                 return this;
             }
 
@@ -1529,7 +1543,6 @@ namespace SharpGLTF.Schema2
             public StructuralMetadataClassProperty WithVector3ArrayType(int? count = null, Vector3? noData = null)
             {
                 var property = WithArrayType(ELEMENTTYPE.VEC3, DATATYPE.FLOAT32, count);
-                // todo: how to set noData for vector3?
                 return property;
             }
             public StructuralMetadataClassProperty WithMatrix4x4ArrayType(int? count = null)
@@ -1584,6 +1597,37 @@ namespace SharpGLTF.Schema2
             }
 
 
+            private static JsonArray ToJsonArray(Vector3 vector3)
+            {
+                var floats = new float[] { vector3.X, vector3.Y, vector3.Z };
+                var  jsonNode = ToJsonArray(floats);
+                return jsonNode;
+            }
+
+            private static JsonArray ToJsonArray(Matrix4x4 matrix4x4)
+            {
+                var floats = new float[] {
+                    matrix4x4.M11, matrix4x4.M12, matrix4x4.M13, matrix4x4.M14,
+                    matrix4x4.M21, matrix4x4.M22, matrix4x4.M23, matrix4x4.M24,
+                    matrix4x4.M31, matrix4x4.M32, matrix4x4.M33, matrix4x4.M34,
+                    matrix4x4.M41, matrix4x4.M42, matrix4x4.M43, matrix4x4.M44
+                };
+
+                var jsonNode = ToJsonArray(floats);
+                return jsonNode;
+            }
+
+            private static JsonArray ToJsonArray(float[] floats)
+            {
+                var jsonNode = new JsonArray();
+                foreach (var f in floats)
+                {
+                    jsonNode.Add(f);
+                }
+
+                return jsonNode;
+            }
+
             #endregion
         }
 

+ 18 - 13
tests/SharpGLTF.Ext.3DTiles.Tests/ExtStructuralMetadataTests.cs

@@ -112,7 +112,7 @@ namespace SharpGLTF.Schema2.Tiles3D
 
             var schemaClass = schema.UseClassMetadata("triangles");
 
-            var speciesEnum = schema.UseEnumMetadata("speciesEnum", ("Unspecified", 0), ("Oak", 1), ("Pine", 2), ("Maple",3));
+            var speciesEnum = schema.UseEnumMetadata("speciesEnum", ("Unspecified", 0), ("Oak", 1), ("Pine", 2), ("Maple", 3));
             speciesEnum.Name = "Species";
             speciesEnum.Description = "An example enum for tree species.";
 
@@ -162,10 +162,6 @@ namespace SharpGLTF.Schema2.Tiles3D
                 .UseProperty("float64")
                 .WithFloat64Type(double.MinValue);
 
-            var vector3Property = schemaClass
-                .UseProperty("vector3")
-                .WithVector3Type();
-
             var stringProperty = schemaClass
                 .UseProperty("string")
                 .WithStringType("noData");
@@ -176,6 +172,14 @@ namespace SharpGLTF.Schema2.Tiles3D
                 .WithEnumeration(speciesEnum, "Unspecified")
                 .WithRequired(false);
 
+            var vector3Property = schemaClass
+                .UseProperty("vector3")
+                .WithVector3Type(new Vector3(-10.0f, -10.0f, -10.0f));
+
+            var matrix4x4Property = schemaClass
+                .UseProperty("matrix4x4")
+                .WithMatrix4x4Type(Matrix4x4.Identity * -10);
+
             // todo add array types
 
             var propertyTable = schemaClass.AddPropertyTable(1);
@@ -224,11 +228,6 @@ namespace SharpGLTF.Schema2.Tiles3D
                 .UseProperty(float64Property)
                 .SetValues(double.MinValue);
 
-            // todo: how to set a vector3 to null?
-            propertyTable
-                .UseProperty(vector3Property)
-                .SetValues(new Vector3(0,0,0));
-
             propertyTable
                 .UseProperty(stringProperty)
                 .SetValues("noData");
@@ -237,6 +236,15 @@ namespace SharpGLTF.Schema2.Tiles3D
                 .UseProperty(speciesProperty)
                 .SetValues((short)0);
 
+            propertyTable
+                .UseProperty(vector3Property)
+                .SetValues(new Vector3(10.0f,10.0f,10.0f));
+
+            var m4 = Matrix4x4.Identity;
+            propertyTable
+                .UseProperty(matrix4x4Property)
+                .SetValues(m4);
+
             foreach (var primitive in model.LogicalMeshes[0].Primitives)
             {
                 var featureIdAttribute = new FeatureIDBuilder(1, 0, propertyTable);
@@ -250,9 +258,6 @@ namespace SharpGLTF.Schema2.Tiles3D
             model.AttachToCurrentTest("cesium_ext_structural_minimal_metadata_sample.plotly");
         }
 
-
-
-
         [Test(Description = "MinimalMetadataAttributeSample")]
         public void MinimalMetadataAttributeSample()
         {