Skip to content

Commit

Permalink
added ModifyVerticesMeshChange
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Apr 25, 2018
1 parent f7622ca commit 36ae0fb
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions mesh/DMesh3Changes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,117 @@

namespace g3
{
/// <summary>
/// Mesh change for vertex deformations. Currently minimal support for initializing buffers.
/// AppendNewVertex() can be used to accumulate modified vertices and their initial positions.
/// </summary>
public class ModifyVerticesMeshChange
{
public DVector<int> ModifiedV;
public DVector<Vector3d> OldPositions, NewPositions;
public DVector<Vector3f> OldNormals, NewNormals;
public DVector<Vector3f> OldColors, NewColors;
public DVector<Vector2f> OldUVs, NewUVs;

public Action<ModifyVerticesMeshChange> OnApplyF;
public Action<ModifyVerticesMeshChange> OnRevertF;

public ModifyVerticesMeshChange(DMesh3 mesh, MeshComponents wantComponents = MeshComponents.All)
{
initialize_buffers(mesh, wantComponents);
}


public int AppendNewVertex(DMesh3 mesh, int vid)
{
int idx = ModifiedV.Length;
ModifiedV.Add(vid);
OldPositions.Add(mesh.GetVertex(vid));
NewPositions.Add(OldPositions[idx]);
if (NewNormals != null) {
OldNormals.Add(mesh.GetVertexNormal(vid));
NewNormals.Add(OldNormals[idx]);
}
if (NewColors != null) {
OldColors.Add(mesh.GetVertexColor(vid));
NewColors.Add(OldColors[idx]);
}
if (NewUVs != null) {
OldUVs.Add(mesh.GetVertexUV(vid));
NewUVs.Add(OldUVs[idx]);
}
return idx;
}

public void Apply(DMesh3 mesh)
{
int N = ModifiedV.size;
for (int i = 0; i < N; ++i) {
int vid = ModifiedV[i];
mesh.SetVertex(vid, NewPositions[i]);
if (NewNormals != null)
mesh.SetVertexNormal(vid, NewNormals[i]);
if (NewColors != null)
mesh.SetVertexColor(vid, NewColors[i]);
if (NewUVs != null)
mesh.SetVertexUV(vid, NewUVs[i]);
}
if (OnApplyF != null)
OnApplyF(this);
}


public void Revert(DMesh3 mesh)
{
int N = ModifiedV.size;
for (int i = 0; i < N; ++i) {
int vid = ModifiedV[i];
mesh.SetVertex(vid, OldPositions[i]);
if (NewNormals != null)
mesh.SetVertexNormal(vid, OldNormals[i]);
if (NewColors != null)
mesh.SetVertexColor(vid, OldColors[i]);
if (NewUVs != null)
mesh.SetVertexUV(vid, OldUVs[i]);
}
if (OnRevertF != null)
OnRevertF(this);
}


void initialize_buffers(DMesh3 mesh, MeshComponents components)
{
ModifiedV = new DVector<int>();
NewPositions = new DVector<Vector3d>();
OldPositions = new DVector<Vector3d>();
if (mesh.HasVertexNormals && (components & MeshComponents.VertexNormals) != 0) {
NewNormals = new DVector<Vector3f>();
OldNormals = new DVector<Vector3f>();
}
if (mesh.HasVertexColors && (components & MeshComponents.VertexColors) != 0) {
NewColors = new DVector<Vector3f>();
OldColors = new DVector<Vector3f>();
}
if (mesh.HasVertexUVs && (components & MeshComponents.VertexUVs) != 0) {
NewUVs = new DVector<Vector2f>();
OldUVs = new DVector<Vector2f>();
}
}

}













/// <summary>
/// Remove triangles from mesh and store necessary data to be able to reverse the change.
/// Vertex and Triangle IDs will be restored on Revert()
Expand Down

0 comments on commit 36ae0fb

Please sign in to comment.