Skip to content

Commit

Permalink
Merge pull request #38 from ptasev/imeshbuilder-usemorphtarget
Browse files Browse the repository at this point in the history
Made it possible to call UseMorphTarget on IMeshBuilder<TMaterial>
  • Loading branch information
vpenades committed Apr 11, 2020
2 parents 1e9caa3 + b21c8d7 commit 958b5e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/MeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public MorphTargetBuilder<TMaterial, TvG, TvS, TvM> UseMorphTarget(int index)
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))
Expand Down
2 changes: 2 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/MeshBuilderToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface IMeshBuilder<TMaterial>

IPrimitiveBuilder UsePrimitive(TMaterial material, int primitiveVertexCount = 3);

IMorphTargetBuilder UseMorphTarget(int index);

IMeshBuilder<TMaterial> Clone(Func<TMaterial, TMaterial> materialCloneCallback = null);

void Validate();
Expand Down
30 changes: 29 additions & 1 deletion src/SharpGLTF.Toolkit/Geometry/MorphTargetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,21 @@ internal void SetMorphTargets(PrimitiveMorphTargetBuilder<TvG> other, IReadOnlyD
#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
Expand Down Expand Up @@ -210,6 +221,8 @@ internal MorphTargetBuilder(MeshBuilder<TMaterial, TvG, TvM, TvS> mesh, int morp

public IReadOnlyCollection<TvG> Vertices => _Vertices.Keys;

IReadOnlyCollection<IVertexGeometry> IMorphTargetBuilder.Vertices => (IReadOnlyList<IVertexGeometry>)(IReadOnlyCollection<TvG>)_Vertices.Keys;

#endregion

#region API
Expand All @@ -219,6 +232,11 @@ public IReadOnlyList<TvG> GetVertices(Vector3 position)
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))
Expand All @@ -240,6 +258,11 @@ public void SetVertex(TvG meshVertex, TvG morphVertex)
}
}

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))
Expand All @@ -253,6 +276,11 @@ public void SetVertexDelta(TvG meshVertex, VertexGeometryDelta delta)
}
}

void IMorphTargetBuilder.SetVertexDelta(IVertexGeometry meshVertex, VertexGeometryDelta delta)
{
SetVertexDelta(meshVertex.ConvertToGeometry<TvG>(), delta);
}

#endregion
}
}

0 comments on commit 958b5e4

Please sign in to comment.