+1 vote

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?

in Engine by (1,869 points)

1 Answer

+1 vote
Best answer

As I can see in the doc for TileSet, there is a way to set a custom material on a specific tile: https://docs.godotengine.org/en/stable/classes/class_tileset.html#class-tileset-method-tile-set-material

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.

by (28,876 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.