OpenGL:重叠2个纹理而没有白色融合

时间:2019-03-25 21:54:04

标签: opengl fragment-shader

我正在尝试为正在工作的2d艺术风格制作片段着色器。 但是要完成这项工作,我必须使纹理能够自我重叠多次。

我想要的结果和2个当前结果:

img

Edit1:在我的艺术风格中,我每平面有1个纹理。为了使每个平面都具有厚度的错觉,我需要用一个像素距离自我绘制2次。

插图:

img

如果绘制的是绿色平面,然后是蓝色平面,是的,我想用重叠的蓝色替换绿色区域。

我试图减去下一个纹理位置以使纹理混合无效,​​但结果还是在空白区域添加了负值。

```
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float shxx;
uniform float shyy;

void main()
{
    vec2 Coord1 = v_vTexcoord + vec2(0,0);
    vec2 Coord2 = v_vTexcoord + vec2(shxx,shyy);
    vec2 Coord3 = v_vTexcoord + vec2(shxx+shxx,shyy+shyy);
    vec2 Coord4 = v_vTexcoord + vec2(+shxx+shxx+shxx,+shyy+shyy+shyy);

    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, Coord1)
        - v_vColour * texture2D( gm_BaseTexture, Coord2) 
        + v_vColour * texture2D( gm_BaseTexture, Coord2)
        - v_vColour * texture2D( gm_BaseTexture, Coord3)  
        + v_vColour * texture2D( gm_BaseTexture, Coord3)
        - v_vColour * texture2D( gm_BaseTexture, Coord4) 
        + v_vColour * texture2D( gm_BaseTexture, Coord4);
}
```

1 个答案:

答案 0 :(得分:0)

找到了解决方案。每次减去下一个纹理位置时,我都会将所有alpha值小于0.9的片段设置为默认颜色黑色。这样,下一个纹理将在没有现有颜色的基础上绘制。从而避免纹理融合为白色。

最终结果= https://i.imgur.com/aexqIts.png

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float shxx;
uniform float shyy;


void main()
{
    vec2 Coord1 = v_vTexcoord + vec2(0,0);
    vec2 Coord2 = v_vTexcoord + vec2(shxx,shyy);
    vec2 Coord3 = v_vTexcoord + vec2(shxx+shxx,shyy+shyy);
    vec2 Coord4 = v_vTexcoord + vec2(+shxx+shxx+shxx,+shyy+shyy+shyy);
    vec2 Coord5 = v_vTexcoord + vec2(+shxx+shxx+shxx+shxx,+shyy+shyy+shyy+shyy);    

    gl_FragColor =  v_vColour * texture2D( gm_BaseTexture, Coord1)
    -  texture2D( gm_BaseTexture, Coord2)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord2)
    -  texture2D( gm_BaseTexture, Coord3)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord3)
    -  texture2D( gm_BaseTexture, Coord4)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord4)
    -  texture2D( gm_BaseTexture, Coord5)*0.9;
    if( gl_FragColor.a < 0.9 ) gl_FragColor = vec4(0.0,0.0,0.0,0.0);

    gl_FragColor += v_vColour * texture2D( gm_BaseTexture, Coord5);
}