Gradient with fixed number of levels

后端 未结 2 1351
陌清茗
陌清茗 2021-01-19 08:26

I drawing a set of quads. For each quad I have a defined color in a vertex of it.

E.g. now my set of quads looks like:

2条回答
  •  面向向阳花
    2021-01-19 09:08

    Just expanding on Nico Schertler's comment: you can modify your fragment shader to:

    void main(void) {
        out_Color = vec4(pass_Color, 1.0);
        out_Color = floor(color * steps)/steps;
    }
    

    where steps in the number of color steps you want. The floor function will indeed work on a vector, however, the steps will be calculated separately for every color, so the result might not be exactly what you want (the steps might not be as nice as in your example).

    Alternatively, you can use some form of "toon shading" (see for example here). That means that you only pass a single number (think a color in grayscale) to your shader, then use your shader to select a color from a color table. The table can either be hardcoded in the shader or selected from a 1-dimensional texture.

提交回复
热议问题