Reflection shader for floor

Godot Version

4.3

Question

So I am making a 2D top down game and I want to make a glossy floor, meaning that the floor should reflect everything. I have made a shader that works, but only for the player. I have a player scene that has a camera2D following the player. Whenever the player moves the reflection is under him, but all of the objects that dont move have the reflection move with the player also. I want the reflection for the object that dont move to be always underneath them and never move. I have little to no experience with shaders and would really appreciate if somebody would help me out :folded_hands:

This is the shader:

shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE: hint_screen_texture, filter_linear_mipmap;

uniform float reflection_strength : hint_range(0.0, 1.0) = 0.35;

uniform vec3 reflection_tint : source_color = vec3(0.9, 0.9, 1.0);

uniform float floor_visibility : hint_range(0.0, 1.0) = 0.7;

void fragment() {
vec2 uv = SCREEN_UV;
uv.y = 1.1 - uv.y;

vec4 reflection = texture(SCREEN_TEXTURE, uv);
reflection.rgb *= reflection_tint * reflection_strength;

vec4 floor_color = COLOR;

// Make the reflection layer semi-transparent
COLOR.rgb = mix(reflection.rgb, floor_color.rgb, floor_visibility);
COLOR.a = reflection_strength; // Use strength as alpha

}

You can’t do this with a post processing shader. Why not just draw another tile layer with reflection tiles?

2 Likes

So I have to add each reflection manually? If I eventually add other tiles like tables, chairs and others I have to add a shadow for each of them?

You could automate the generation of reflection (or shadow) tile map. Iterate through tiles in the original map, determine which tiles are candidates for reflections and then set corresponding tiles in the reflection map.

1 Like

Okay ill try to do that. Thanks for the help! :grin:

1 Like