unity-triplanar-terrain-cliff-shader icon indicating copy to clipboard operation
unity-triplanar-terrain-cliff-shader copied to clipboard

drop-in replacement shader to add triplanar cliffs to Unity terrain (built-in 3D pipeline, 2021.2+)

unity-triplanar-terrain-cliff-shader

drop-in replacement shader to add triplanar cliff shading to the default Unity terrain system (no plugins, vanilla terrain system, built-in 3D pipeline)

image

description

  • this shader adds a single albedo + bump map as a general "cliff" texture, to tile along the X and Z planes
  • as an optimization, I didn't add Y plane sampling, so technically this is more biplanar than triplanar
  • as an optimization, I didn't add smoothness / metalness / occlusion maps
    • instead, I sample some approximate smoothness / occlusion from the cliff's albedo
    • I recommend against adding extra texture map slots, because you have to sample each texture map twice (once for X plane, once for Z plane) so it's a bit expensive

compatibility

  • for built-in 3D pipeline only (maybe you can modify it for HDRP / URP? but I'm not going to port it, sorry)
  • made for Unity 2021.2+ but probably mostly works for Unity 2019.3+ / 2020.x? but haven't tested it, sorry
    • probably doesn't work for pre-Unity 2019.3 because that's when Unity added terrain holes
  • breaks on instanced terrain though (idk why)... PRs welcome

usage / install

  1. put TerrainSplatmapTriplanar.cginc and Terrain-TriplanarStandard-FirstPass.shader somewhere in your Unity /Assets/ folder
  2. create a new Material that uses the /Nature/Terrain/StandardTriplanar shader
  3. in your Terrain object settings, assign the material from step 2
    • KNOWN ISSUE: you must disable Draw Instanced on the Terrain...
  4. configure cliff albedo + normal map in the material (not as a terrain layer)

image

image

implementation

This tries to replace as little as possible in the built-in Unity shaders. It's 2 files:

  • TerrainSplatmapTriplanar.cginc is just a copy of TerrainSplatmapCommon.cginc from Unity 2021.2.0f1 built-in shaders, except it adds worldPos and worldNormal to the Input shader struct. Unity's shader magic automatically populates IN.worldPos and IN.worldNormal variables if they're defined, so there's nothing else to do there.
  • Terrain-TriplanarStandard-FirstPass.shader is a modded version of Terrain-Standard-FirstPass.shader etc etc... I added various cliff texture properties, hooks into TerrainSplatmapTriplanar instead of default TerrainSplatmapCommon, and then applies triplanar mapping to the terrain mesh in the surf() function. You should probably tweak some of the hardcoded values.

When this inevitably breaks in future Unity versions (or if you need to add this to a terrain shader for an older Unity version) then you can easily patch this shader yourself by following those steps above, and copy and pasting the relevant sections. You'll need to know a little about shaders to do that. I recommend Catlike Coding's shader tutorials for a good intro to Unity shaders.

license

MIT

I'm happy if this helps you and I'm happy to share, but I'm not really going to maintain this repo or update it frequently

textures (not included) are from ambientCG.com