Skip to content

Commit

Permalink
Made some more methods AOT friendly.
Browse files Browse the repository at this point in the history
vpenades committed Jan 5, 2024

Verified

This commit was signed with the committer’s verified signature.
amomchilov Alexander Momchilov
1 parent de297f0 commit c735acc
Showing 10 changed files with 413 additions and 163 deletions.
34 changes: 8 additions & 26 deletions src/SharpGLTF.Toolkit/Geometry/MeshBuilderToolkit.cs
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public interface IMeshBuilder<TMaterial>
void Validate();
}

static class MeshBuilderToolkit
static partial class MeshBuilderToolkit
{
public static VertexBuilder<VertexGeometryDelta, VertexMaterialDelta, VertexEmpty>[] GetMorphTargetVertices(this IPrimitiveMorphTargetReader morphTarget, int vertexCount)
{
@@ -115,6 +115,7 @@ public static Schema2.EncodingType GetOptimalJointEncoding<TMaterial>(this IEnum

return maxIndex < 256 ? Schema2.EncodingType.UNSIGNED_BYTE : Schema2.EncodingType.UNSIGNED_SHORT;
}


public static IMeshBuilder<TMaterial> CreateMeshBuilderFromVertexAttributes
<
@@ -123,31 +124,12 @@ public static IMeshBuilder<TMaterial> CreateMeshBuilderFromVertexAttributes
#endif
TMaterial>(params string[] vertexAttributes)
{
Type meshType = GetMeshBuilderType(typeof(TMaterial), vertexAttributes);

var mesh = Activator.CreateInstance(meshType, string.Empty);

return mesh as IMeshBuilder<TMaterial>;
}

#if NET6_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
public static Type GetMeshBuilderType
(
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Type materialType, string[] vertexAttributes)
{
var tvg = VertexUtils.GetVertexGeometryType(vertexAttributes);
var tvm = VertexUtils.GetVertexMaterialType(vertexAttributes);
var tvs = VertexUtils.GetVertexSkinningType(vertexAttributes);

var meshType = typeof(MeshBuilder<,,,>);

return meshType.MakeGenericType(materialType, tvg, tvm, tvs);
}
return VertexUtils
.GetVertexBuilderType(vertexAttributes) // get a vertex factory from attributes
.BuilderFactory
.Invoke() // create a single vertex
.CreateCompatibleMesh<TMaterial>(); // create a mesh with the given vertex format.
}

public static IReadOnlyDictionary<Vector3, Vector3> CalculateSmoothNormals<TMaterial>(this IMeshBuilder<TMaterial> srcMesh)
{
2 changes: 2 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/PrimitiveBuilder.cs
Original file line number Diff line number Diff line change
@@ -111,6 +111,8 @@ protected PrimitiveBuilder(MeshBuilder<TMaterial, TvG, TvM, TvS> mesh, Primitive
#endif
public Type VertexType => typeof(VertexBuilder<TvG, TvM, TvS>);

public Func<IVertexBuilder> VertexFactory => () => new VertexBuilder<TvG, TvM, TvS>();

/// <summary>
/// Gets the list of vertices used by this primitive.
/// </summary>
1 change: 1 addition & 0 deletions src/SharpGLTF.Toolkit/Geometry/PrimitiveInterfaces.cs
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ public interface IPrimitiveBuilder
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Type VertexType { get; }
Func<IVertexBuilder> VertexFactory { get; }

void SetVertexDelta(int morphTargetIndex, int vertexIndex, VertexGeometryDelta geometryDelta, VertexMaterialDelta materialDelta);

39 changes: 24 additions & 15 deletions src/SharpGLTF.Toolkit/Geometry/VertexBufferColumns.cs
Original file line number Diff line number Diff line change
@@ -169,10 +169,12 @@ private void _ApplyTransform(Transforms.IGeometryTransform transform)
this.Positions = _IsolateColumn(this.Positions); // Position, normal and tangent can be modified by morphing and skinning
this.Normals = _IsolateColumn(this.Normals);
this.Tangents = _IsolateColumn(this.Tangents);
this.Colors0 = _IsolateColumn(this.Colors0); // colors0,1 and texCoords0,1 can be modified by morphing
this.Colors0 = _IsolateColumn(this.Colors0); // colors0,1 and texCoords 0,1,2,3 can be modified by morphing
this.Colors1 = _IsolateColumn(this.Colors1);
this.TexCoords0 = _IsolateColumn(this.TexCoords0);
this.TexCoords1 = _IsolateColumn(this.TexCoords1);
this.TexCoords2 = _IsolateColumn(this.TexCoords2);
this.TexCoords3 = _IsolateColumn(this.TexCoords3);

// prepare animation data, if available

@@ -185,6 +187,8 @@ private void _ApplyTransform(Transforms.IGeometryTransform transform)
Vector4[] morphColors1 = null;
Vector2[] morphTexcrd0 = null;
Vector2[] morphTexcrd1 = null;
Vector2[] morphTexcrd2 = null;
Vector2[] morphTexcrd3 = null;

if (_MorphTargets != null)
{
@@ -195,6 +199,8 @@ private void _ApplyTransform(Transforms.IGeometryTransform transform)
if (_MorphTargets.All(item => item.Colors1 != null)) morphColors1 = new Vector4[this.MorphTargets.Count];
if (_MorphTargets.All(item => item.TexCoords0 != null)) morphTexcrd0 = new Vector2[this.MorphTargets.Count];
if (_MorphTargets.All(item => item.TexCoords1 != null)) morphTexcrd1 = new Vector2[this.MorphTargets.Count];
if (_MorphTargets.All(item => item.TexCoords2 != null)) morphTexcrd2 = new Vector2[this.MorphTargets.Count];
if (_MorphTargets.All(item => item.TexCoords3 != null)) morphTexcrd3 = new Vector2[this.MorphTargets.Count];
}

// loop over every vertex
@@ -252,6 +258,18 @@ private void _ApplyTransform(Transforms.IGeometryTransform transform)
_FillMorphData(morphTexcrd1, vc => vc.TexCoords1[i]);
TexCoords1[i] = morphMaterial.MorphTexCoord(TexCoords1[i], morphTexcrd1);
}

if (this.TexCoords2 != null)
{
_FillMorphData(morphTexcrd2, vc => vc.TexCoords2[i]);
TexCoords1[2] = morphMaterial.MorphTexCoord(TexCoords2[i], morphTexcrd2);
}

if (this.TexCoords3 != null)
{
_FillMorphData(morphTexcrd3, vc => vc.TexCoords3[i]);
TexCoords3[2] = morphMaterial.MorphTexCoord(TexCoords3[i], morphTexcrd3);
}
}
}

