我在弄清楚如何使我的fragmenthader正确加载3张图像时遇到问题。
目的是:加载三个纹理图像IMAGE1,IMAGE2和IMAGE3。 IMAGE3应该是黑白图像。使用IMAGE3作为过滤器。对于任何给定的像素P,如果IMAGE3中的对应纹理像素颜色为白色,则使用IMAGE1来纹理映射像素P。如果MAGE3中的对应像素颜色为黑色,则使用IMAGE2来纹理映射像素P。如果对应的像素颜色MAGE3中的像素不是黑色或白色,然后使用IMAGE1和IMAGE2的混合物来纹理贴图像素P。

现在,我可以使它与IMAGE1在IMAGE3的白色阴影区域中显示一起工作,但是我很难让IMAGE2在IMAGE3的黑色区域中显示而不与HEXAGON中的IMAGE1重叠。任何帮助,将不胜感激。

#version 330

in vec2 textureCoord;

uniform sampler2D textureMap0;
uniform sampler2D textureMap1;
uniform sampler2D textureMap2;

out vec4 fragColor;

void main() {

// retrieve color from each texture
    vec4 textureColor1 = texture2D(textureMap0, textureCoord);
    vec4 textureColor2 = texture2D(textureMap1, textureCoord);
    vec4 textureColor3 = texture2D(textureMap2, textureCoord);
    //vec4 finalColor = textureColor1 + textureColor2 + textureColor3;

// Combine the two texture colors
// Depending on the texture colors, you may multiply, add,
// or mix the two colors.
#if __VERSION__ >= 130
    if ((textureColor1.r == 0.0f) && (textureColor1.g == 0.0f)
        && (textureColor1.b == 0.0f)) {
        textureColor1.r = 1.0f;
    }

    //fragColor = mix(fragColor,finalColor,0.5);
    fragColor = textureColor1 * textureColor3 ;//* textureColor3;
#else
    gl_FragColor = textureColor1 * textureColor2;// * textureColor3;
#endif

}

最佳答案

我认为这应该符合您的描述:

vec4 textureColor4 = vec4(vec3(1.0, 1.0, 1.0) - textureColor3.rgb, 1.0)

//...

fragColor = textureColor3 * textureColor1 + textureColor 4 * textureColor2;

这本质上是线性插值。

10-08 18:45