Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

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:
    6,334
    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.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    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.

    EDIT: FEATURE ADDED 2023.3+
     
    Last edited: Jan 26, 2024
    agate-pris, Dorodo and NotaNaN like this.
  3. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    44
    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.
     
    agate-pris likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    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:
    38
    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.
     
  6. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    Small update: I'm not sure what version I was running at the time, but I'm now running Unity 2021.3.16f1, which does no longer have this issue.
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    @Waarten do you mind specifying which version of URP you ran on this project?
    I'm currently having the same issue, and have tried multiple versions of both Unity & URP, to no avail.
     
  8. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    161
    In case someone lands on this thread looking for an answer to this question.
    We've added the ability to 'opt out' of final tint in both Canvas and Sprite Targets as of 2023.3 / Unity 6.

    More info here.
     
    Baste likes this.