@@ -318,11 +336,8 @@ public VertexBufferColumns AddMorphTarget()
#endregion

#region API - Vertex indexing

#if NET6_0_OR_GREATER
[return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
public Type GetCompatibleVertexType()

public (Type BuilderType, Func<IVertexBuilder> BuilderFactory) GetCompatibleVertexType()
{
var hasNormals = Normals != null;
var hasTangents = hasNormals && Tangents != null;
@@ -394,21 +409,15 @@ private TvS GetVertexSkinning<TvS>(int index)
}

return s;
}
}

public IVertexBuilder GetVertex
(
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Type vertexType,
int index)
public IVertexBuilder GetVertex(Func<IVertexBuilder> factory, int index)
{
var g = GetVertexGeometry<VertexPositionNormalTangent>(index);
var m = GetVertexMaterial<VertexColor2Texture2>(index);
var s = GetVertexSkinning<VertexJoints8>(index);

return new VertexBuilder(g, m, s).ConvertToType(vertexType);
return new VertexBuilder(g, m, s).ConvertToType(factory);
}

public VertexBuilder<TvG, TvM, VertexEmpty> GetVertex<TvG, TvM>(int index)
27 changes: 17 additions & 10 deletions src/SharpGLTF.Toolkit/Geometry/VertexBuilder.cs
Original file line number Diff line number Diff line change
@@ -33,6 +33,9 @@ public interface IVertexBuilder
/// </summary>
/// <param name="skinning">A <see cref="IVertexSkinning"/> set.</param>
void SetSkinning(IVertexSkinning skinning);


IMeshBuilder<TMaterial> CreateCompatibleMesh<TMaterial>(string name = null);
}

/// <summary>
@@ -331,6 +334,11 @@ public static MeshBuilder<TvG, TvM, TvS> CreateCompatibleMesh(string name = null
return new MeshBuilder<TvG, TvM, TvS>(name);
}

IMeshBuilder<TMaterial> IVertexBuilder.CreateCompatibleMesh<TMaterial>(string name)
{
return new MeshBuilder<TMaterial, TvG, TvM, TvS>(name);
}

#pragma warning restore CA1000 // Do not declare static members on generic types

readonly IVertexGeometry IVertexBuilder.GetGeometry() { return this.Geometry; }
@@ -449,7 +457,7 @@ public readonly VertexBuilder<TvG, TvM, TvS> WithSkinning(IEnumerable<(int Index
v.Skinning.SetBindings(sparse);

return v;
}
}

#endregion
}
@@ -496,17 +504,11 @@ public VertexBuilder(IVertexGeometry g, IVertexMaterial m, IVertexSkinning s)

public void SetMaterial(IVertexMaterial material) { this.Material = material; }

public void SetSkinning(IVertexSkinning skinning) { this.Skinning = skinning; }
public void SetSkinning(IVertexSkinning skinning) { this.Skinning = skinning; }

public readonly IVertexBuilder ConvertToType
(
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Type vertexType
)
public readonly IVertexBuilder ConvertToType(Func<IVertexBuilder> factory)
{
var v = (IVertexBuilder)Activator.CreateInstance(vertexType);
var v = factory.Invoke();

v.SetGeometry(Geometry);
v.SetMaterial(Material);
@@ -515,6 +517,11 @@ Type vertexType
return v;
}

IMeshBuilder<TMaterial> IVertexBuilder.CreateCompatibleMesh<TMaterial>(string name)
{
throw new NotImplementedException();
}

#endregion
}
}
Loading

0 comments on commit c735acc

Please sign in to comment.