Issue with transparency while splatting texture on terrain

Which is sort of what I said… except he has to set it to some non-1.0 value based on the fact that turning on alpha blending indicates that he wants some alpha. He just needs to decide where his proper alpha source is and set it to that.

Maybe for this particular project he’d better write his own terrain shader ? It seems to me his ambitions require more than a few hacks on TerrainLighting.
Or maybe use a second sampler2D to control exactly where is the final alpha :

gl_FragColor.a = texture2D(m_Something,texCoord).whatever

But what about the effects on the borders ? I’d really like to understand.

What is it that you don’t understand?

@pspeed:
Pardon me for not understanding the obvious. I can be a stupid person :slight_smile:

What I don’t understand is why and how we are getting this gradient on the borders (between the two AlphaMap’s colors). Why/How is that gradient’s look changing with the AlphaMap resolution ?

Because texture filtering.

@pspeed : Ok I’ll have a little trip into these bi-tri-aniso things that I don’t know well enough. Thank you.

I’ll start with Wikipedia. If by chance you or someone else know a better source of information my ears are all open.

This gradient is caused because of how the shader works:

vec4 diffuseColor = texture2D(m_DiffuseMap, texCoord * m_DiffuseMap_0_scale);
    diffuseColor *= alphaBlend.r;
    #ifdef DIFFUSEMAP_1
        vec4 diffuseColor1 = texture2D(m_DiffuseMap_1, texCoord * m_DiffuseMap_1_scale);
        diffuseColor = mix( diffuseColor, diffuseColor1, alphaBlend.g );
    #endif

The alpha map shouldn’t be normalized. Each channel in the alpha map is simply layered on top of the previous one, for example the rgba values of 255/255/0/0 will result in only the green layer visible, so I would just use an alpha map like this:

this will result in a blending gradient looking like this:

Ok thank you guys !
This may help someone else too (the GLSL mix function) :

https://www.opengl.org/sdk/docs/man/html/mix.xhtml

I’m currently working at the modification of TerrainLighting.vert. The transparency of the final color will be replaced by the blend of the transparencies of all the diffuse maps (proportionally to the alpha map).

I think this may be the correct behavior but I see it in the scope of my specific need so I can’t tell.

Anyway, I’ll paste the result when i’ve figured it out, for the records.

Thanks for the help !

Everything is working now. The terrain material accepts transparent textures and uses their alpha correctly. No more issue ! Thanks to all of you for your help.

For the records, here is the modifications (lignes indicated by arrows) of the material definition, located in TerrainLighing.frag :

vec4 calculateDiffuseBlend(in vec2 texCoord) {
    vec4 alphaBlend   = texture2D( m_AlphaMap, texCoord.xy );

    #ifdef ALPHAMAP_1
        vec4 alphaBlend1   = texture2D( m_AlphaMap_1, texCoord.xy );
    #endif
    #ifdef ALPHAMAP_2
        vec4 alphaBlend2   = texture2D( m_AlphaMap_2, texCoord.xy );
    #endif

    vec4 diffuseColor0 = texture2D(m_DiffuseMap, texCoord * m_DiffuseMap_0_scale);
->  float transp = (1-diffuseColor0.a)*alphaBlend.r;
    vec4 diffuseColor = diffuseColor0*alphaBlend.r;

    #ifdef DIFFUSEMAP_1
        vec4 diffuseColor1 = texture2D(m_DiffuseMap_1, texCoord * m_DiffuseMap_1_scale);
->      transp += (1-diffuseColor1.a)*alphaBlend.g;
        diffuseColor = mix( diffuseColor, diffuseColor1, alphaBlend.g );

        #ifdef DIFFUSEMAP_2
            vec4 diffuseColor2 = texture2D(m_DiffuseMap_2, texCoord * m_DiffuseMap_2_scale);
->	        transp += (1-diffuseColor2.a)*alphaBlend.b;
  	        diffuseColor = mix( diffuseColor, diffuseColor2, alphaBlend.b );

            [...]

        #endif
   #endif
-> diffuseColor.a = 1-transp;
   return diffuseColor;
}
2 Likes