Dec
22
2014

Custom Terrain Shader: Sharp Blending

Modified terrain shader for sharp blending.

Tutorial:
– Download terrain shader sources for your unity version: http://unity3d.com/unity/download/archive
– Copy “DefaultResourcesExtra\Nature\Terrain\TerrBumpFirstPass.shader” into your project
– Rename the “TerrBumpFirstPass.shader” into “”TerrBumpFirstPassCustom.shader”
– Edit “TerrBumpFirstPassCustom.shader”
– Find line:

Shader "Nature/Terrain/Custom Bumped Specular" {

– Replace with: *So we get this shader under custom list

Shader "Custom/Nature/Terrain/Custom Bumped Specular" {

– Find line:

fixed4 col;

– Add these lines after it: *I’m sure there would be a better way to clamp those to use max channel only, but works for now 🙂

	if (splat_control.r>=splat_control.g && splat_control.r>=splat_control.b && splat_control.r>=splat_control.a)
    {
        splat_control.r=1;
        splat_control.g=0;
        splat_control.b=0;
        splat_control.a=0;
    }
 
    if (splat_control.g>=splat_control.r && splat_control.g>=splat_control.b && splat_control.g>=splat_control.a)
    {
        splat_control.r=0;
        splat_control.g=1;
        splat_control.b=0;
        splat_control.a=0;
    }
 
    if (splat_control.b>=splat_control.g && splat_control.b>=splat_control.r && splat_control.b>=splat_control.a)
    {
        splat_control.r=0;
        splat_control.g=0;
        splat_control.b=1;
        splat_control.a=0;
    }
 
    if (splat_control.a>=splat_control.g && splat_control.a>=splat_control.b && splat_control.a>=splat_control.r)
    {
        splat_control.g=0;
        splat_control.b=0;
        splat_control.r=0;
        splat_control.a=1;
    }

– Now create new material (Right click over project window, Create / Material

– Rename the new material as “CustomTerrainMaterial”
– Assign “Custom/Nature/Terrain/Custom Bumped Specular” to that material (from the material shader dropdown)
– Now assign material to the terrain: to Terrain object, Settings tab, Material slot

—————————————-

*Update for Unity5 (5.1.2)

– Download latest builtin shader (tested with 5.1.2) : http://unity3d.com/unity/download/archive
– Copy this file to your project: “\DefaultResourcesExtra\TerrainShaders\Splats\Standard-FirstPass.shader”
– Edit the copied “Standard-FirstPass.shader” and rename it to “Nature/Terrain/StandardSharp”
– Copy file “\512\CGIncludes\TerrainSplatmapCommon.cginc” to your project
– Modify file “TerrainSplatmapCommon.cginc”, find line “splat_control = tex2D(_Control, IN.tc_Control);”
– After that line, insert same custom code as above for unity4
– Create new material, assign shader Nature/Terrain/StandardSharp
– Assign this material to terrain (from terrain settings, material > custom, assign your material here)

Original terrain shader: (default blend)
terrain_blending_custom_unity_1
Custom terrain shader: (sharp blending)
terrain_blending_sharp_custom_unity_2


10 Comments + Add Comment

  • Hey dude, i’ve been trying to use this type of shader on UNITY 5 , but i can’t figure it out, it just dosnt work. can u update this shader into a Unity5 ready one?

    Thanksss!!

    • ^ updated post for unity5 differences

      • Can you update this tutorial to work with 5.6.0f3. I tried it and it didn’t work.

        • tested on 2007.1.0b5 still works.

          • It doesn’t work for me on my unity 5.6. there is still terrain blending. However it did not throw back any errors.

  • Hi !

    This post is very usefull for my project. But I can’t manage to use it on a terrain with more than 4 textures. The others turn very very white and shiny. Am I doing something wrong ?

    • yeah, would need to add support for more colors..

      i think it would happen at file “TerrainSplatmapCommon.cginc”, inside “void SplatmapMix(“..

  • What do you mean ? More splat_controls are needed ?

    I don’t really get what happen but i think the shader Standard Pass only deal with the first 4 textures, the others are dealt with AddPass shader which is referenced in the one downloaded from unity website at the end :

    Dependency “AddPassShader” = “Hidden/TerrainEngine/Splatmap/Standard-AddPass”
    Dependency “BaseMapShader” = “Hidden/TerrainEngine/Splatmap/Standard-Base”

    So it’s still the standard one.

    But we now kinda have two versions of TerrainSplatmapCommon (which can make the librairy bug).

    I’m not even sur it’s using the modifiyed oned for the pass shader (but it does for the firstpass and is still using the standard one if the first pass shader is standard).

    I tried to upload the others standard shader and renaming them but it didn’t work. Also tried to rename TerrainSplatmapCommon.cginc but didn’t managed to so the two differents versions still exists…

  • How would you do this on Unity 2019 4.41?

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.