Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HDRenderpipline as Terrainshader

Discussion in 'Graphics Experimental Previews' started by Spy-Shifty, Jun 3, 2018.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Hi I know that isn't supported official.

    Here is a short trick on how to use HDRenderPipline/LayeredLit shader as Terrain shader.

    I was inspired by Megascans. Great thanks to them!


    Add the following script to:
    Assets/Editor
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public static class TerrainTools {
    5.  
    6.     [MenuItem("TerrainTools/Enable Splatmaps")]
    7.     private static void EnableSplatmaps() {
    8.  
    9.         Object activeObject = Selection.activeObject;
    10.         if(!(activeObject is TerrainData)) {
    11.             Debug.LogError("Selected Object isn't an TerrainData Asset");
    12.             return;
    13.         }
    14.  
    15.         Object[] data = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(activeObject));
    16.         foreach (Object o in data) {
    17.             if (o is Texture2D) {
    18.                 (o as Texture2D).hideFlags = HideFlags.None;
    19.                 Debug.Log(o);
    20.                 AssetDatabase.SaveAssets();
    21.                 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(o));
    22.             }
    23.         }
    24.     }
    25.  
    26.     [MenuItem("TerrainTools/Disable Basemap")]
    27.     private static void DisableBasemap() {
    28.  
    29.         Object activeObject = Selection.activeObject;
    30.         Terrain terrain = (activeObject as GameObject)?.GetComponent<Terrain>();
    31.         if (!terrain) {
    32.             Debug.LogError("Selected Object doesn't have an Terrain");
    33.             return;
    34.         }
    35.  
    36.         terrain.basemapDistance = 10000000;
    37.         Debug.Log(terrain.name + "  " + terrain.basemapDistance);
    38.     }
    39. }
    First create a material with a HDRenderPipline/LayeredLit shader
    Set Layercount to 4 and add the layer materials / textures

    Now create a terrain.
    Go to the Painting tab.
    Add 4 Textures
    upload_2018-6-3_22-41-32.png


    In the terrain, go to Terrainsettings and change the material to custom.
    Assign the created layer material to the custom material field
    upload_2018-6-3_22-39-45.png


    Now go to the TerrainData.asset (In the Terrain Collider component -> TerrainData)
    With the asset selected, go in the menu to TerrainTools -> Enable Splatmaps
    upload_2018-6-3_22-44-52.png

    Now you see an arrow at your TerrainData.asset expand it.
    This is the Splatmap
    upload_2018-6-3_22-46-23.png

    Go to your created material and assign the splatmap to the Layer Mask field.
    upload_2018-6-3_22-47-24.png

    If you want to disable the basemap (max dist 2000m -> pink...)
    Select the terrain again go to the TerrainTools menu and click on Disable Basemap!
    This will set the distance to 10,000,000

    Be aware!
    If you go to the TerrainSettings one more time the distance will be reseted to 2000 again!


    Thats it!
    Have fun!
     
  2. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,115
    Big thanks!
     
  3. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
    Thanks for sharing with us! That'll come in handy.
     
  4. Kolyasisan

    Kolyasisan

    Joined:
    Feb 2, 2015
    Posts:
    397
    Have some troubles setting it up, the shader fails to render all the layers besides the first one.
     
  5. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    How does look your settings?
     
  6. xrooshka

    xrooshka

    Joined:
    Mar 5, 2014
    Posts:
    59
    Yeah! It works! Some mess with the layers: RGBA format is used for the splatmap channels, but in material UI layers are stacked in ARGB. So the first texture is Layer 1 and Main Layer is the last terrain texture.
     
    Spy-Shifty likes this.
  7. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    The only thing is...
    You can't use more than 4 Layers
     
  8. Paddington_Bear

    Paddington_Bear

    Joined:
    Dec 9, 2012
    Posts:
    274
    I'm getting a null propagating operator error with the C#? Dusted off (and updated visual studio) but hasn't worked.


    Turns out my brain wasn't working, I needed to update VS to build to C# 6.0.. but this seems to have tuned up another issue. .NET compatibility is limited to 2.0 with 2018.2

    Anyone else have this issue?
    ___________________

    SOLVED: Turns out I need more sleep.

    If anyone else is using b9 you need to set the compiler to 4.x and do a restart.
     
    Last edited: Jun 22, 2018
  9. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Its work. Thank you!
    Any idea how add grass?
     
  10. xrooshka

    xrooshka

    Joined:
    Mar 5, 2014
    Posts:
    59
    I think we can add the grass like BotD team does: just place the grass meshes as a tree mesh :)
     
  11. Adam_Starcaster

    Adam_Starcaster

    Joined:
    Dec 7, 2012
    Posts:
    37
    Random question, I got this somewhat working but not sure about the proper settings for UV etc. Any recommendations how to prepare the 4 splats properly to follow? I am converting from micro splat.
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The up-coming terrain updates will be supplying a HDRP terrain shader FWIW (in case anyone missed anything).
     
    twobob, Thall33 and Spy-Shifty like this.
  13. LOTR2317

    LOTR2317

    Joined:
    Mar 11, 2018
    Posts:
    2
    if anyone still needs help fixing the null propagating operator error, all you need to do is take out the question mark before get component terrain on line 36
     
    MURZELLO likes this.
  14. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    By the way...

    I've seen this today.
    Thanks to the creator ;)

     
    Flurgle likes this.
  15. monah62rus

    monah62rus

    Joined:
    Feb 7, 2016
    Posts:
    3
    I try to do it according to your example in version 2018.3.8f, but unfortunately the terrain remains transparent, which can occur, there are no errors.!? Interested in the question, how is the mask for you?
     
  16. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    There are already built in terrain shaders for HDRP, hacks are not necessary any more.
     
  17. monah62rus

    monah62rus

    Joined:
    Feb 7, 2016
    Posts:
    3
    but there is no tessellation for the sake of what it is.
     
    racarate likes this.
  18. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Tessellation in HDRP is going to all be changed and overhauled, FYI. How or when I don't know but it's part of the plan since the current approach is apparently not optimal.
     
    monah62rus likes this.
  19. Deleted User

    Deleted User

    Guest

    Can you demonstrate how to use the built-in shaders, please?

    Edit: NVM, I figured it out. You have to go to the terrain settings, set the "material" dropdown to "custom", and select the DefaultHDTerrainMaterial as your custom material. The DefaultHDTerrainMaterial object is located at
    Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipelineResources/Material/DefaultHDTerrainMaterial.mat
     
    Last edited by a moderator: Mar 23, 2019
  20. superaldo666

    superaldo666

    Joined:
    Aug 30, 2013
    Posts:
    17
    Hi, this still can working for Unity 2019.3.4 or what alternative suggest?
     
  21. PIERRE999

    PIERRE999

    Joined:
    Jan 9, 2019
    Posts:
    2
    Yes it works for me in 2019.3 !