|
|
@@ -14,50 +14,22 @@ namespace SharpGLTF.Schema2
|
|
|
{
|
|
|
#region meshes
|
|
|
|
|
|
- public static Mesh CreateMesh<TvP, TvM, TvS>(this ModelRoot root, MeshBuilder<Materials.MaterialBuilder, TvP, TvM, TvS> meshBuilder)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
- {
|
|
|
- return root.CreateMeshes(meshBuilder).First();
|
|
|
- }
|
|
|
-
|
|
|
- public static Mesh CreateMesh<TvP, TvM, TvS>(this ModelRoot root, MeshBuilder<Material, TvP, TvM, TvS> meshBuilder)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
- {
|
|
|
- return root.CreateMeshes(meshBuilder).First();
|
|
|
- }
|
|
|
-
|
|
|
- public static Mesh CreateMesh<TMaterial, TvP, TvM, TvS>(this ModelRoot root, Func<TMaterial, Material> materialEvaluator, MeshBuilder<TMaterial, TvP, TvM, TvS> meshBuilder)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
+ public static Mesh CreateMesh(this ModelRoot root, IMeshBuilder<Materials.MaterialBuilder> mesh)
|
|
|
{
|
|
|
- return root.CreateMeshes(materialEvaluator, meshBuilder).First();
|
|
|
+ return root.CreateMeshes(mesh).First();
|
|
|
}
|
|
|
|
|
|
- public static IReadOnlyList<Mesh> CreateMeshes<TvP, TvM, TvS>(this ModelRoot root, params MeshBuilder<Material, TvP, TvM, TvS>[] meshBuilders)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
+ public static Mesh CreateMesh<TMaterial>(this ModelRoot root, Func<TMaterial, Material> materialEvaluator, IMeshBuilder<TMaterial> mesh)
|
|
|
{
|
|
|
- return root.CreateMeshes(m => m, meshBuilders);
|
|
|
+ return root.CreateMeshes<TMaterial>(materialEvaluator, mesh).First();
|
|
|
}
|
|
|
|
|
|
- public static IReadOnlyList<Mesh> CreateMeshes<TvP, TvM, TvS>(this ModelRoot root, params MeshBuilder<Materials.MaterialBuilder, TvP, TvM, TvS>[] meshBuilders)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
+ public static IReadOnlyList<Mesh> CreateMeshes(this ModelRoot root, params IMeshBuilder<Materials.MaterialBuilder>[] meshBuilders)
|
|
|
{
|
|
|
return root.CreateMeshes(mb => root.CreateMaterial(mb), meshBuilders);
|
|
|
}
|
|
|
|
|
|
- public static IReadOnlyList<Mesh> CreateMeshes<TMaterial, TvP, TvM, TvS>(this ModelRoot root, Func<TMaterial, Material> materialEvaluator, params MeshBuilder<TMaterial, TvP, TvM, TvS>[] meshBuilders)
|
|
|
- where TvP : struct, IVertexGeometry
|
|
|
- where TvM : struct, IVertexMaterial
|
|
|
- where TvS : struct, IVertexSkinning
|
|
|
+ public static IReadOnlyList<Mesh> CreateMeshes<TMaterial>(this ModelRoot root, Func<TMaterial, Material> materialEvaluator, params IMeshBuilder<TMaterial>[] meshBuilders)
|
|
|
{
|
|
|
Guard.NotNull(root, nameof(root));
|
|
|
Guard.NotNull(materialEvaluator, nameof(materialEvaluator));
|
|
|
@@ -89,46 +61,38 @@ namespace SharpGLTF.Schema2
|
|
|
return dstMeshes;
|
|
|
}
|
|
|
|
|
|
- public static IReadOnlyList<Mesh> CreateMeshes(this ModelRoot root, params IMeshBuilder<Materials.MaterialBuilder>[] meshBuilders)
|
|
|
- {
|
|
|
- return root.CreateMeshes(mb => root.CreateMaterial(mb), meshBuilders);
|
|
|
- }
|
|
|
+ #endregion
|
|
|
|
|
|
- public static IReadOnlyList<Mesh> CreateMeshes<TMaterial>(this ModelRoot root, Func<TMaterial, Material> materialEvaluator, params IMeshBuilder<TMaterial>[] meshBuilders)
|
|
|
- {
|
|
|
- Guard.NotNull(root, nameof(root));
|
|
|
- Guard.NotNull(materialEvaluator, nameof(materialEvaluator));
|
|
|
- Guard.NotNull(meshBuilders, nameof(meshBuilders));
|
|
|
+ #region accessors
|
|
|
|
|
|
- foreach (var m in meshBuilders) m.Validate();
|
|
|
+ public static MeshPrimitive WithIndicesAutomatic(this MeshPrimitive primitive, PrimitiveType primitiveType)
|
|
|
+ {
|
|
|
+ var root = primitive.LogicalParent.LogicalParent;
|
|
|
|
|
|
- // create a new material for every unique material in the mesh builders.
|
|
|
- var mapMaterials = meshBuilders
|
|
|
- .SelectMany(item => item.Primitives)
|
|
|
- .Select(item => item.Material)
|
|
|
- .Distinct()
|
|
|
- .ToDictionary(m => m, m => materialEvaluator(m));
|
|
|
+ primitive.DrawPrimitiveType = primitiveType;
|
|
|
+ primitive.SetIndexAccessor(null);
|
|
|
|
|
|
- // creates meshes and primitives using MemoryAccessors using a single, shared vertex and index buffer
|
|
|
- var srcMeshes = PackedMeshBuilder<TMaterial>
|
|
|
- .PackMeshes(meshBuilders)
|
|
|
- .ToList();
|
|
|
+ return primitive;
|
|
|
+ }
|
|
|
|
|
|
- var dstMeshes = new List<Mesh>();
|
|
|
+ public static MeshPrimitive WithIndicesAccessor(this MeshPrimitive primitive, PrimitiveType primitiveType, IReadOnlyList<Int32> values)
|
|
|
+ {
|
|
|
+ var root = primitive.LogicalParent.LogicalParent;
|
|
|
|
|
|
- foreach (var srcMesh in srcMeshes)
|
|
|
- {
|
|
|
- var dstMesh = srcMesh.CreateSchema2Mesh(root, m => mapMaterials[m]);
|
|
|
+ // create an index buffer and fill it
|
|
|
+ var view = root.UseBufferView(new Byte[4 * values.Count], 0, null, 0, BufferMode.ELEMENT_ARRAY_BUFFER);
|
|
|
+ var array = new IntegerArray(view.Content);
|
|
|
+ array.Fill(values);
|
|
|
|
|
|
- dstMeshes.Add(dstMesh);
|
|
|
- }
|
|
|
+ var accessor = root.CreateAccessor();
|
|
|
|
|
|
- return dstMeshes;
|
|
|
- }
|
|
|
+ accessor.SetIndexData(view, 0, values.Count, IndexEncodingType.UNSIGNED_INT);
|
|
|
|
|
|
- #endregion
|
|
|
+ primitive.DrawPrimitiveType = primitiveType;
|
|
|
+ primitive.SetIndexAccessor(accessor);
|
|
|
|
|
|
- #region accessors
|
|
|
+ return primitive;
|
|
|
+ }
|
|
|
|
|
|
public static MeshPrimitive WithVertexAccessor(this MeshPrimitive primitive, string attribute, IReadOnlyList<Single> values)
|
|
|
{
|
|
|
@@ -200,62 +164,51 @@ namespace SharpGLTF.Schema2
|
|
|
return primitive;
|
|
|
}
|
|
|
|
|
|
- public static MeshPrimitive WithIndicesAutomatic(this MeshPrimitive primitive, PrimitiveType primitiveType)
|
|
|
+ public static MeshPrimitive WithVertexAccessors(this MeshPrimitive primitive, IReadOnlyList<VertexPosition> vertices)
|
|
|
{
|
|
|
- var root = primitive.LogicalParent.LogicalParent;
|
|
|
-
|
|
|
- primitive.DrawPrimitiveType = primitiveType;
|
|
|
- primitive.SetIndexAccessor(null);
|
|
|
+ var xvertices = vertices
|
|
|
+ .Select(item => new VertexBuilder<VertexPosition, VertexEmpty, VertexEmpty>(item))
|
|
|
+ .ToList();
|
|
|
|
|
|
- return primitive;
|
|
|
+ return primitive.WithVertexAccessors(xvertices);
|
|
|
}
|
|
|
|
|
|
- public static MeshPrimitive WithIndicesAccessor(this MeshPrimitive primitive, PrimitiveType primitiveType, IReadOnlyList<Int32> values)
|
|
|
+ public static MeshPrimitive WithVertexAccessors(this MeshPrimitive primitive, IReadOnlyList<VertexPositionNormal> vertices)
|
|
|
{
|
|
|
- var root = primitive.LogicalParent.LogicalParent;
|
|
|
-
|
|
|
- // create an index buffer and fill it
|
|
|
- var view = root.UseBufferView(new Byte[4 * values.Count], 0, null, 0, BufferMode.ELEMENT_ARRAY_BUFFER);
|
|
|
- var array = new IntegerArray(view.Content);
|
|
|
- array.Fill(values);
|
|
|
-
|
|
|
- var accessor = root.CreateAccessor();
|
|
|
-
|
|
|
- accessor.SetIndexData(view, 0, values.Count, IndexEncodingType.UNSIGNED_INT);
|
|
|
-
|
|
|
- primitive.DrawPrimitiveType = primitiveType;
|
|
|
- primitive.SetIndexAccessor(accessor);
|
|
|
+ var xvertices = vertices
|
|
|
+ .Select(item => new VertexBuilder<VertexPositionNormal, VertexEmpty, VertexEmpty>(item))
|
|
|
+ .ToList();
|
|
|
|
|
|
- return primitive;
|
|
|
+ return primitive.WithVertexAccessors(xvertices);
|
|
|
}
|
|
|
|
|
|
- public static MeshPrimitive WithVertexAccessors<TvP>(this MeshPrimitive primitive, IReadOnlyList<TvP> vertices)
|
|
|
- where TvP : struct, Geometry.VertexTypes.IVertexGeometry
|
|
|
+ public static MeshPrimitive WithVertexAccessors<TvP, TvM>(this MeshPrimitive primitive, IReadOnlyList<(TvP, TvM)> vertices)
|
|
|
+ where TvP : struct, IVertexGeometry
|
|
|
+ where TvM : struct, IVertexMaterial
|
|
|
{
|
|
|
var xvertices = vertices
|
|
|
- .Select(item => new Geometry.VertexBuilder<TvP, Geometry.VertexTypes.VertexEmpty, Geometry.VertexTypes.VertexEmpty>(item))
|
|
|
+ .Select(item => new VertexBuilder<TvP, TvM, VertexEmpty>(item.Item1, item.Item2))
|
|
|
.ToList();
|
|
|
|
|
|
return primitive.WithVertexAccessors(xvertices);
|
|
|
}
|
|
|
|
|
|
- public static MeshPrimitive WithVertexAccessors<TvP, TvM>(this MeshPrimitive primitive, IReadOnlyList<(TvP, TvM)> vertices)
|
|
|
- where TvP : struct, Geometry.VertexTypes.IVertexGeometry
|
|
|
- where TvM : struct, Geometry.VertexTypes.IVertexMaterial
|
|
|
+ public static MeshPrimitive WithVertexAccessors<TvP, TvM, TvS>(this MeshPrimitive primitive, IReadOnlyList<(TvP, TvM, TvS)> vertices)
|
|
|
+ where TvP : struct, IVertexGeometry
|
|
|
+ where TvM : struct, IVertexMaterial
|
|
|
+ where TvS : struct, IVertexSkinning
|
|
|
{
|
|
|
var xvertices = vertices
|
|
|
- .Select(item => new Geometry.VertexBuilder<TvP, TvM, Geometry.VertexTypes.VertexEmpty>(item.Item1, item.Item2))
|
|
|
+ .Select(item => new VertexBuilder<TvP, TvM, TvS>(item.Item1, item.Item2, item.Item3))
|
|
|
.ToList();
|
|
|
|
|
|
return primitive.WithVertexAccessors(xvertices);
|
|
|
}
|
|
|
|
|
|
- public static MeshPrimitive WithVertexAccessors<TvP, TvM, TvS>(this MeshPrimitive primitive, IReadOnlyList<Geometry.VertexBuilder<TvP, TvM, TvS>> vertices)
|
|
|
- where TvP : struct, Geometry.VertexTypes.IVertexGeometry
|
|
|
- where TvM : struct, Geometry.VertexTypes.IVertexMaterial
|
|
|
- where TvS : struct, Geometry.VertexTypes.IVertexSkinning
|
|
|
+ public static MeshPrimitive WithVertexAccessors<TVertex>(this MeshPrimitive primitive, IReadOnlyList<TVertex> vertices)
|
|
|
+ where TVertex : IVertexBuilder
|
|
|
{
|
|
|
- var memAccessors = Geometry.VertexTypes.VertexUtils.CreateVertexMemoryAccessors(new[] { vertices }).First();
|
|
|
+ var memAccessors = VertexUtils.CreateVertexMemoryAccessors(new[] { vertices }).First();
|
|
|
|
|
|
return primitive.WithVertexAccessors(memAccessors);
|
|
|
}
|