Terrain Splatmap Material


Job Open: Blitz3D/C++ DLL RPLidar Programming. View Job Posting
(Posted 1 year 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.

terrainsplat.frag.glsl

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" )
ShaderTexture(shader,grass_tex,"texture0",0)
ShaderTexture(shader,dirt_tex,"texture1",1)
ShaderTexture(shader,rock_tex,"texture2",2)
ShaderTexture(shader,splatmap,"splatmap",3)
SetFloat2(shader,"texscale",texwidth,texlength)
ShadeEntity(terrain, shader)
(Posted 1 year ago)

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 1 year ago)

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

Please log in to reply