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

How to use Tile color property in custom shader?

Discussion in '2D' started by theGreatSlutze, May 12, 2022.

  1. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    I'm writing a custom shader to use with Tilemaps, and I'd like to incorporate the `Color` property on the tile asset itself. Unfortunately, I can't figure out how to reference it (at least using Shader Graph). I have a shader property named `Color`, with reference `_Color`, and I've tried marking it both exposed and not, without much success. When the property is exposed I can edit it in the material and see the effect I want, so I know the property I have is hooked up correctly in the shader. The problem seems to be that the tile object `Color` property isn't working, or at least, I can't tell what it actually does to the shader. Any suggestions?

    EDIT: To be clear, the tiles don't have color locked.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The Color property in the Tile and the Color property in the Tilemap will be combined together and set as the vertex color for each vertex in the Tile's Sprite.

    In the Sprites-Default material, the Tint Property (corresponding with _Color in the shader) will be multiplied with the vertex color in the Tile's Sprite to give the final color. This may differ depending on how your custom shader works.

    In the screenshot below, I have a white and a blue Tile setup in a Tilemap. The blue color is set using the Tile's Color property. The TilemapRenderer has a Material with the Tint color set to half alpha. This is applied onto the white and blue Tile.
    upload_2022-5-13_10-0-7.png

    If you are using ShaderGraph, you will need to add the vertex color as an input for your ShaderGraph. This will "allow" you to make use of the Tile.Color property as an input to your shader (in actuality, it is Tile.Color and Tilemap.Color combined.)
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    It would be nice if there were a checkbox in Shader Graph to turn this behavior off (the automatic multiplication after your SG runs). Sometimes it's very convenient to pass some data through vertex color instead of using it as a tint, and so it's a shame that doing so breaks Shader Graph sprite shaders.
     
  4. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Got it, thanks! I'm interpreting this to mean that I need to add a property of type color to my shader graph. Does that vertex color have a particular name/reference I need to use? I would assume `_VertexColor`? Does it need to be exposed/not?
     
  5. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Vertex Color is a node.
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    We will see what we can do about this! Would it be possible to share a sample Shader Graph that does this here so that we can check it out?
     
  7. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    As you can see in this simple example, let's say I have damage (black specks) that I want to use vertexColor.r to control. However, with a shader graph, I can't use the vertex color without complication: the tint results in a teal when I want no tinting at all. I can't disable this tinting from the graph. The workaround is to create a non-SG shader. Within SG, it would be nice to tick a checkbox to not apply the tint.
    dd - Copy.gif
    Graph:
    d - Copy.png
     
  8. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Thanks! We'll check this out!
     
  9. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Great! A similar request, not as easy/feasible. Something to the same effect would allow users to go further before needing to break out of nice SpriteRenderer/TilemapRenderer zone into fully custom.
     
  10. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    I am not well-versed in shaders or Shader Graph in Unity, but I assume that you would like
    SpriteRenderer.custom
    as an input for the Shader that does not come from a modified mesh of the Sprite or property in the MaterialPropertyBlock (breaks batching)?
     
  11. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Yes, per-instance custom data that doesn't break batches. One could it set with anything at runtime for wide array of effects. Right now, the options are Vertex Color, position.z (v limited) or breaking away from SpriteRenderers entirely.
     
  12. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    This is a bug and should be fixed in the future. Do let us know which Unity version you are using and we will see if the fix can be backported.

    I forwarded this request to the graphics team to see if this is feasible.
     
    Lo-renzo likes this.
  13. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    2020.3 and 2021.2