diff --git a/mesh/DMesh3Changes.cs b/mesh/DMesh3Changes.cs index 694a78b6..384d96eb 100644 --- a/mesh/DMesh3Changes.cs +++ b/mesh/DMesh3Changes.cs @@ -5,6 +5,117 @@ namespace g3 { + /// + /// Mesh change for vertex deformations. Currently minimal support for initializing buffers. + /// AppendNewVertex() can be used to accumulate modified vertices and their initial positions. + /// + public class ModifyVerticesMeshChange + { + public DVector ModifiedV; + public DVector OldPositions, NewPositions; + public DVector OldNormals, NewNormals; + public DVector OldColors, NewColors; + public DVector OldUVs, NewUVs; + + public Action OnApplyF; + public Action 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(); + NewPositions = new DVector(); + OldPositions = new DVector(); + if (mesh.HasVertexNormals && (components & MeshComponents.VertexNormals) != 0) { + NewNormals = new DVector(); + OldNormals = new DVector(); + } + if (mesh.HasVertexColors && (components & MeshComponents.VertexColors) != 0) { + NewColors = new DVector(); + OldColors = new DVector(); + } + if (mesh.HasVertexUVs && (components & MeshComponents.VertexUVs) != 0) { + NewUVs = new DVector(); + OldUVs = new DVector(); + } + } + + } + + + + + + + + + + + + + /// /// Remove triangles from mesh and store necessary data to be able to reverse the change. /// Vertex and Triangle IDs will be restored on Revert()