Terrain Alpha Splatting         Basic terrain shader with up to 8 splat textures

Intro

This CG-powered alphasplat shader example is using the standard Ogre Terrain Scene Manager.

It takes two RGBA alphamaps and up to eight textures and splats them on the terrain in one pass, taking the global light position into account.

Result:
Terrain splatting shader

Prerequisites:

You need an alphamap or two, depending on how many textures you are using.
It's basically a black and white coverage map for each texture, put into a channel each (RGBA) in the finished alphamap texture.

Four textures per alphamap texture.
Some terrain exporters will do this for you. Otherwise follow this guide for the GIMP: Using Alpha-Splat Terrain Textures with PnP TerrainCreator - Create RGB Texture Maps.
If you have four textures, or less, you need to do a completely black texture (png) - as a blank no-splat texture.

Shader:

Terrain Alphasplat Shader

Put this in your media/materials/programs directory.

Program:

Terrain Alphasplat Program

Put this in your media/materials/programs directory.

Material:

Terrain Alphasplat Material

Put this in your media/materials/scripts directory.

Terrain Configuration File:

Terrain Alphasplat Terrain Configuration File

Put this in your executables working directory, as terrain.cfg.

Necessary Tweaks:

Before we can use the shader, we need to tweak the material script to fit our particular use.

Open the material script in your favorite text editor.

In the section entitled vertex_program_ref AlphaSplatTerrain/VP there is a list of parameters passed to the vertex shader.
We might want to tweak a few of them.

materialDiffuse and materialAmbient are RGBA values which controls how bright diffuse and ambient textures are going to be.


pageSize should match your pagesize. (doh).

textureTileSize controls how big the splat textures are scaled. Smaller values, smaller splats.

Next section is fragment_program_ref AlphaSplatTerrain/FP, which passes two parameters to the pixel shader.
alpha0Mask and alpha1Mask are values which determines which channels of your alphamaps are used.

Very important: Do set the channels you are not using to 0 (zero). If you have 6 textures, set all channels on alpha0Mask to 1 (one) and the two first channels on alpha1Mask to 1 (one). The rest should be zero. You get the picture (hopefully).

Now, navigate to the bottom, line 115, to the section named material DemoSplatTerrainShader : AlphaSplatTerrain.

Substitute the textures with whatever textures you're using.
If you're only using 6 textures, for instance, the names of the rest of the textures doesn't matter, as they're not used.


Enjoy! :-)

In Closing:

Fool around with diffuse and ambient parameters, and with light position. With it you can simulate changes in light.
Please post any improvements to this shader script.

This is a link to a tutorial on exporting terrain from PnP TerrainCreator and submitting it to this script:
Ogre3D export with splatting shader

Extras:

Give me back my Fog:

Add this vertex shader to your shader:

void ETFog_VS
(
    float4      position   : POSITION,
    float      delta      : BLENDWEIGHT,

    uniform   float4x4   worldViewProj,
    uniform float4      fogColour,
    uniform float4      fogParams,
    uniform float3      camPos,

    out      float4      oPosition   : POSITION,
    out      float4      oColor      : COLOR
)
{
    oPosition = mul(worldViewProj, position);

    float dist = length(position - camPos);
    float fogAmount = max((dist - fogParams.y) * fogParams.w, 0);
    oColor = float4(fogColour.rgb, fogAmount);
}


Then add a ref to your program definition file:

vertex_program ET/Programs/VSFog cg
{
   source AlphaSplatTerrain.cg
   entry_point ETFog_VS
   profiles vs_1_1 arbvp1
   
   default_params
   {
      param_named_auto worldViewProj worldviewproj_matrix
      param_named_auto fogColour fog_colour
      param_named_auto fogParams fog_params
      param_named_auto camPos camera_position_object_space
   }
}


Finally, add a pass to your material script, just after the splatting pass:

pass
        {
          scene_blend alpha_blend
          vertex_program_ref ET/Programs/VSFog
          {
          }
        }


That's it.
Now you have fog. Courtesy of Editable Terrain Library from which I nicked the fog shader.

Discussion topic:


This wiki page's support/discussion forum topic is here: Splatting Shader test
Post questions, issues, thoughts and ideas for improvement.


Alias: Terrain_Alpha_Splatting