Search Unity

Coloring Triangles without splitting the mesh?

Discussion in 'Shaders' started by IustinianDrug, Feb 12, 2019.

  1. IustinianDrug

    IustinianDrug

    Joined:
    Dec 7, 2015
    Posts:
    2
    Hi, I am basically trying to change the vertex color of the triangles based on triangleIndex raycasting. Managed to do that, however I'm looking for a way to color individual triangles instead of the vertices having a spread color to all of the triangles they belong to. Is There a way of doing that either in Shader Code or C#, without having to split the mesh into individual triangles? Thank you.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There are two ways to go about this.

    The “simple” method is to use a nointerpolation modifier on the color interpolant in the shader. This will use the vertex color from the triangle’s first vertex across the entire surface. This only works if every triangle in your mesh has a unique first vertex, which isn’t likely.

    The second method is to store your colors in an array and index into it using SV_PrimitiveID in the fragment shader. You may want to use a structured buffer instead of an array to get passed the fairly low array size limit. This does require keeping the buffer and the mesh used in sync, and precludes any use of batching which will alter the primitive IDs.

    Both of these solutions only work with custom vertex fragment shaders. You can’t do either of them with Surface Shaders.