Code Monkey home page Code Monkey logo

unity-triplanar-terrain-cliff-shader's Introduction

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

unity-triplanar-terrain-cliff-shader's People

Contributors

radiatoryang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unity-triplanar-terrain-cliff-shader's Issues

needs terrain base map shader

there's no triplanar rendered into the terrain base map shader currently

but maybe it should be?

no ETA on this because I don't need it myself, but PRs are welcome

Cliff texture is very metallic/shiny

THe cliff texture is very metallic, no matter the metallicness or smoothness of the texture applied, leading to odd spectral highlights on what is supposed to be (in my case) a rough texture

does this work with terrain add-pass shader

if your terrain has more than 4 terrain layers, unity renders additional terrain passes with another splatmap + add-pass shader

idk how this interacts with the firstpass shader though? is it ok?

no ETA on this because I don't need this, but PRs and testing / research are welcome

fix instanced terrain support

for some reason, triplanar stuff breaks when Draw Instanced is enabled on a terrain

ideally, it should work for instanced terrain too

no ETA on a fix because I don't need it myself, but PRs are welcome

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.