Search Unity

Multi Texture Shader based on color

Discussion in 'Shaders' started by Vanblam, Aug 24, 2015.

  1. Vanblam

    Vanblam

    Joined:
    Jul 19, 2013
    Posts:
    10
    I am very new to shaders and coding period . So I hope I explain this with enough detail. What I'm looking to do is create a shader that has specific colors set to equal a texture. for example: magenta = (selected texture), pink = (selected texture), yellow = (selected texture) and so on and so on ( I would want to display the RGBA values instead of a color name or maybe both). I just want to be able to go into the shader and add another RGBA code for another texture ( with normals and spec maps). That way the one material can contain like 200 textures based on RGBA codes. Is this possible? If it is would someone be willing to show me a way to start this shader?
     
  2. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    There's quite a few complications with this;
    • Colors are built as floating point, [0,1] range values. RGBA is a Vector4 of ([0,1],[0,1],[0,1],[0,1]) format. This means that setting specific colors is rather awkward, as you don't have integer comparisons, floating point equality checks are notoriously inaccurate.
    • Because equality is inaccurate, you have to check a range. There is no such thing as a switch statement, so you'd have to iterate across every option until it returns true (hundreds of operations in a shader, per pixel... which means hundreds of millions of operations).
    • Because if statements are slow, and dynamic branching per pixel is extremely slow, this would involve hundreds of if-statements per pixel, which means it'll actually, most likely, be reading from dozens, if not hundreds, of textures per pixels (shaders are processed in groups of pixels, where the groups of pixels are processed with the same code - if the code differs, it will often run both sides of if-statements, unless you explicitly mark as dynamic branches, which incurs an additional performance cost on it's own).
    So, no, this would not be possible in any feasible performance scenario. Can you describe a use-case? Maybe we can help you find an alternative way to design this. If you simply did not care about performance (a research scenario of rendering less than a dozen objects, on a desktop class GPU) you could take floats and multiply by 255 and cast to integers, and use them as indices, and set up a long chain of if-else statements. Otherwise, you would be best off explaining what you want to use it for, and there might be alternatives.

    There is vertex-blended shaders, where it uses vertex colors to blend between 4 textures, each channel representing the percentage of a texture. Those are more suitable because it accesses 4 textures, with no branching (it's adding the colors together by percentage).
     
  3. Vanblam

    Vanblam

    Joined:
    Jul 19, 2013
    Posts:
    10
    Thank you very much Plutoman for your input ( sorry for the late replay ). We are working with vertex blended shaders ( Ultimate Terrains / RTP3 ) . The problem that were running into is blending between two different vertex-blended materials (using the same shader of course). So I was hoping there might be way to add more textures to 1 material so they could all blend nicely together like with splat maps. Were planning on having multiple biomes in our procedural generated world, as you can imagine we will be using more then 4 textures :p .