Sfoglia il codice sorgente

Added general interface IMorphTargetBuilder to make it possible for IMeshBuilder to UseMorphTarget.

Petar Tasev 5 anni fa
parent
commit
81ab609427

+ 5 - 0
src/SharpGLTF.Toolkit/Geometry/MeshBuilder.cs

@@ -130,6 +130,11 @@ namespace SharpGLTF.Geometry
             return new MorphTargetBuilder<TMaterial, TvG, TvS, TvM>(this, index);
         }
 
+        IMorphTargetBuilder IMeshBuilder<TMaterial>.UseMorphTarget(int index)
+        {
+            return UseMorphTarget(index);
+        }
+
         private PrimitiveBuilder<TMaterial, TvG, TvM, TvS> _UsePrimitive((TMaterial Material, int PrimType) key)
         {
             if (!_Primitives.TryGetValue(key, out PrimitiveBuilder<TMaterial, TvG, TvM, TvS> primitive))

+ 2 - 0
src/SharpGLTF.Toolkit/Geometry/MeshBuilderToolkit.cs

@@ -18,6 +18,8 @@ namespace SharpGLTF.Geometry
 
         IPrimitiveBuilder UsePrimitive(TMaterial material, int primitiveVertexCount = 3);
 
+        IMorphTargetBuilder UseMorphTarget(int index);
+
         IMeshBuilder<TMaterial> Clone(Func<TMaterial, TMaterial> materialCloneCallback = null);
 
         void Validate();

+ 29 - 1
src/SharpGLTF.Toolkit/Geometry/MorphTargetBuilder.cs

@@ -153,10 +153,21 @@ namespace SharpGLTF.Geometry
         #endregion
     }
 
+    public interface IMorphTargetBuilder
+    {
+        IReadOnlyCollection<Vector3> Positions { get; }
+        IReadOnlyCollection<IVertexGeometry> Vertices { get; }
+        IReadOnlyList<IVertexGeometry> GetVertices(Vector3 position);
+
+        void SetVertex(IVertexGeometry meshVertex, IVertexGeometry morphVertex);
+        void SetVertexDelta(Vector3 key, VertexGeometryDelta delta);
+        void SetVertexDelta(IVertexGeometry meshVertex, VertexGeometryDelta delta);
+    }
+
     /// <summary>
     /// Utility class to edit the Morph targets of a mesh.
     /// </summary>
-    public sealed class MorphTargetBuilder<TMaterial, TvG, TvS, TvM>
+    public sealed class MorphTargetBuilder<TMaterial, TvG, TvS, TvM> : IMorphTargetBuilder
             where TvG : struct, IVertexGeometry
             where TvM : struct, IVertexMaterial
             where TvS : struct, IVertexSkinning
@@ -210,6 +221,8 @@ namespace SharpGLTF.Geometry
 
         public IReadOnlyCollection<TvG> Vertices => _Vertices.Keys;
 
+        IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => (IReadOnlyList<IVertexGeometry>)(IReadOnlyCollection<TvG>)_Vertices.Keys;
+
         #endregion
 
         #region API
@@ -219,6 +232,11 @@ namespace SharpGLTF.Geometry
             return _Positions.TryGetValue(position, out List<TvG> geos) ? (IReadOnlyList<TvG>)geos : Array.Empty<TvG>();
         }
 
+        IReadOnlyList<IVertexGeometry> IMorphTargetBuilder.GetVertices(Vector3 position)
+        {
+            return _Positions.TryGetValue(position, out List<TvG> geos) ? (IReadOnlyList<IVertexGeometry>)geos : Array.Empty<IVertexGeometry>();
+        }
+
         public void SetVertexDelta(Vector3 key, VertexGeometryDelta delta)
         {
             if (_Positions.TryGetValue(key, out List<TvG> geos))
@@ -240,6 +258,11 @@ namespace SharpGLTF.Geometry
             }
         }
 
+        void IMorphTargetBuilder.SetVertex(IVertexGeometry meshVertex, IVertexGeometry morphVertex)
+        {
+            SetVertex(meshVertex.ConvertToGeometry<TvG>(), morphVertex.ConvertToGeometry<TvG>());
+        }
+
         public void SetVertexDelta(TvG meshVertex, VertexGeometryDelta delta)
         {
             if (_Vertices.TryGetValue(meshVertex, out List<(PrimitiveBuilder<TMaterial, TvG, TvM, TvS>, int)> val))
@@ -253,6 +276,11 @@ namespace SharpGLTF.Geometry
             }
         }
 
+        void IMorphTargetBuilder.SetVertexDelta(IVertexGeometry meshVertex, VertexGeometryDelta delta)
+        {
+            SetVertexDelta(meshVertex.ConvertToGeometry<TvG>(), delta);
+        }
+
         #endregion
     }
 }