Grasshopper

algorithmic modeling for Rhino

Hello,

I'm trying to get mesh vertex color using colorAt method, but I always get as output 255 (even on painted meshes).

I also found this topic on the forum talking about the same issue:

http://www.grasshopper3d.com/forum/topics/getting-meshcolour-c

Anyway to solve?

Thank you.

Paolo

Views: 1997

Replies to This Discussion

If you're not picky about exactly interpolating the color in between vertices (which mesh.ColorAt tries to do) you can just find the closest mesh vertex, get its index, and use Mesh.VertexColors[i];

the quick way I pulled together to do this...

(where M is a mesh, P is a Point3d, and A is the output color)

PointCloud pc = new PointCloud(M.Vertices.ToPoint3dArray());

A = M.VertexColors[pc.ClosestPoint(P)];

Thank you for your reply Andrew.

Actually what you describe is exactly what I've been doing so far. I was looking for more precision indeed. probably I should calculate it myself using barycentric coordinates...

Hey guys,

I tried to find a work around using the barycentric coordinates from T propriety in MeshPoint class, but I was getting strange results on some of the faces ( the script outputs opposite values , like black where actually the color is white).Then I thought that the evaluation is influenced by face vertices order and I tried to get Triangle propriety to use the correct vertex order in the evaluation: I don't know why the Triangle propriety outputs nothing.

At the very end I decide to code everything from scratch. Probably It is a bit convoluted, or not optimized, but it works..and that's fine ( and I learned something new about mesh shading :) ).

See attached the code.

Cheers.

Attachments:

You can find the original code of the ColorAt function at RhinoCommon's Git repository:

https://raw.githubusercontent.com/mcneel/rhinocommon/master/c/on_me...

I tried to port it to C#. See the attached file and code below:

Color ColorAt(Mesh mesh, int faceIndex, double t0, double t1, double t2, double t3)
  {
    // int rc = -1;
    var color = Rhino.Display.Color4f.Black;

    if( mesh.VertexColors.Count != 0)
    {
      // test to see if face exists
      if( faceIndex >= 0 && faceIndex < mesh.Faces.Count )
      {
        /// Barycentric quad coordinates for the point on the mesh
        /// face mesh.Faces[FaceIndex].

        /// If the face is a triangle
        /// disregard T[3] (it should be set to 0.0).

        /// If the face is
        /// a quad and is split between vertexes 0 and 2, then T[3]
        /// will be 0.0 when point is on the triangle defined by vi[0],
        /// vi[1], vi[2]

        /// T[1] will be 0.0 when point is on the
        /// triangle defined by vi[0], vi[2], vi[3].

        /// If the face is a
        /// quad and is split between vertexes 1 and 3, then T[2] will
        /// be -1 when point is on the triangle defined by vi[0],
        /// vi[1], vi[3]

        /// and m_t[0] will be -1 when point is on the
        /// triangle defined by vi[1], vi[2], vi[3].

        MeshFace face = mesh.Faces[faceIndex];

        // Collect data for barycentric evaluation.
        Color p0, p1, p2;

        if(face.IsTriangle)
        {
          p0 = mesh.VertexColors[face.A];
          p1 = mesh.VertexColors[face.B];
          p2 = mesh.VertexColors[face.C];
        }
        else
        {
          if( t3 == 0 )
          { // point is on subtriangle {0,1,2}
            p0 = mesh.VertexColors[face.A];
            p1 = mesh.VertexColors[face.B];
            p2 = mesh.VertexColors[face.C];
          }
          else if( t1 == 0 )
          { // point is on subtriangle {0,2,3}
            p0 = mesh.VertexColors[face.A];
            p1 = mesh.VertexColors[face.C];
            p2 = mesh.VertexColors[face.D];
            //t0 = t0;
            t1 = t2;
            t2 = t3;
          }
          else if( t2 == -1 )
          { // point is on subtriangle {0,1,3}
            p0 = mesh.VertexColors[face.A];
            p1 = mesh.VertexColors[face.B];
            p2 = mesh.VertexColors[face.D];
            //t0 = t0;
            //t1 = t1;
            t2 = t3;
          }
          else
          { // point must be on remaining subtriangle {1,2,3}
            p0 = mesh.VertexColors[face.B];
            p1 = mesh.VertexColors[face.C];
            p2 = mesh.VertexColors[face.D];
            t0 = t1;
            t1 = t2;
            t2 = t3;
          }
        }

/**
        double r = t0 * p0.FractionRed() + t1 * p1.FractionRed() + t2 * p2.FractionRed();
        double g = t0 * p0.FractionGreen() + t1 * p1.FractionGreen() + t2 * p2.FractionGreen();
       double b = t0 * p0.FractionBlue() + t1 * p1.FractionBlue() + t2 * p2.FractionBlue();

        ON_Color color;
        color.SetFractionalRGB(r, g, b);

        unsigned int abgr = (unsigned int)color;
        rc = (int) ABGR_to_ARGB(abgr);
  **/
        var c0 = new Rhino.Display.Color4f(p0);
        var c1 = new Rhino.Display.Color4f(p1);
        var c2 = new Rhino.Display.Color4f(p2);
        float s0 = (float) t0;
        float s1 = (float) t1;
        float s2 = (float) t2;

        float R = s0 * c0.R + s1 * c1.R + s2 * c2.R;
        float G = s0 * c0.G + s1 * c1.G + s2 * c2.G;
        float B = s0 * c0.B + s1 * c1.B + s2 * c2.B;
        color = new Rhino.Display.Color4f(R, G, B, 1);
      }
    }
    return color.AsSystemColor();
  }

Attachments:

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service