Browse Source

use schema or schemaUri

Bert Temme 2 years ago
parent
commit
9f865490f5

+ 34 - 17
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -1,4 +1,5 @@
 using SharpGLTF.Validation;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 
@@ -9,52 +10,57 @@ namespace SharpGLTF.Schema2
         // sample see https://github.com/CesiumGS/3d-tiles-samples/blob/main/glTF/EXT_structural_metadata/SimplePropertyTexture/SimplePropertyTexture.gltf
         public static void SetPropertyTexture(
             this ModelRoot modelRoot,
-            StructuralMetadataSchema schema,
-            PropertyTexture propertyTexture)
+            PropertyTexture propertyTexture,
+            StructuralMetadataSchema schema = null,
+            Uri schemaUri = null
+            )
         {
-            if (schema == null || propertyTexture == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
+            if (propertyTexture == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
-            ext.Schema = schema;
             ext.PropertyTextures.Clear();
             ext.PropertyTextures.Add(propertyTexture);
+            ext.AddSchema(schema, schemaUri);
         }
 
         public static void SetPropertyAttribute(
             this ModelRoot modelRoot,
-            PropertyAttribute propertyAttribute)
+            PropertyAttribute propertyAttribute,
+            StructuralMetadataSchema schema = null,
+            Uri schemaUri = null)
         {
             if (propertyAttribute == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
-            // Todo: Check if this is correct, it's a kinda workaround
-            // for the OneOf issue. https://github.com/CesiumGS/glTF/blob/proposal-EXT_structural_metadata/extensions/2.0/Vendor/EXT_structural_metadata/schema/glTF.EXT_structural_metadata.schema.json#L53
-            // Here we use schemaUri but not schema
-            ext.SchemaUri = "MetadataSchema.json"; 
             ext.PropertyAttributes.Clear();
             ext.PropertyAttributes.Add(propertyAttribute);
+            ext.AddSchema(schema, schemaUri);
         }
 
         public static void SetPropertyTable(
             this ModelRoot modelRoot,
-            StructuralMetadataSchema schema,
-            Dictionary<string, List<int>> attributes
+            Dictionary<string, List<int>> attributes,
+            string name = "PropertyTable",
+            StructuralMetadataSchema schema = null,
+            Uri schemaUri = null
             )
         {
-            if (schema == null || attributes == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
+            if (attributes == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
-            ext.Schema = schema;
             ext.PropertyTables.Clear();
-            ext.PropertyTables.Add(GetPropertyTable(modelRoot, schema, attributes));
+            ext.PropertyTables.Add(GetPropertyTable(modelRoot, attributes, name));
+            ext.AddSchema(schema, schemaUri);
         }
 
 
         private static PropertyTable GetPropertyTable(
             ModelRoot modelRoot,
-            StructuralMetadataSchema schema,
             Dictionary<string, List<int>> attributes,
-            string name = "PropertyTable")
+            string name = "PropertyTable",
+            StructuralMetadataSchema schema = null
+            // todo: use? Uri schemaUri = null
+            )
         {
             var propertyTable = new PropertyTable(name, attributes.FirstOrDefault().Value.Count);
 
@@ -79,7 +85,6 @@ namespace SharpGLTF.Schema2
 
             return propertyTable;
         }
-
     }
 
     public partial class EXTStructuralMetaDataRoot
@@ -94,6 +99,18 @@ namespace SharpGLTF.Schema2
             _propertyTextures = new List<PropertyTexture>();
         }
 
+        internal void AddSchema(StructuralMetadataSchema schema, Uri schemaUri)
+        {
+            if (schema != null)
+            {
+                Schema = schema;
+            }
+            if (schemaUri != null)
+            {
+                SchemaUri = schemaUri.ToString();
+            }
+        }
+
         internal List<PropertyTable> PropertyTables
         {
             get { return _propertyTables; }

+ 6 - 6
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -38,7 +38,7 @@ namespace SharpGLTF.Cesium
                     {
                         // intensity values is based on x-axis values
                         // classification of points is 0 or 1 (random)
-                        var vt0 = VertexBuilder.GetVertexPointcloud(new Vector3(x, y, z), redColor, x, rand.Next(0,2));
+                        var vt0 = VertexBuilder.GetVertexPointcloud(new Vector3(x, y, z), redColor, x, rand.Next(0, 2));
 
                         pointCloud.AddPoint(vt0);
                     }
@@ -60,7 +60,7 @@ namespace SharpGLTF.Cesium
             propertyAttribute.Properties["intensity"] = intensityProperty;
             propertyAttribute.Properties["classification"] = classificationProperty;
 
-            model.SetPropertyAttribute(propertyAttribute);
+            model.SetPropertyAttribute(propertyAttribute, schemaUri: new Uri("MetadataSchema.json", UriKind.Relative));
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             model.AttachToCurrentTest("cesium_ext_structural_metadata_with_pointcloud_attributes.glb");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_with_pointcloud_attributes.gltf");
@@ -68,18 +68,18 @@ namespace SharpGLTF.Cesium
         }
 
 
-        [Test(Description ="First test with ext_structural_metadata")]
+        [Test(Description = "First test with ext_structural_metadata")]
         public void TriangleWithMetadataTest()
         {
-            var  model = GetTriangleModel();
+            var model = GetTriangleModel();
 
             var schema = GetSampleSchema();
 
             var attribute = new List<int>() { 100 };
             var dict = new Dictionary<string, List<int>>();
             dict["age"] = attribute;
-            model.SetPropertyTable(schema, dict);
-            
+            model.SetPropertyTable(dict, schema: schema);
+
             // create files
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.glb");