I’ve been working on a modified TerrainLighting shader. The idea was that I could create a terrain splatting system with support for up to 3 textures per fragment (divided equally) and up to 255 textures total, by gridding all possible terrain textures.
It works by putting the texture index in the r, g or b value of the alpha map, then the fragment shader will locate the texture.
For some reason, it’s not working. Here’s the main bit of modified code in the frag shader
[java] vec4 calculateDiffuseBlend(in vec2 texCoord) {
vec4 alphaBlend = texture2D( m_AlphaMap, texCoord.xy );
float width = 1.0 / 53.0;
vec2 newTexCoord = vec2(mod(texCoord.x * m_DiffuseMap_0_scale, 128), mod(texCoord.y * m_DiffuseMap_0_scale, 128));// We have to manually wrap it.
vec2 tex1 = vec2((alphaBlend.r * 255) * width) + newTexCoord.xy;
vec2 tex2 = vec2((alphaBlend.g * 255) * width) + newTexCoord.xy;
vec2 tex3 = vec2((alphaBlend.b * 255) * width) + newTexCoord.xy;
vec4 diffuseColor = vec4(0, 0, 0, 0);
float div = 0.0;
if (alphaBlend.r != 0.0) {
div += 1.0;
}
if (alphaBlend.g != 0.0) {
div += 1.0;
}
if (alphaBlend.b != 0.0) {
div += 1.0;
}
float mixVal = 1.0 / div;
if (alphaBlend.r != 0.0) {
vec4 rColor = texture2D(m_DiffuseMap, tex1);
diffuseColor = mix(diffuseColor, rColor, mixVal);
}
if (alphaBlend.g != 0.0) {
vec4 gColor = texture2D(m_DiffuseMap, tex2);
diffuseColor = mix(diffuseColor, gColor, mixVal);
}
if (alphaBlend.b != 0.0) {
vec4 bColor = texture2D(m_DiffuseMap, tex3);
diffuseColor = mix(diffuseColor, bColor, mixVal);
}
return diffuseColor;
}[/java]
Any ideas why it’s not working?
Sorry for hideous code, bad naming and overall bad methods. I dislike GLSL immensely, and don’t use it very often.
I know that I could just replace it with a texture array, but this method would enable support of an infinite number of textures whilst utilizing only 2 textures in the GPU (falls within opengl 2 support)
Just to point out, there are exactly 53 textures in the example, and each one is exactly 128x128.
I believe World of Warcraft and RuneScape both use this method, as if you run in opengl mode and then debug the process, it has a single grid of textures in 1 texture.