Search Unity

Remapping RGB values, how can I remap more than 3 channels easily?

Discussion in 'Shaders' started by Botaurus, Dec 3, 2015.

  1. Botaurus

    Botaurus

    Joined:
    Feb 20, 2013
    Posts:
    81
    I wrote a shader so I can remap rgb values on a sprite. I can make a sprite that has red parts, blue parts, and green parts and then during runtime I can set those parts to whatever color I want. Can anyone think of a way that I can have more customization "parts" beyond just rgb. The only way I can think to do this with more than rgb is to just have multiple layered sprites, each with their own rgb parts. I'd like to find an easier to use solution though. If I recall correctly, its possible to use if statements in shaders but they are slow, so maybe thats not a good option? Any help would be greatly appreciated, thanks!


    heres the frag function Im using:

    Code (CSharp):
    1.                
    2. float ra = (c.r * (_R_opacity));
    3. float ga = (c.g * (_G_opacity));
    4. float ba = (c.b * (_B_opacity));
    5. float a = saturate(ra+ga+ba);
    6.  
    7. fixed4 c_remap = c.r*_r_remap;
    8. c_remap += c.g*_g_remap;
    9. c_remap += c.b*_b_remap;
    10. c = saturate(c_remap);
    11.  
    12. c.rgb *= a;
    13. c.a = a;
    14. return c;
    15.  
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    You could also work with combined colors, so at the 8 corners of the rgb cube.
    Code (csharp):
    1.  
    2. fixed4 remap_x00 = lerp(_black_remap, _red_remap, c.r);
    3. fixed4 remap_x01 = lerp(_blue_remap, _magenta_remap, c.r);
    4. fixed4 remap_x10 = lerp(_green_remap, _yellow_remap, c.r);
    5. fixed4 remap_x11 = lerp(_cyan_remap, _white_remap, c.r);
    6. fixed4 remap_xx0 = lerp(remap_x00, remap_x10, c.g);
    7. fixed4 remap_xx1 = lerp(remap_x01, remap_x11, c.g);
    8. fixed4 c_remap = lerp(remap_xx0, remap_xx1, c.b);
    9.