Terrain Splatmap Material

Tweet blitzmax glsl shaders code-archives
(Posted 2 years ago) RonTek

Terrain material with basic splatmap, 3 textures.

Basically requires 3 textures (power of 2) with an rgb texture splatmap or mixmap like the one below. The shader also needs an unwrapped UV map (ie. flat projection) so a terrain (b3d) mesh was used in this example screenshot.


uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D splatmap;

uniform vec2 texscale;

void main()

    vec2 texCoord = gl_TexCoord[0].st;
    vec2 tiling = fract(texCoord / texscale);

    vec3 weights = texture2D(splatmap, texCoord).rgb;
    float sumWeights = weights.r + weights.g + weights.b;
    weights /= sumWeights;
    vec4 finalColor = weights.r * texture2D(texture0, tiling) 
                      + weights.g * texture2D(texture1, tiling) 
                      + weights.b * texture2D(texture2, tiling);

    gl_FragColor = finalColor;


Example Usage:

Local shader:TShader=LoadShader("","../glsl/default.vert.glsl", "../glsl/terrainsplat.frag.glsl")

Local texwidth# = 0.05,texlength# = 0.05

' Texture terrain using shader
Local grass_tex:TTexture=LoadTexture( "../terrain/grass_0.jpg" )
Local dirt_tex:TTexture=LoadTexture( "../terrain/dirt_1.jpg" )
Local rock_tex:TTexture=LoadTexture( "../terrain/stone_2.jpg" )
Local splatmap:TTexture=LoadTexture( "../media2/splatmap.png" )
ShadeEntity(terrain, shader)
(Posted 2 years ago) markcwm commented:

Well this looks great, thanks Ron!

I tried it on a terrain and got a black surface, so I realized terrains don't have vertices so no uvs, they're entities but not meshes. Edit: they do have vertices and uvs but it's internal. The funny thing is you can apply an EntityTexture and it works but I'm not sure how. I'd really like to fix terrains so they can take a shader material.

(Posted 2 years ago) RonTek commented:

Sure thing! and yes, I had the same realization with the terrain not having uvs or have no clue how to reference it or pull it out. I do see the ShaderMat on the terrain source, but still no luck. I will definitely help (and also still trying to figure this out) so openb3d can have a terrain material/shader method. cheers.

Reply To Topic (minimum 10 characters)

Please log in to reply