Is there a way to apply a shader to a specific tile of a tilemap?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By johnygames

I want to add a shader to a particular tile in a tileset and I want it to affect the specific tile only. So far I have tried adding shaders in the individual tileset slots, but the effect of those shaders seem to take the whole tileset into account and not just the tile that I have put the shader into.

Here’s a simple example:

Say I have four tiles in tileset. Now, I add the following shader to the last tile:

shader_type canvas_item;

void fragment(){
	vec2 uv_2 = (UV * cos(TIME))*0.5;
	COLOR = texture(TEXTURE, uv_2 );

This is supposed to animate the tile’s texture and create a swaying motion. But to my dismay I see that the whole texture is being animated and not just the tile I have selected. I’ve thought of exporting all tiles s individual textures and aign these into the sshaders, but that is a lot of work and it seems impractical. How do we make it so the shader samples one tile and considers only that tile’s UVs?

:bust_in_silhouette: Reply From: Zylann

As I can see in the doc for TileSet, there is a way to set a custom material on a specific tile: TileSet — Godot Engine (stable) documentation in English

However applying a shader material to a specific cell of the TileMap doesn’t seem to be possible. At least, not without complex workarounds. If that’s what you need, you could replace your tile with a temporary node on top of it with the effect.

Note that if you use an atlas, this needs to be taken care of within the shader, as UV will not be between 0 and 1, but only span a small section instead corresponding to the sub-region. I never did this before though, so I dont have a code example right now sorry.