Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Get animated color while shader is changing it?

Discussion in 'Shaders' started by FuzzyOnion, Apr 7, 2019.

  1. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    Hi,

    I've been trying to find a way to have the same color across objects from a material with an animated shader. This shader takes a color input and cycles its hue.
    Using GetColor only gets the starting color and not the animated output color as the hue shifts.

    I haven't been able to find a solution and any help is greatly appreciated.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,242
    GetColor only gets the color value you originally set on the material. The work the shader does to animate the color only happens on the GPU, and the CPU / script side of things have no knowledge of those changes. You’ll have to replicate the same hue modifications the shader is doing in your script.

    The Color class has RGBtoHSV and HSVtoRGB functions which should make that easier. Assuming you’re using the _Time.y in the shader to animate the color, then Time.timeSinceLevelLoad, or use Shader.GetGlobalVector(“_Time”) to get the time the shader uses.
    https://docs.unity3d.com/ScriptReference/Color.html
     
    FuzzyOnion likes this.
  3. FuzzyOnion

    FuzzyOnion

    Joined:
    Aug 22, 2014
    Posts:
    31
    Thank you for the reply! I didn't expect syncing color to be this complex.
    Right now I have multiple materials/shaders doing the RGBtoHSV, then hue shift, then back to HSVtoRGB calculations which I imagine isn't great for performance. Sounds like in the future I should do the calculations once via a script and then pass the resulting color to the shader via a global parameter.
    It's also great to know that there are other ways of syncing shader happenings. Thanks again for the help!