public void FastUpdateVertices(DMesh3 source, int[] source_vertices, bool bCopyNormals = false, bool bCopyColors = false) { int NV = source_vertices.Length; Vector3[] vertices = new Vector3[NV]; for (int i = 0; i < NV; ++i) { vertices[i] = (Vector3)source.GetVertex(source_vertices[i]); } mesh.vertices = vertices; if (bCopyNormals && source.HasVertexNormals) { Vector3[] normals = new Vector3[NV]; for (int i = 0; i < NV; ++i) { normals[i] = (Vector3)source.GetVertexNormal(source_vertices[i]); } mesh.normals = normals; } if (bCopyColors && source.HasVertexColors) { Color[] colors = new Color[NV]; for (int i = 0; i < NV; ++i) { colors[i] = (Color)source.GetVertexColor(source_vertices[i]); } mesh.colors = colors; } }
protected override void SolveInstance(IGH_DataAccess DA) { DMesh3_goo goo = null; DA.GetData(0, ref goo); DMesh3 mesh = new DMesh3(goo.Value); List <Point3d> vertices = new List <Point3d>(); List <MeshFace> faces = new List <MeshFace>(); List <Color> cols = new List <Color>(); List <int> v_i = new List <int>(); List <int> f_i = new List <int>(); foreach (var ind in mesh.VertexIndices()) { v_i.Add(ind); vertices.Add(mesh.GetVertex(ind).ToRhinoPt()); var col = mesh.GetVertexColor(ind); cols.Add(Color.FromArgb((int)(col.x * 255), (int)(col.y * 255), (int)(col.z * 255))); } foreach (var ind in mesh.TriangleIndices()) { f_i.Add(ind); var tri = mesh.GetTriangle(ind); faces.Add(new MeshFace(tri.a, tri.b, tri.c)); } DA.SetDataList(0, v_i); DA.SetDataList(1, vertices); DA.SetDataList(2, f_i); DA.SetDataList(3, faces); DA.SetDataList(4, cols); }
public fMesh(int[] triangles, DMesh3 source, int[] source_vertices, bool bCopyNormals = false, bool bCopyColors = false, bool bCopyUVs = false) { int NV = source_vertices.Length; Vector3[] vertices = new Vector3[NV]; for (int i = 0; i < NV; ++i) { vertices[i] = (Vector3)source.GetVertex(source_vertices[i]); } Mesh m = new Mesh(); m.vertices = vertices; if (NV > 65000 || triangles.Length / 3 > 65000) { m.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; } m.triangles = triangles; if (bCopyNormals && source.HasVertexNormals) { Vector3[] normals = new Vector3[NV]; for (int i = 0; i < NV; ++i) { normals[i] = (Vector3)source.GetVertexNormal(source_vertices[i]); } m.normals = normals; } else { m.RecalculateNormals(); } if (bCopyColors && source.HasVertexColors) { Color[] colors = new Color[NV]; for (int i = 0; i < NV; ++i) { colors[i] = (Color)source.GetVertexColor(source_vertices[i]); } m.colors = colors; } if (bCopyUVs && source.HasVertexUVs) { Vector2[] uvs = new Vector2[NV]; for (int i = 0; i < NV; ++i) { uvs[i] = source.GetVertexUV(source_vertices[i]); } m.uv = uvs; } mesh = m; }
public fMesh(int[] triangles, DMesh3 source, int[] source_vertices, bool bCopyNormals = false, bool bCopyColors = false) { int NV = source_vertices.Length; Vector3[] vertices = new Vector3[NV]; for (int i = 0; i < NV; ++i) { vertices[i] = (Vector3)source.GetVertex(source_vertices[i]); } Mesh m = new Mesh(); m.vertices = vertices; m.triangles = triangles; if (bCopyNormals && source.HasVertexNormals) { Vector3[] normals = new Vector3[NV]; for (int i = 0; i < NV; ++i) { normals[i] = (Vector3)source.GetVertexNormal(source_vertices[i]); } m.normals = normals; } else { m.RecalculateNormals(); } if (bCopyColors && source.HasVertexColors) { Color[] colors = new Color[NV]; for (int i = 0; i < NV; ++i) { colors[i] = (Color)source.GetVertexColor(source_vertices[i]); } m.colors = colors; } unitymesh = m; }
/// <summary> /// copy vertex positions from sourceMesh. /// [TODO] perhaps can refactor into a call to EditAndUpdateMesh() ? /// </summary> public void UpdateVertices(DMesh3 sourceMesh, bool bNormals = true, bool bColors = true) { if (sourceMesh.MaxVertexID != mesh.MaxVertexID) { throw new Exception("DMeshSO.UpdateVertexPositions: not enough positions provided!"); } bNormals &= sourceMesh.HasVertexNormals; if (bNormals && mesh.HasVertexNormals == false) { mesh.EnableVertexNormals(Vector3f.AxisY); } bColors &= sourceMesh.HasVertexColors; if (bColors && mesh.HasVertexColors == false) { mesh.EnableVertexColors(Colorf.White); } lock (mesh_write_lock) { foreach (int vid in mesh.VertexIndices()) { Vector3d sourceV = sourceMesh.GetVertex(vid); mesh.SetVertex(vid, sourceV); if (bNormals) { Vector3f sourceN = sourceMesh.GetVertexNormal(vid); mesh.SetVertexNormal(vid, sourceN); } if (bColors) { Vector3f sourceC = sourceMesh.GetVertexColor(vid); mesh.SetVertexColor(vid, sourceC); } } } fast_mesh_update(bNormals, bColors); post_mesh_modified(); }
public virtual void Update() { base.begin_update(); if (MeshSource == null) { throw new Exception("MeshVertexDisplacementOp: must set valid MeshSource to compute!"); } if (DisplacementSource == null) { throw new Exception("MeshVertexDisplacementOp: must set valid DisplacementSource to compute!"); } DMesh3 meshIn = MeshSource.GetDMeshUnsafe(); IVectorDisplacement displace = DisplacementSource.GetDisplacement(); if (displace.Count != 0 && displace.Count != meshIn.MaxVertexID) { throw new Exception("MeshVertexDisplacementOp: inconsistent counts " + displace.Count.ToString() + " != " + meshIn.MaxVertexID.ToString()); } DMesh3 mesh = new DMesh3(meshIn, MeshHints.None); //if (!mesh.HasVertexNormals) // MeshNormals.QuickCompute(mesh); if (displace.Count > 0) { gParallel.ForEach(mesh.VertexIndices(), (vid) => { Vector3d dv = displace.GetDisplacementForIndex(vid); //Vector3f n = mesh.GetVertexNormal(vid); Vector3d v = mesh.GetVertex(vid); v += dv; mesh.SetVertex(vid, v); }); if (enable_heat_map) { // compute max displace len ColorMap map = new ColorMap(); map.AddPoint(0, Colorf.CornflowerBlue); float d = (float)HeatMapMaxDistance; map.AddPoint(d, Colorf.Orange); map.AddPoint(2 * d, Colorf.VideoYellow); map.AddPoint(4 * d, Colorf.VideoRed); map.AddPoint(-d, Colorf.VideoMagenta); float max_displace = d; gParallel.ForEach(mesh.VertexIndices(), (vid) => { Vector3f dv = (Vector3f)displace.GetDisplacementForIndex(vid); Vector3f n = mesh.GetVertexNormal(vid); float sign = n.Dot(dv) > 0 ? 1 : -1; Colorf c = map.Linear(dv.Length * sign); Colorf existing_c = mesh.GetVertexColor(vid); float preserve__max = max_displace / 2; float t = MathUtil.Clamp(dv.Length / preserve__max, 0.0f, 1.0f); c = (1.0f - t) * existing_c + (t) * c; mesh.SetVertexColor(vid, c); //float t = MathUtil.Clamp(dv.Length / max_displace, -1.0f, 1.0f); //mesh.SetVertexColor(vid, t * Colorf.Orange); }); } } MeshNormals.QuickCompute(mesh); DisplacedMesh = mesh; base.complete_update(); }