Search Unity

Question How is a sprite color passed to the shader graph?

Discussion in 'Shader Graph' started by Cambesa, Jul 23, 2020.

  1. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    In a Sprite Renderer you can set the Color like the image below. How is this color passed to the shader in shader graph? When I compile and show shader code, I mainly see vertex colors but no general color. It's not an exposed property either. I can't find any color node in the shadergraph.
    upload_2020-7-23_15-42-16.png
    Im asking this because our shader seems multiply the color multiple times with the sprite color and lighting pass so it has a steeper falloff curve than when using Sprite-Lit-Default.
     
  2. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Okay so apparently it passes the color you set in the SpriteRenderer as vertex color
     
    yeercat likes this.
  3. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Update: When passing the SampleTexture directly to the output in shadergraph, the color is already applied to the texture. It seems to pass the color both to the vertex color and to the texture. Where in the process does it do that? How does it end up affecting the shadergraph if you can't use it directly?
     
  4. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    Bump!

    Using Unity 2020.1.17 ... doesn't seem to be a way to use the SpriteRenderer Color in a Shader Graph. As soon as I'm using a (PBR or Sprite Lit) graph the SpriteRenderer Color gets completely ignored.
     
    Cambesa likes this.
  5. Dorodo

    Dorodo

    Joined:
    Mar 8, 2015
    Posts:
    44
    Can we get an official response on this? I've been going nuts with the same issue and couldn't find a response or solution so far. Pinging @rustum and @Chris_Chu as I've seen some support from their part on the 2D Shadergraph threads.

    We can't use the 2D renderer shaders as we need them to be GPU instanced since we use a billboard effect on our sprites (the SRP batcher breaks the effect in some situations). We're using 2019.4 LTS.
     
    Thygrrr, JiriAtanasovsky and Cambesa like this.
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    The default sprite shaders apply the vertex color automatically after the Shader Graph portion of your fragment code runs, so the VertexColor node isn't very useful because you have to hackily counteract it being applied later. Since there's no easy way to create your own Master Nodes which do not have this behavior, this leaves users without much flexibility.

    Relevant request: https://forum.unity.com/threads/interact-with-spriterenderer-color-in-shadergraph.1035214/ It would be nice if it were possible to allow users full control of whether VertexColor gets auto-applied.

    EDIT: it's now possible to control if the sprite color applies in Unity 2023+
     
    Last edited: Apr 22, 2024 at 7:27 AM
    Cambesa likes this.
  7. Zarod

    Zarod

    Joined:
    Jan 20, 2016
    Posts:
    60
    Man it's a long since I've used the forums xD
    In case anyone is still having issues, you just have to a " Vertex Color" node instead of a "Color" one.

    It's pretty simple and i feel a bit silly for not figuring this out sooner!

    Unity gives you all the tools you need and you are just there double checking everything that isn't even related to the issue.

    This works with PBR Shadergraph with unity 2019.4 LTS.
     
    IggyZuk, Lou12331 and ExtraCat like this.
  8. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    What also happens on my side when using shadergraph: Before the game starts, the sprite renderer color, specifically the alpha is properly passed to the shader, but changing the sprite renderer alpha while the game is running, does not change the transparency anymore. As if the vertex color is taken once at the game start, and then it assumes or actually does not change when changing the sprite renderer color.
     
  9. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Might solve it for some cases but it's not just about the vertex color, it's mainly about the link between the color property in my first post and how this is used in shadergraph from which one thing we know is, it's used as vertex color. But there seems to be more going on than just that and strange things like my post before this one.
     
  10. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    That sounds odd.

    Is this one particular shadergraph or all of them? Are you deriving your SG from Sprite Lit or Sprite Unlit Graph? If you're using one of the other perhaps it doesn't play well with the SpriteRenderer.

    And this is a in-scene SpriteRenderer you're editing the color property of, not a prefab, correct?
     
  11. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Not sure where it is derived from, it uses both the color and vertex color nodes. I'm finding all such components. Get their sprite renderer and change the color property on the sprite renderer that is found. You do see the color change in the inspector at the location of my first screenshot but it has no effect on the sprite. Only if you change that color before the scene starts it has effect
     
  12. Double-V

    Double-V

    Joined:
    Aug 13, 2016
    Posts:
    21
    For me, the vertex color node doesn't even register the color. It only seems to work if I make the shader starting from the sprite lit/sprite unlit options, but I can't use these since they break billboarding and have no tickbox for GPU instancing that fixes that in the other ones.
     
    Cambesa likes this.
  13. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    damn, it would be really nice if someone of unity itself could take a look at this @rustum @Chris_Chu
     
    midoski likes this.
  14. midoski

    midoski

    Joined:
    Apr 26, 2015
    Posts:
    6
    I've run into this today as well. I'm trying to do a very simple effect in a generalized way with the SRP. With Sprite rendering in particular, however, I cannot seem override the final color blending behavior that the original mesh instance verts (via the sprite renderer's component) data plugs in, which is a big shame. I basically want to sample the alpha but completely override any color values, and this just isn't possible to do reliably with the builtin sprite material pathways in shader graph.

    edit: Of course, 8 minutes after posting this, I figured out how to set things up just right with the "Unlit" type of material instead of "Sprite Unlit". The trick was enabling alpha clipping in the graph settings
     
    Last edited: Jan 11, 2022
  15. ambre_m

    ambre_m

    Joined:
    Nov 26, 2021
    Posts:
    1
    Hi,
    Since I have the same problem I continued my research.

    1. I tried forcing Unity to use _VertexColor parameter into a custom Sprite Lit Shader. In the Console, the error "UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR" appeared.

    2. I googled the error and found this page : https://issuetracker.unity3d.com/is...r-compilation-error-when-using-gpu-instancing.

    3. Since the first 3 points were ok, I checked GPU Instancing (I discovered previously that Unity often use this function in the editor for optimisation). The material I created didn't have the "Enable GPU Instancing" and I tried to add it : https://forum.unity.com/threads/how...tancing-checkbox-appear-on-the-shader.505085/

    4. thanks to GPU Instancing Documentation : https://docs.unity3d.com/Manual/GPU...41.527173932.1642517175-1450343368.1634916303, I found the process to create shader that support GPU instancing : https://docs.unity3d.com/2021.2/Documentation/Manual/gpu-instancing-shader.html

    5. I reached the goal.. or not.. Since the creation of custom shaders that support GPU seems to be only supported by Built-in Render Pipeline (my full project works in URP, so not for me.. )

    I hope this quick research could help and if some things happen to not be exact, please feel free to correct me :)
     
    Last edited: Jan 21, 2022
  16. Alec-Slayden

    Alec-Slayden

    Joined:
    Dec 16, 2008
    Posts:
    101
    Would you mind sharing more details about your findings?
    Did you have to change the property of the color in shader graph?
    Are you saying that the color set in the SpriteRenderer component is passed successfully to Unlit shader graphs but not sprite unlit?
     
  17. amenonegames

    amenonegames

    Joined:
    Nov 10, 2021
    Posts:
    19
    Hi there!
    I find way to fix it conditionally this matter.
    This condition is can't use GPU instancing.

    Following shader can be reflected Sprite Renderer's Color.
    upload_2023-9-18_23-14-50.png

    If you use Vertex Color node, Sprite renderer color reflect.
    But you must set material Enable GPU Instancing Property to false.
    When we check property, Sprite Renderer's Color is ignored.

    __Disable GPU instancing__
    upload_2023-9-18_23-18-26.png

    __Enable GPU instncing__
    upload_2023-9-18_23-19-12.png

    I hope this information helps.

    my environment
    Unity 2022.3.4f1
    URP 14.0.8
    Shader Graph 14.0.8
    use 3D renderer
     

    Attached Files:

  18. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    168
    Hi,

    Thanks for sharing your insights @ame_y, @ambre_m.

    Currently, the Sprite Sub-Targets are multiplying the Base Color with Vertex Color at the very end and you can't opt out of that behavior. The same applies to recently added Canvas Sub-Targets.

    This is being addressed with an option to disable vertex color multiplication, so you can decide how you want to use vertex colors (Sprite or UI Element color) in your Shader Graphs.

    I'll keep you posted when this is available.
     
    passerbycmc likes this.
  19. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Is there a ticket to follow or a ETA and when this will be a thing, it really means i have to write HLSL for a lot of things since i commonly use vertex color for masking. especially in cases like using tile map
     
  20. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    168
    It should be in this week's 23.3 alpha. I'll post an update when it's there.
    Thanks for your feedback!
     
    passerbycmc likes this.
  21. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    168
    SudoCat, amenonegames and passerbycmc like this.
  22. SudoCat

    SudoCat

    Joined:
    Feb 19, 2013
    Posts:
    65
    Will this feature be available in any older versions for those of us stuck in LTS land?

    Currently I have to divide my output colour by vertex colour as the last step in my sprite shaders to neutralise the effect, which is a little tedious. Looking at your comment in the thread you linked, this won't actually work at runtime either, as apparently it uses unity_SpriteColor at runtime? Or is this only in 2023.

    Also, is there any difference in behaviour for TilemapRenderer?
     
    Last edited: Jan 26, 2024
    passerbycmc likes this.
  23. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    it happens on the tilemap renderer as well, its not a renderer thing its the lite sprite shader you use in shader graph doing it
     
    FredMoreau and SudoCat like this.
  24. FredMoreau

    FredMoreau

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    168
    Thanks @passerbycmc.

    We haven't planned a back port, yet, but I'm going to check what we can expect.

    By the way, any examples/visuals would help. Thanks!
     
    SudoCat likes this.
  25. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    Backport would be amazing :)
     
  26. SudoCat

    SudoCat

    Joined:
    Feb 19, 2013
    Posts:
    65
    In my use case, I apply the sprite colour using a mask texture, so that the colour is only applied to specific areas of the sprite, to allow for easy recolouring of tiles without additional materials.

    Dividing works fine enough, but does run the risk of a divide by zero creating NaN errors.