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

So I created this PBR terrain shader...

Discussion in 'Unity 5 Pre-order Beta' started by steego, Nov 22, 2014.

  1. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    ...and I decided to share it with you guys, 'cause you're awesome.

    So how do you use this thing? After importing the package, create a new material and select the Nature->Terrain->Terrain PBR shader, and assign the material to your terrain. It has two texture slots for a basemap that will be used on far terrain (a texture for the whole terrain) and two values; Basemap Distance is the distance before the basemap will be used and should be set to the same as in your terrain, Fade Length is the fade distance between splatmaps and the basemap.

    As for the textures, the alpha channel of the diffuse map should be your metalness value, and the alpha channel of your normalmap should be your smoothness (not rough). This goes for both the basemap in the material, and for any splatmaps you define in the terrain inspector.

    Because it uses the alpha channel of the normal map, the normal map textures should NOT be set as normalmaps in the import settings, but as a regular texture. This might cause some compression artifacts in the normals, but hopefully should still be acceptable. If you have the memory budget for it, you could set them to be uncompressed to eliminate this.

    If anyone knows how to instead take advantage of the unused channels in a DXT5nm in Unity, or has a better idea how to do this, please let me know.

    Enjoy!


    EDIT: Turns out I forgot to lerp the albedo to black for metallic textures, so here's a new version that does that.

    EDIT2: Whops, one more small mistake corrected.

    EDIT3: Reflections for metallic textures wasn't working, so I've uploaded a new version with the solution from this thread.
     

    Attached Files:

    Last edited: Nov 23, 2014
    Alverik, Eric2241 and mgear like this.
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    BTW if you try it out, please post screenshots, I haven't had any good art assets to test it with.
     
  3. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    RUBILYN and p87 like this.
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Oh that's really neat, I'll be sure to look into that, thanks. I've been thinking about parallax mapping as well, which would also require a heightmap. I've also thought about stealing another channel from the normalmap to use as an ao-map.
     
  5. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    Ooooh yeah. Since you should be able to reconstruct the Z component of the normal map like so (AFAIK):

    Code (csharp):
    1.  
    2. float3 normal = float3( tex2D( _NormalMap, IN.uv_NormalMap ).rg, 1 );
    3. normalize( normal );
    4.  
    So then you could discard the blue channel altogether and use it for something else.

    Then the channel layout goes something like:

    Albedo (RGB albedo, A height)
    Normal (RG normal, B ao, A roughness)
     
    RUBILYN likes this.
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Yeah something like that, this is how they do it for DXT5nm normal maps

    Code (csharp):
    1. inline fixed3 UnpackNormalDXT5nm (fixed4 packednormal)
    2. {
    3.    fixed3 normal;
    4.    normal.xy = packednormal.wy * 2 - 1;
    5.    normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
    6.    return normal;
    7. }
    But I think for both heightmap blending and parallax mapping, it'd have to be restricted to only 4 splat textures, I can't see how it could be done with an Add-Pass.
     
    RUBILYN likes this.
  7. RUBILYN

    RUBILYN

    Joined:
    Jun 22, 2013
    Posts:
    54
    HI ! Thank you deeply so much for Free sharing this AmazingNess !!! Its so much apreciated !

    Hey sory if its too much to ask but can you please include this special No tilling splat Technique :

    http://scrawkblog.com/2013/10/17/terrain-texturing-using-tile-mapping-in-unity/

    Project files are here : https://app.box.com/s/kckyjwl0yf899ayhkmml

    And can we use this SplatbasedShader in triplanar way also for Custom terrain objects ?
    Like terrain voxels imported from 3dcoat etc ...

    THANK YOU SO MUCH !!
     
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    I'll take a look, but no promises. And it won't be today in any case.

    Edit:

    Not as is I don't think, but feel free to re-write it to your needs.
     
    RUBILYN likes this.
  9. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    There seems to be a problem with specular reflections on terrain that I can't figure out, as seen in the screenshot below. To the left is the standard shader, to the right is a specular surface shader I've written for testing applied to a quad, and in the middle the same shader applied to a small terrain.

    Even if they share the same setup, the terrain never gets the orange tint visible on the quads.

    I've reported this as case 650373.

    Screenshot 2014-11-23 18.59.33.png Screenshot 2014-11-23 18.59.46.png Screenshot 2014-11-23 18.59.55.png Screenshot 2014-11-23 19.00.06.png
     

    Attached Files:

  10. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Still seeing the same as above in the new b14.
     
  11. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    Did you manage to fix the problem yet? We recently updated to Unity 5 for our WW1 shooter(http://www.verdungame.com/) and we use a lot of terrain for the trenches so your shader sounds great to accompany with the rest of the PBR assets. We will post some screenshots after a test. Let me know if you have an updated version ready!
     
  12. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Sorry, I haven't had time to look at this lately, but my bug is still open. The problem only seems to be with metallic surfaces though, so as long as your terrain textures aren't metallic (which most terrain textures aren't) I believe it should work as intended.
     
  13. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    Can't wait to play with this when I get home.
    Any chance of of supporting holes in terrain?

    Seems like people are rather keen for PBR terrain shaders that do 'more' :)
     
  14. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Sorry, no plans for that, but feel free to modify them and add this yourself.
     
  15. Axiomatic

    Axiomatic

    Joined:
    Feb 7, 2014
    Posts:
    30
    Thanks steego, very kind of you to share.
     
  16. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    No screenshots yet but I made some slight adjustments. Since our terrains(and most terrains) never have metallic values in them I modified the shader to have the smoothness map in the albedo alpha and nothing in the normal. With that extra normal alpha I made a second variation of the shader with a height map in the normal map for a parallax shader. Im thinking about also making that blending version but do not have much time. If anyone else feels like doing it I would love to see it!
     

    Attached Files:

    odival likes this.
  17. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Show screenshots when you can. I'd like to see this! :)
     
  18. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Does this work correctly with more than 4 splatmaps? I'm not sure, but I think the height might need to be scaled by the splatsum, so that the maximum height will not increase as more add-passes are used.

    The problem with this is that you can't know in the first pass, what the heighs of the add-passes are, and similarly in the add-pass, you don't know the height from the first pass.

    If you only need 4 splatmaps, you only need the first pass anyway so this is not a problem. Another solution would be to add the heightmaps in separate textures set in the material, this way you could use 8 or 12 heightmaps in 2-3 textures. I've also been pondering packing 4 splatmaps in a single splat texture.

    I'll probably look into this myself when I get the time, for me it's important to be able to use more than 4 splatmaps as this makes it much easier to break up repeating patterns by using several similar textures at different scales.
     
  19. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    pbr.gif
    Screenshot!

    The shader indeed does not work properly with 4+ textures but thats alright for my situation at the moment. Feel free to modify.
     
    zwcloud, Reanimate_L and steego like this.