Shader gradient based on world coordinates

Hi All,

I’m looking to create a shader that would have a gradient based on the Heights that I set in the world. Rather than being set based on the surface that it’s applied to.

Example: Black would be set to the tallest point in my map and white to the lowest.

Any suggestions on how to go about this? thank you!

The shader code for this is quite similar to normalising linear depth to the near-far range.

return ( ( 1 / WorldPositionZ ) - MinHeight ) / (MaxHeight - MinHeight );

I would post a node graph but I’m not at home right now, hope this will be helpful. You could put that code into a custom node and set up the three inputs (WorldPositionZ, MinHeight, MaxHeight) and then hook the output up to whatever you want. The world position node may or may not use Z as up, I can’t remember if it’s Y-up or Z-up in UE4. The 1/WorldPositionZ may need changing to just WorldPositionZ also, but the code should do what you’re asking for.

Hope it helps.

there is world coordinate node, get it

get component mask, mark only blue, uncheck red and green

multiply that value by something like 0.0001 (you need to experiment here based on height of your meshes)

clamp that value to 0 … 1

use linear interpolate for 2 vector paramater nodes (those are your colors for zero and some max height) , feed clamped value to alpha in lerp node.

For testing time remove clamp and see if colors change etc.

That would also work but there’s way more guess work involved.

Thank you! I will try both methods in the next few days and report back.

I was on my tablet when I replied, but I’m at home and have made the shader, I implemented it as a post process as it would be much more flexible that way but you can do it as part of your regular materials and it’ll be the same. I prefer to defer effects like these as it’s much easier to implement global changes that way.