Search Unity

[SOLVED] Gradient Rainbow Shader

Discussion in 'Shaders' started by olie304, Jan 3, 2017.

  1. olie304

    olie304

    Joined:
    Dec 18, 2016
    Posts:
    10
    I want to mimic the effect shown through a shader. I have been playing around with this but I don't understand shaders enough to fully replicate the effect. Could someone walk me through this? Thanks. Rainbow_Hatchback.gif
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    olie304 likes this.
  3. olie304

    olie304

    Joined:
    Dec 18, 2016
    Posts:
    10
    Alright that helped me understand how the I/O of shaders works for the component, but I still don't really understand how animated shaders like these work. Is there a way I can have the shader take a picture and repeatedly move it as above? I feel like if that were possible it'd be a good place to start.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    vain430 and olie304 like this.
  5. olie304

    olie304

    Joined:
    Dec 18, 2016
    Posts:
    10

    Attached Files:

  6. corbinyo

    corbinyo

    Joined:
    Aug 23, 2017
    Posts:
    26
    Anyone able to help change the shader to just use three colors of your choosing?
     
  7. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  8. corbinyo

    corbinyo

    Joined:
    Aug 23, 2017
    Posts:
    26
    @Olmi My Main issue is that i am really new to shaders and cant translate the movement part to this shader, I did see it and i did try to combine them! Do you have any ideas as to how I could use this shader (3 color) and make it move like the rainbow shader?
     
  9. starfoxy

    starfoxy

    Joined:
    Apr 24, 2016
    Posts:
    184
    Hi everyone. I am resurrecting this thread rather than creating a new one as it's the only one that got me going down the correct path.

    I am using @olie304 's shader that they attached here but am experiencing a couple of issues.

    First off, it seems to 'fill in the holes' on a transparent png sprite. Say for example, a donut would look like a solid circle instead of having a hole in the middle. Any ideas how I can fix that?

    Also, I would like to have a gradient color picker for the shader using Unity's gradient editor. Is this possible somehow?

    Sorry if these are silly questions but I am certainly not a shader expert and would be very grateful for some guidance. Thanks!
     
  10. starfoxy

    starfoxy

    Joined:
    Apr 24, 2016
    Posts:
    184
    I would really like to know how to move this as well. It appears to be a different method than shifting the hue value. Anyone know?
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The 3 color shader is lerping from the start to the mid color then the mid color to the end color over a 0.0 to 1.0 range, in this case sampled from the texcoords. If you want it to move, add
    _Time.y
    . However the current code means input values over >1.0 just end up being the end color. You could use
    frac()
    to loop between 0.0 and 1.0, but that means it'll just be snapping between the end color and the start color. So you'd need to modify the code to lerp between the first and mid color from 0.0 to 0.33, mid color to end color between 0.33 and 0.67, and end color to start color over 0.67 to 1.0.
    Code (csharp):
    1. fixed4 ThreeColorGradientLoop(fixed4 start, fixed4 mid, fixed4 end, float t)
    2. {
    3.     t = frac(t) * 3.0; // loop between 0.0 and 1.0 and rescale to a 0.0 to 3.0 range
    4.     fixed4 color = start;
    5.     color = lerp(color, mid, saturate(t));       // t 0.0 to 0.33
    6.     color = lerp(color, end, saturate(t + 1));   // t 0.33 to 0.67
    7.     color = lerp(color, start, saturate(t + 2)); // t 0.67 to 1.0
    8.     return color;
    9. }
    Well, the shader doesn't use a texture at all, so it doesn't matter what texture the sprite is using because the shader totally ignores it. I'll repeat my original suggestion to the op and say "try reading some tutorials". That should give you an idea of how to add a texture to a shader, sample it, and use its color and/or alpha value.

    There's kind of two ways to go about gradients in a shader.

    The easiest method is to make 1 pixel tall textures that are some arbitrary width that you paint using some external application and just use that. There are some Unity assets that put gradients in the material inspector that behind the scenes are generating those textures in c# for the user.

    The other way is something like the 3 color example linked to above. Pass in some number of colors and lerp between them. Unity's Shader Graph has a Gradient node that passes an array of colors and alphas with a "time" value per index. It then calculates the fractional value between each key and the previous key and lerps between them. This also requires custom c# to fill in that array from the C# gradient, but is a little less work than generating texture assets. It does make for a much more complicated shader, but it's fundamentally not too different than the example code I posted above.
     
  12. starfoxy

    starfoxy

    Joined:
    Apr 24, 2016
    Posts:
    184
    Thank you very much @bgolus ! What a fantastic and insightful response. I will dig further into this based on your advice and report back soon.
     
  13. NoHaxJustSoFlo

    NoHaxJustSoFlo

    Joined:
    Jul 10, 2020
    Posts:
    1
    Can you upload complete shader because I am not sure where should I put and call that function you mentioned?
     
  14. AsifNaeem

    AsifNaeem

    Joined:
    Apr 12, 2016
    Posts:
    29
    Hi! I want to use that same shader on 2d sprites. I try to configure, But it doesn't work.
    How that make possible?
     
  15. Graigy1337

    Graigy1337

    Joined:
    Oct 26, 2019
    Posts:
    9
    Adjusting the Hue it a much easier way to achieve this effect I feel.
    Simply Split your UV's into X or Y (depending on how you want the stripes to go) then multiply that by 360 (this number represents the full spectrum). Set an Initial Hue (I used x as 1) then offset it by the UV multiplier. Boom it splits the entire color spectrum over the texture.

    Then simply use tiling and offset to move the effect how ever you want. You can scale the lines as well.
    Here is my shader graph.

    Note: This is including the texture of the sprite it is attached to, if that sprite has colors other than White or Black the effect will look kinda weird. If you dont want this, just remove the sample texture color multiplier and the shader will overlay the colors over anything, but it wont take into account the shading of the texture.

    I also added a Color multiplier, this is simply to turn on HDR mode and make a glowing rainbow effect it you want.

    Anyway hope this helps someone.


    upload_2022-1-10_15-22-30.png
     
    VladushkaV and YousifRagab like this.