Search Unity

Multiple Materials/Shaders

Discussion in 'Shaders' started by delinx32, Aug 23, 2013.

  1. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I'm finishing up some code that allows me to generate a terrain mesh and I'm about to start writing the shader to texture the mesh. I'm not all that familiar with unity's ShaderLab and I've been unable to find the details I need to decide what path to take. Anyway, I'm hoping someone can offer some advice before I start down several wrong paths.

    Basically, my map can have several environments, say 4 for example. I'd like to support more than 4 if possible. Each environment will have a ground tile texture, a cliff tile texture, and a water texture, plus I'd like to support bump mapping if possible.

    So, I see several ways of arranging things.

    1: have a texture atlas for each part of each environment. So, 4xground, 4xcliff, 4xwater, 4xbump. I'm pretty sure that regardless of shader model, that's too many textures so I'd have to combine them into a single texture for each environment. If I do that, then I'll probably have a lot more operations in the shader and maybe bump into the operation limit.

    2: have 2 big textures that I build in my program that combine all of the parts up front so I don't have to do it in the shader. With 4 environments it ends up being 2 textures of size (4^environmentCount)*textureTileResolution. That fixes the texture count limit, but I'll quickly run out of texture size if I want to do anything greater than a 32x32 tile.

    3: multiple shaders?

    I noticed that I can apply multiple materials in unity, each with their own shader. If it works the way I think it would work then it would probably be perfect. Can I make one shader for each environment? Would all of the shaders be executed and "layered" together, or would the last one win out?

    I used to write HLSL for XNA and I know that I could only ever have one shader with any model. Is it the same in Unity?
     
  2. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    How would you blend between them anyways? Would you set the blending factors per vertex or would you use a texture?
     
  3. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I want to layer them like a tile system, not blend them like a height map.
     
  4. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Hmm... you could use a separate material for each tile then. It would take five textures: It's own and the textures of its four neighboring tiles. You would then blend between them in your shader. This would break batching, but I don't think that would cause any performance problems.