Search Unity

Best Practices for Terrain Shader

Discussion in 'Shaders' started by edwar496, Nov 20, 2017.

  1. edwar496

    edwar496

    Joined:
    Jun 28, 2016
    Posts:
    18
    Hey all, I'm working on a Terrain system/shader that will use vertex color to blend between multiple texture sets. My texture setup is as follows:
    Diffuse map
    Normals
    Stack map of Roughness, AO, and Height packed.

    All of these will be sampled twice using distance tiled UV's, making a grand total of 6 texture samples per material. However, I want to use vertex color to blend these, so that actually adds up to 6x3=18 texture samples, which is a lot. It does have the memory benefit of using only one material though, whereas I could break it down into just two texture sets in the blend areas and one texture set everywhere else, which would be 12-6 texture samples respectively. This, however, increases the number of materials the terrain uses for a performance gain.

    So, all you experienced graphics programmers - what is the best practice? Do I bite the bullet and have 18 (or even 24) texture samples in my master terrain shader? Or do I set up a system where different on-ground texture sets means different materials, inflating the memory somewhat? Is there a correct answer to this question or is it really based on the needs of the project?

    Thanks!
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
  3. edwar496

    edwar496

    Joined:
    Jun 28, 2016
    Posts:
    18
    Neat! I will check this out this evening to see if it’s combatible with my engine. Thanks!
     
  4. edwar496

    edwar496

    Joined:
    Jun 28, 2016
    Posts:
    18
    I am planning on putting my engine on the asset store though, and I'm not sure I would feel right if I used someone elses product in my package. So, can anyone answer my initial question? It seems Microsplat took the one-material, buttload of samples route.
     
  5. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    For the most part you cannot use other peoples products in your package. However, I have a UAS shippable version of MicroSplat that partners can use- which easily converts into the free version of MicroSplat when the user decides they want to edit the terrain material.

    Also, MicroSplat is extremely efficient in terms of sample counts- far more so than any other shader available. It can do a fully PBR terrain with 16 textures on it in as little as 4 samples per pixel for the terrain texturing in a single pass. For vertex based workflows, I also have MegaSplat, which can do 256 textures on a single mesh or terrain in either 6 or 12 samples per pixel in a single pass.
     
  6. edwar496

    edwar496

    Joined:
    Jun 28, 2016
    Posts:
    18

    How is it possible to do 4 samples per pixel if there are 16 textures? Wouldn't that mean 12 textures just don't get used? Or am I missing some fundamental shading concept here that can use multiple textures in a pixel without sampling? (I am very curious)
     
  7. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    The magic of Optimization!

    The trick is simple; you might have 16 textures (or 48 with my new Texture Cluster module) on your terrain, but you don’t have 16 on a single pixel, so most of the samples in the traditional approach aren’t needed and are thus wasted work. You take advantage of that domain specific knowledge. The trick is in figuring out how to do that without stalling the GPU with branches.

    MegaSplat, in contrast, uses its own splat format which doesn’t store a weight per texture, but rather the indices and weights between them for each control point. Changing the format means an entirely different toolset, but it only needs 1 control texture for 256 possible textures with up to 6 textures blended at any pixel.
     
  8. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    109
    Couldn't you do full pbr doing just 2 samples, via packing, or 3 if you are also using a height map in addition to your normal map.
     
  9. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    You can easily pack a full PBR workflow into 2 textures- the trick is keeping sample counts down when blending large numbers of textures on a single pixel.