50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using global::System.Numerics;
|
|
|
|
namespace Pilz.S3DFileParser
|
|
{
|
|
public class Mesh
|
|
{
|
|
public List<Vertex> Vertices { get; private set; } = new List<Vertex>();
|
|
public List<Normal> Normals { get; private set; } = new List<Normal>();
|
|
public List<UV> UVs { get; private set; } = new List<UV>();
|
|
public List<VertexColor> VertexColors { get; private set; } = new List<VertexColor>();
|
|
public List<Face> Faces { get; private set; } = new List<Face>();
|
|
|
|
public void CenterModel()
|
|
{
|
|
CenterModel(new[] { this });
|
|
}
|
|
|
|
public static void CenterModel(IEnumerable<Mesh> meshes)
|
|
{
|
|
int avgX = 0;
|
|
int avgY = 0;
|
|
int avgZ = 0;
|
|
long vertsCount = 0L;
|
|
foreach (Mesh m in meshes)
|
|
{
|
|
foreach (Vertex v in m.Vertices)
|
|
{
|
|
avgX = (int)(avgX + v.X);
|
|
avgY = (int)(avgY + v.Y);
|
|
avgZ = (int)(avgZ + v.Z);
|
|
}
|
|
|
|
vertsCount += m.Vertices.Count;
|
|
}
|
|
|
|
var avg = new Vector3(avgX, avgY, avgZ);
|
|
avg /= new Vector3(vertsCount);
|
|
foreach (Mesh m in meshes)
|
|
{
|
|
foreach (Vertex v in m.Vertices)
|
|
{
|
|
v.X -= avg.X;
|
|
v.Y -= avg.Y;
|
|
v.Z -= avg.Z;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |