GLSL custom/no interpolation of triangles with vertex colors

后端 未结 1 575
孤街浪徒
孤街浪徒 2020-12-22 04:04

The effect I want to achieve is vertex color with sharp contours. So inside the triangle the fragment shader should use the color of whatever vertex is closest to that fragm

相关标签:
1条回答
  • 2020-12-22 04:52

    [replaced incorrect original]

    This should actually work...

    //vert
    out vec3 colour;
    ...
    
    //geom
    //simple passthrough but dupe colours to separate and include barycentric coord
    layout(triangles) in;
    layout(triangle_strip, max_vertices = 3) out;
    in vec3 colour[];
    flat out vec3 colours[3];
    out vec3 coord;
    ...
    for (int i = 0; i < 3; ++i)
        colours[i] = colour[i];
    for (int i = 0; i < 3; ++i)
    {
        coord = vec3(0.0);
        coord[i] = 1.0;
        gl_Position = gl_in[i].gl_Position;
        EmitVertex();
    }
    
    //frag
    flat in vec3 colours[3]; //triangle's 3 vertex colours
    in vec3 coord; //barycentric weights as a result of interp
    ...
    //get index of biggest coord
    int i = (coord.x > cood.y && coord.x > coord.z) ? 0 :
        ((coord.y > coord.z) ? 1 : 2);
    //choose that colour
    vec3 fragColour = colours[i];
    
    0 讨论(0)
提交回复
热议问题