Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MeshDataBuilder<VT> where VT : IVertex
- {
- public float MergeTolerance { get; set; }
- public List<VT> Vertices { get; private set; }
- public List<UInt32> Indices { get; private set; }
- public bool ClockwiseOrder { get; set; }
- private int smoothingVertexStart = 0;
- private int smoothingIndexStart = 0;
- private bool smoothEnabled = false;
- public MeshDataBuilder()
- {
- this.MergeTolerance = 0.00001f;
- this.Vertices = new List<VT>();
- this.Indices = new List<UInt32>();
- this.ClockwiseOrder = false;
- }
- public void Begin()
- {
- this.Vertices.Clear();
- this.Indices.Clear();
- smoothingVertexStart = 0;
- smoothingIndexStart = 0;
- smoothEnabled = false;
- }
- public void End()
- {
- FinishSmoothingGroup();
- }
- public void NewSmoothingGroup(bool smooth)
- {
- if (smoothingVertexStart > 0)
- FinishSmoothingGroup();
- smoothingVertexStart = Vertices.Count();
- smoothingIndexStart = Indices.Count();
- smoothEnabled = smooth;
- }
- struct SortedVertexInfo: IComparable<SortedVertexInfo>
- {
- public int Index;
- public int NewIndex;
- public int PreviousRemoveCounter;
- public float LengthSquared;
- public Vector3 Position;
- public bool Removed;
- public int CompareTo(SortedVertexInfo other)
- {
- if (other.LengthSquared == LengthSquared) return 0;
- return other.LengthSquared < LengthSquared ? -1 : 1;
- }
- };
- private void RemoveDuplicateVertices()
- {
- int verticesCount = this.Vertices.Count - smoothingVertexStart;
- int indicesCount = this.Indices.Count - smoothingIndexStart;
- SortedVertexInfo[] sortedVertices = new SortedVertexInfo[verticesCount];
- for (int i = 0; i < verticesCount; ++i)
- {
- Vector3 pos = this.Vertices[i].GetPosition();
- sortedVertices[i].Index = i;
- sortedVertices[i].NewIndex = sortedVertices[i].Index;
- sortedVertices[i].Position = pos;
- sortedVertices[i].LengthSquared = pos.LengthSquared();
- sortedVertices[i].Removed = false;
- sortedVertices[i].PreviousRemoveCounter = 0;
- }
- Array.Sort<SortedVertexInfo>(sortedVertices);
- int removeCounter = 0;
- int vi = 0;
- while (vi < verticesCount)
- {
- int newIndex = sortedVertices[vi].Index;
- Vector3 comparedPosition = sortedVertices[vi].Position;
- float comparedLength = sortedVertices[vi].LengthSquared;
- ++vi;
- int vj = vi;
- while (vj < verticesCount && sortedVertices[vj].LengthSquared == comparedLength)
- {
- SortedVertexInfo svi = sortedVertices[vj];
- if (!svi.Removed && svi.Position == comparedPosition)
- {
- svi.Removed = true;
- svi.NewIndex = newIndex;
- sortedVertices[vj] = svi;
- ++removeCounter;
- }
- ++vj;
- }
- }
- Console.WriteLine(removeCounter.ToString());
- Array.Sort<SortedVertexInfo>(sortedVertices, delegate(SortedVertexInfo v1, SortedVertexInfo v2)
- {
- return v1.Index - v2.Index;
- });
- int previousRemoveCounter = 0;
- for (int i = 0; i < verticesCount; ++i)
- {
- sortedVertices[i].PreviousRemoveCounter = previousRemoveCounter;
- if (sortedVertices[i].Removed)
- ++previousRemoveCounter;
- }
- for (int i = 0; i < indicesCount; ++i)
- {
- int ind = (int)this.Indices[i] - smoothingVertexStart;
- if (sortedVertices[ind].Removed)
- ind = sortedVertices[ind].NewIndex;
- this.Indices[i] = (UInt32)(ind - sortedVertices[ind].PreviousRemoveCounter + smoothingVertexStart);
- }
- List<VT> newVertices = new List<VT>();
- foreach (SortedVertexInfo svi in sortedVertices)
- {
- if (!svi.Removed)
- newVertices.Add(this.Vertices[svi.Index + smoothingVertexStart]);
- }
- this.Vertices.RemoveRange(smoothingVertexStart, verticesCount);
- this.Vertices.AddRange(newVertices);
- }
- private int AddVertex(VT vertex)
- {
- this.Vertices.Add(vertex);
- return this.Vertices.Count - 1;
- }
- public void AddTriangle(VT vertex1, VT vertex2, VT vertex3)
- {
- UInt32 i1 = (UInt32)AddVertex(vertex1);
- UInt32 i2 = (UInt32)AddVertex(vertex2);
- UInt32 i3 = (UInt32)AddVertex(vertex3);
- if (this.ClockwiseOrder)
- {
- this.Indices.Add(i1);
- this.Indices.Add(i2);
- this.Indices.Add(i3);
- }
- else
- {
- this.Indices.Add(i1);
- this.Indices.Add(i3);
- this.Indices.Add(i2);
- }
- }
- public void AddQuad(VT vertex1, VT vertex2, VT vertex3, VT vertex4)
- {
- UInt32 i1 = (UInt32)AddVertex(vertex1);
- UInt32 i2 = (UInt32)AddVertex(vertex2);
- UInt32 i3 = (UInt32)AddVertex(vertex3);
- UInt32 i4 = (UInt32)AddVertex(vertex4);
- if (this.ClockwiseOrder)
- {
- this.Indices.Add(i1);
- this.Indices.Add(i2);
- this.Indices.Add(i4);
- this.Indices.Add(i2);
- this.Indices.Add(i3);
- this.Indices.Add(i4);
- }
- else
- {
- this.Indices.Add(i1);
- this.Indices.Add(i4);
- this.Indices.Add(i2);
- this.Indices.Add(i2);
- this.Indices.Add(i4);
- this.Indices.Add(i3);
- }
- }
- private void FinishSmoothingGroup()
- {
- if (smoothEnabled)
- {
- RemoveDuplicateVertices();
- for (int i = smoothingIndexStart; i < this.Indices.Count; i += 3)
- {
- Vector3 p1 = this.Vertices[(int)this.Indices[i]].GetPosition();
- Vector3 p2 = this.Vertices[(int)this.Indices[i + 1]].GetPosition();
- Vector3 p3 = this.Vertices[(int)this.Indices[i + 2]].GetPosition();
- Vector3 faceNormal = Vector3.Cross(p3 - p1, p2 - p1);
- faceNormal.Normalize();
- for (int j = 0; j < 3; ++j)
- {
- VT vertex = this.Vertices[(int)this.Indices[i + j]];
- vertex.SetNormal(vertex.GetNormal() + faceNormal);
- this.Vertices[(int)this.Indices[i + j]] = vertex;
- }
- }
- for (int i = smoothingVertexStart; i < this.Vertices.Count; ++i)
- {
- VT vertex = this.Vertices[i];
- Vector3 normal = vertex.GetNormal();
- normal.Normalize();
- vertex.SetNormal(normal);
- this.Vertices[i] = vertex;
- }
- }
- else
- {
- for (int i = smoothingIndexStart; i < this.Indices.Count; i += 3)
- {
- Vector3 p1 = this.Vertices[(int)this.Indices[i]].GetPosition();
- Vector3 p2 = this.Vertices[(int)this.Indices[i + 1]].GetPosition();
- Vector3 p3 = this.Vertices[(int)this.Indices[i + 2]].GetPosition();
- Vector3 faceNormal = Vector3.Cross(p3 - p1, p2 - p1);
- faceNormal.Normalize();
- for (int j = 0; j < 3; ++j)
- {
- VT vertex = this.Vertices[(int)this.Indices[i + j]];
- vertex.SetNormal(faceNormal);
- this.Vertices[(int)this.Indices[i + j]] = vertex;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement