3D Blend textures based on terrain height.


I've working on a project where terrain is generated based on height, based from a heightmap, and I've followed this tutorial.
At the moment I'm using @Kentae's "Multi Texture Shader" to 'paint' the terrain based on a splatmap, and @DragoniteSpam's tutorials on how to make various 3D shader to add both fog and directional light to the scene.

Here's the result so far:

I still need to work on mip mapping, level of detail based on terrain steepness and much more, but as for now I wonder if anyone want to help me on how to blend textures based on the terrain vertex's height?


šŸ§ *penguin noises*
GMC Elder
The "height" is the world space z coordinate, which is available as one of the inputs (vertex format, to be precise - not uniforms) to the vertex shader. I'm thinking you'd add a varying for the height which you pass from the vertex shader to the fragment shader... just pass world-space z unchanged, (or perhaps normalize it to some range based on total world size), then apply some magic in the fragment shader.

Simplest magic would be to compute an atlas "subtexture index" based on height (ƍ'm thinking something along the lines of a list of ranges which corresponds to either a integer texture or a fractional lerping) and then the fragment shader either draws a single subtexture if the STI is integer, or mixes them together with linear interpolation if it has a fractional part (e.g. if it's 3 it uses texture 3, if it's 3.25 it uses 0.75*texture3 + 0.25*texture4.
(of course, you might prefer having separate variables for first texture, second texture and blending if you don't want to lock yourself into having a exactly 1 transition for every texture into every other).