Change Mesh VertexColors


#1

Hi,

I’m trying to get different colors at different heights of a mesh.

What I do is:

  Protected Overrides Function RunCommand(ByVal doc As RhinoDoc, ByVal mode As RunMode) As Result

            Dim MyMesh As Rhino.DocObjects.ObjRef = Nothing
            Dim Rc As Rhino.Commands.Result = Rhino.Input.RhinoGet.GetOneObject("Click on Mesh", False, DocObjects.ObjectType.Mesh, MyMesh)
            If Rc <> Rhino.Commands.Result.Success Then
                Return Nothing
            End If

            Dim Mesh As New Geometry.Mesh
            Mesh = MyMesh.Mesh.Duplicate
            Dim Count As Integer = Mesh.Faces.Count
            For i = 0 To Count - 1
                Dim Pt3D As Geometry.Point3d = Mesh.Faces.GetFaceCenter(i)
                If Pt3D.Z < 15 Then
                    Mesh.VertexColors.SetColor(i, Drawing.Color.Red)
                ElseIf Pt3D.Z < 25 Then
                    Mesh.VertexColors.SetColor(i, Drawing.Color.Orange)
                Else
                    Mesh.VertexColors.SetColor(i, Drawing.Color.Green)
                End If
            Next
            doc.Objects.Replace(MyMesh, Mesh)
            doc.Views.Redraw()
            Return Result.Success
        End Function

And I get:

Colors are being placed randomly. It’s not using the Pt3d.Z right. After red orange should come. why is there green? What am I doing wrong?

I’m getting the center of a face. Checks its height and give it a color and replace the mesh.

I guess the i isnt taking the same VertexXolors.Setcolor as Faces.GetFaceCenter


#2

Oke was looking at faces but should have looked into vertices:


Protected Overrides Function RunCommand(ByVal doc As RhinoDoc, ByVal mode As RunMode) As Result

            Dim MyMesh As Rhino.DocObjects.ObjRef = Nothing
            Dim Rc As Rhino.Commands.Result = Rhino.Input.RhinoGet.GetOneObject("Click on Mesh", False, DocObjects.ObjectType.Mesh, MyMesh)
            If Rc <> Rhino.Commands.Result.Success Then
                Return Nothing
            End If

            Dim NewMesh As Geometry.Mesh = TestMesh(MyMesh.Mesh.Duplicate)

            doc.Objects.Replace(MyMesh, NewMesh)
            doc.Views.Redraw()

            Return Result.Success
        End Function

        Private Function TestMesh(ByVal Mesh As Geometry.Mesh) As Geometry.Mesh
            If Mesh.VertexColors.Tag.Id <> Me.Id Then
                Dim colors As System.Drawing.Color() = New System.Drawing.Color(Mesh.Vertices.Count - 1) {}
                For i As Integer = 0 To Mesh.Vertices.Count - 1
                    Dim z As Double = Mesh.Vertices(i).Z

                    If z < 10 Then
                        colors(i) = Drawing.Color.Red
                    Else
                        colors(i) = Drawing.Color.Green
                    End If
                Next
                Mesh.VertexColors.SetColors(colors)
            End If

            Return Mesh
        End Function


#3

Hi Jordy,

i think i refers to the index of mesh faces. You cannot use the same index for the vertex or ?

c.


#4

Ah like that.
My 2nd post works though :slight_smile:

So I can set the colors now.
But how do I return it too its original? ^^


#5

Hi Jordy, without code, you could use the command _RebuildMesh with code, you would need to recreate the mesh as you did but without vertex colors. Just use the face indices, vertices and vertex normals.

c.