Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

Interact with SpriteRenderer color in ShaderGraph

Discussion in '2D Experimental Preview' started by Baste, Jan 8, 2021.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,666
    If I'm writing a normal shader, I can get at a SpriteRenderer's .color by reading the the vertex color. I also have full control of the final, output color.

    When I'm creating a shader in ShaderGraph using the SpriteLit template, I can still read the .color through reading the vertex color. But I can't do anything about the color being automatically applied to the final output color.

    That's a problem! I need to create an effect that first applies the SpriteRenderer's color to the texture, and then applies the rest of the shader effect. That's trivial in a normal shader, but in the SpriteLit template, I'm unable to prevent the tint being applied in the end.

    Any way I can fix this now, that's not a huge hack? I've gotten some suggestions, but it seems a bit over the board.
     
    Dorodo, GliderGuy and Lo-renzo like this.
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    971
    This would be extremely useful. If there were Lit and Unlit master nodes which left it to the user to apply vertex color earlier if at all, I'd immediately switch to them in nearly all cases.

    edit: Could this be an option enabled in the 2020.2+ Master Stack and Stage Blocks feature? https://portal.productboard.com/8ufdwj59ehtmsvxenjumxo82/c/52-master-stack-and-stage-blocks
    ss.png

    Some checkbox [ ] for whether Vertex Color is auto-applied or whether it's left up to user to make that decision. By default on, but can be turned off.
     
    Last edited: Feb 7, 2021
    Dorodo and GliderGuy like this.
  3. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    43
    This so much. Could someone elaborate what's happening behind the scenes? I'm just confused as to how shadergraph seems to behave differently with sprite renderer colors. I don't even see this issue happening on UI Image components.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,666
    I asked someone on the Unity Discord, and they pointed me to some source code. Unity's shader graph to shader conversion code seems to have hard-coded "multiply the vertex color in after the shader graph".

    We ended up just copying the Sprite-Lit's code and making a few adjustments as necessary. Took a lot less work than the shader graph version did.
     
    agate-pris likes this.
  5. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    25
    Thanks for this thead Baste.

    I still find it weird that they forcibly apply the color _after_ your graph, because of two reasons:
    • The node "Fragment > Base Color" implies a final shader output
    • The vertex color (which the sprite renderer sets) is available for use _within_ shader graph, so that's where I would expect the operation to take place.
    For now I'll be 'pre-dividing' my output as a before-last step in shader graph, hoping that this will be an optional setting in the future.
     
unityunity