How can I colour my terrain using a colour gradient based on the elevation?

Hi guys,

This question is rather simple, yet I haven’t been able to solve it.
I just want to colour my terrain based on its height using an rgb colour gradient. So higher places should be coloured red, lower ones green and even lower ones blue…
I blended these colours as seen in the image and I probably have to use the absolute world position and extract the Z- value. Yet i just dont know how to link the colour to the height or even between certain height values.

You’ll need Lerp!
Use the blue output of world position and divide for color region, using appropriate amounts of +offset for every following lerp, plug one lerp into another lerp into another.

Thanks for the reply! You mean i shall lerp each colour one by one with each other? And i plug the Blue output in a divide node which i will then plug into the alpha from each lerp?

yup! so off of the same world position you can setup your offsets–for 6 colors youd have 6 subtract nodes with different values to separate the colors in the spectrum, following those a divide node with a value for the length of the fades, then each of those plugging into the alpha lerp.

there’s possibly a more optimized way of setting it up, but that should work fine–Ive used this way to make sand wet along the waterline, and less brown as height goes up in a sand material.

Here’s a way to do it using a 1D texture lookup. You take the Z position, subtract the minimum height, divide it by the height range, clamp it in the range (0,1) and use that as UVs for your texture.

I don’t know if BreakOutFloat3Components is preferable versus using a ComponentMask here; both allow you to pick out just the Z channel, and both yield the same stats, but perhaps one is more performant.

1 Like

This works perfectly! thanks very much for sharing :smiley:

Can anyone elaborate on what @Simsaladoo said above in some more detail. Perhaps with a quick example. I’ve been trying to get this to work and have been struggling. I just end up with strange results that don’t look right when I follow the instructions in this thread. I’m not interested in the last post that uses the gradient. I want to try and use the solution with the lerps so I can control the individual color regions.