0

我无法找到一种模式来相互绘制纹理。我需要使结果片段颜色如下:

tex1 + (1-tex1alpha)*tex2 + (1-tex1alpha-tex2alpha)*tex3

不是混合纹理,而是在图像编辑器中将一个放在其他类似图层上。

4

2 回答 2

0

我并没有真正使用 OpenGL,因此 GLSL 代码可能并不完全有效。但是您正在寻找的着色器应该看起来像这样。随时纠正任何错误。

vec4 col;
col = texture2D(tex3, uv)
if(col.a == 1) { gl_FragColor = col; return; }
col = texture2D(tex2, uv)
if(col.a == 1) { gl_FragColor = col; return; }
col = texture2D(tex1, uv)
if(col.a == 1) { gl_FragColor = col; return; }

也就是说,如果纹理的数量是固定的。如果您有可变数量的纹理,您可以将它们放入纹理数组并执行以下操作:

vec4 col;
for(int i = nTextures - 1; i >= 0; --i)
{
    col = texture2DArray(textures, vec3(uv, i));
    if(col.a == 1)
    {
        gl_FragColor = col;
        return;
    }
}
于 2013-01-31T21:18:44.300 回答
0

You can write the formula you posted directly in to the shader. The following will work:

uniform sampler2D sampler1;
uniform sampler2D sampler2;
uniform sampler2D sampler3;

varying vec2 texCoords;

void main()
{
    vec4 tex1 = texture(sampler1, texCoords);
    vec4 tex2 = texture(sampler2, texCoords);
    vec4 tex3 = texture(sampler3, texCoords);
    gl_FragColor = tex1 + (1 - tex1.a) * tex2 + (1 - tex1.a - tex2.a) * tex3;
};

However the 3rd argument could turn negative, which would have undesired results. Better would be:

gl_FragColor = tex1 + (1 - tex1.a) * tex2 + max(1 - tex1.a - tex2.a, 0) * tex3;
于 2013-01-31T21:50:33.073 回答