在glsl es 2.0和Gamemaker Studio 2.0中,存在获取渐变正方形的问题。

5
我创建了一个由4个三角形组成的三角形列表,其中中心点有不同的颜色。然后我想将这些三角形组合起来,得到一个漂亮的渐变效果。 但是三角形的边缘会产生不需要的线条,我不想要这些线条,我希望它一直平滑下去。 我该如何获得所需的结果?
图片: Unwanted lines 着色器代码:
    // Simple passthrough vertex shader
    //
    attribute vec3 in_Position;                  // (x,y,z)
    attribute vec4 in_Colour;                    // (r,g,b,a)
    attribute vec2 in_TextureCoord;              // (u,v)

    varying vec2 v_texcoord;
    varying vec4 v_colour;

    void main()
    {
        vec4 object_space_pos = vec4( in_Position.x, in_Position.y,         in_Position.z, 1.0);
        gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;

        v_colour = in_Colour;
        v_texcoord = in_TextureCoord;
    }

    //
    // Simple passthrough fragment shader
    //
    varying vec2 v_texcoord;
    varying vec4 v_colour;

    void main()
    {
        gl_FragColor = v_colour;
    }

游戏制作代码: 创建事件:
    //Build vertices list


    vertex_format_begin();
    vertex_format_add_position();
    vertex_format_add_colour();
    vertex_format_add_textcoord();
    v_format = vertex_format_end();
    v_buff = vertex_create_buffer();
    vertex_begin(v_buff, v_format);

    //triangle 0
    vertex_position(v_buff, 200, 100);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 0.0, 0.0);

    vertex_position(v_buff, 600, 100);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 1.0, 0.0);

    vertex_position(v_buff,  400, 300);
    vertex_colour(v_buff, c_red, 1);
    vertex_texcoord(v_buff, 0.5, 0.5);

    //triangle 1
    vertex_position(v_buff, 200, 100);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 0.0, 0.0);

    vertex_position(v_buff, 200, 500);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 0.0, 1.0);

    vertex_position(v_buff,  400, 300);
    vertex_colour(v_buff, c_red, 1);
    vertex_texcoord(v_buff, 0.5, 0.5);

    //triangle 2
    vertex_position(v_buff, 600, 100);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 1.0, 0.0);

    vertex_position(v_buff, 600, 500);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 1.0, 1.0);

    vertex_position(v_buff,  400, 300);
    vertex_colour(v_buff, c_red, 1);
    vertex_texcoord(v_buff, 0.5, 0.5);

    //triangle 3
    vertex_position(v_buff, 200, 500);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 0.0, 1.0);

    vertex_position(v_buff, 600, 500);
    vertex_colour(v_buff, c_black, 1);
    vertex_texcoord(v_buff, 1.0, 1.0);

    vertex_position(v_buff,  400, 300);
    vertex_colour(v_buff, c_red, 1);
    vertex_texcoord(v_buff, 0.5, 0.5);

    vertex_end(v_buff);
    tex = sprite_get_texture(sprite_index, 0);

绘制事件:
    shader_set(shd_prim);
    shader_set_uniform_f(uni_radius, var_radius);
    vertex_submit(v_buff, pr_trianglelist, tex);
    shader_reset();
1个回答

5
您所看到的效果是光学幻觉。您可以通过分级颜色来使其可见。请使用以下片段着色器进行操作:
varying vec2 v_texcoord;
varying vec4 v_colour;

void main()
{
    float steps   = 4.0;
    //float steps   = 8.0;
    //float steps   = 16.0;
    //float steps   = 32.0;

    vec3 gradColor = floor(v_colour.rgb * steps) / steps;
    gl_FragColor   = vec4(gradColor, 1.0);
}

4种颜色:

enter image description here

8种颜色:

enter image description here

16种颜色:

enter image description here

32种颜色:

enter image description here


为了获得更好的效果,您需要在片段着色器中进行颜色计算。以下着色器平滑地改变了渐变,从视图中间的圆形渐变到视图边界的方形渐变。使用GLSL mix函数,片段颜色从color1插值到color2

varying vec2 v_texcoord;
varying vec4 v_colour;

void main()
{
    vec4 color1 = vec4(1.0, 0.0, 0.0, 1.0);
    vec4 color2 = vec4(0.0, 0.0, 0.0, 1.0);

    vec2 distV     = v_texcoord * 2.0 - 1.0;
    float maxDist  = max(abs(distV.x), abs(distV.y));
    float circular = length(distV);
    float square   = maxDist;

    gl_FragColor = mix(color1, color2, mix(circular,square,maxDist));
}

预览:

输入图像描述

这是一个关于IT技术的图片预览,点击链接可以查看更多详情。

谢谢您的回复。不幸的是,我正在尝试实现一个方形渐变,而不是一个圆形。 - Jaswir
我尝试过这个,但它仍然得到了那些行。一旦我有时间,我会尝试制作所需结果的图像,这可能会有所帮助。 - Jaswir
非常感谢,朋友。这正是我想要的!附注:你可能需要编辑这一行代码:vec2 distV = vertPos; -> vec2 distV = v_texcoord; - Jaswir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接