How to use Tile color property in custom shader?

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.

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.

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

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.

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?

Vertex Color is a node.

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?

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.

8136815--1055993--dd - Copy.gif

Graph: 8136815--1055999--d - Copy.png

Thanks! We’ll check this out!

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.

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)?

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.

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.

1 Like

2020.3 and 2021.2