Browse Source

use nullable attributeOrTexture in FeatureIDBuilder

Bert Temme 1 year ago
parent
commit
8e8b51371e

+ 17 - 4
src/SharpGLTF.Ext.3DTiles/Schema2/FeatureIDBuilder.cs

@@ -1,21 +1,34 @@
 using System;
 using System.Collections.Generic;
+using OneOf;
 
 namespace SharpGLTF.Schema2.Tiles3D
 {
-    public sealed class FeatureIDBuilder : IMeshFeatureIDInfo , IEquatable<IMeshFeatureIDInfo>
+    public sealed class FeatureIDBuilder : IMeshFeatureIDInfo, IEquatable<IMeshFeatureIDInfo>
     {
-        public FeatureIDBuilder(int featureCount, int? attribute = null, PropertyTable propertyTable = null, string label = null, int? nullFeatureId = null)
+
+        public FeatureIDBuilder(int featureCount, OneOf<int, Texture>? attributeOrTexture, PropertyTable propertyTable = null, string label = null, int? nullFeatureId = null)
         {
             Guard.MustBeGreaterThanOrEqualTo(featureCount, 1, nameof(featureCount));
             FeatureCount = featureCount;
 
+            if (attributeOrTexture != null)
+            {
+                attributeOrTexture.Value.Switch(
+                    attribute => Attribute = attribute,
+                    texture =>
+                    {
+                        Texture = texture;
+                        Channels = new[] { 0 };
+                    }
+                );
+            }
+
             if (propertyTable != null)
             {
                 PropertyTableIndex = propertyTable.LogicalIndex;
             }
 
-            Attribute = attribute;
             Label = label;
             NullFeatureId = nullFeatureId;
         }
@@ -39,7 +52,7 @@ namespace SharpGLTF.Schema2.Tiles3D
             if (Label != other.Label) return false;
             if (PropertyTableIndex != other.PropertyTableIndex) return false;
 
-            if(other is FeatureIDBuilder featureIdBuilder)
+            if (other is FeatureIDBuilder featureIdBuilder)
             {
                 if (Texture != featureIdBuilder.Texture) return false;
                 if (Channels != featureIdBuilder.Channels) return false;

+ 0 - 2
src/SharpGLTF.Ext.3DTiles/SharpGLTF.Ext.3DTiles.csproj

@@ -20,10 +20,8 @@
     <Folder Include="Schema2\Generated\" />
   </ItemGroup>
 
-  <!--
   <ItemGroup>
     <PackageReference Include="OneOf" Version="3.0.263" />
   </ItemGroup>
-  -->
 
 </Project>

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

@@ -38,7 +38,7 @@ namespace SharpGLTF.Schema2.Tiles3D
 
 
             var featureId0 = new FeatureIDBuilder(2, 0, label: "Forests");
-            var featureId1 = new FeatureIDBuilder(9, label: "Trees");            
+            var featureId1 = new FeatureIDBuilder(9, 0, label: "Trees");            
 
             var model = sceneBuilder.ToGltf2(settings);
             model.LogicalNodes[0].AddInstanceFeatureIds(featureId0, featureId1);

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

@@ -106,9 +106,7 @@ namespace SharpGLTF.Schema2.Tiles3D
             var model = scene.ToGltf2();
 
             // Set the FeatureIds, pointing to the red channel of the texture            
-            var featureId = new FeatureIDBuilder(4);
-            featureId.Channels = new int[] { 0 };
-            featureId.Texture = model.LogicalTextures[0];
+            var featureId = new FeatureIDBuilder(4, model.LogicalTextures[0]);
 
             var primitive = model.LogicalMeshes[0].Primitives[0];
             primitive.AddMeshFeatureIds(featureId);