Interact with SpriteRenderer color in ShaderGraph

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.

6 Likes

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
6704722--791024--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+

3 Likes

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.

1 Like

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.

1 Like

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.

7 Likes

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.

@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.

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 .

1 Like