Approach for writing a GLSL fragment shader with a solid color per triangle/face

后端 未结 3 680
盖世英雄少女心
盖世英雄少女心 2021-02-10 02:36

I have vertex and triangle data which contains a color for each triangle (face), not for each vertex. i.e. A single vertex is shared by multiple faces, each face poten

3条回答
  •  有刺的猬
    2021-02-10 02:49

    While you maybe could do this in high end GLSL, the right way to do solid shading is to make unique vertices for every triangle. This is a trivial loop. For every vertex, count how many triangles share it. That's how often you have to replicate it. Make sure your loop to do this is O(n). Then just set each vertex color or normal to that of the triangle. Again one straight loop. Do not bother to optimize for shared colors, it is not worth it.

    Edit much later, because this is a popular answer:

    To do flat per face shading you can interpolate the vertex position in world or view space. Then in the fragment shader compute ddx(dFdx) and ddy(dFdy) of this variable. Take the cross product of those two vectors and normalize it - you got a flat normal! No mesh changes or per vertex data needed at all.

提交回复
热议问题