2D Object Shadow changing according to Time (with height map)

Make 2D sprite shadow that moves over time

This shader is based on https://godotshaders.com/shader/2d-shadow-out-of-bounds/

 

To use this shader, you need height map like this Screenshot1

Drag and drop this image to ‘Shader Parameters/Height Map’

 

Shader Parameters

– Height Map : To make uv transformation according to y-diff 

– UV Add : To change Height map’s Sampling UV.

    – x doesn’t have any effect, but if you use another image, it works

    – y changes shadow’s bottom line (near land)

– UV Mul : To change Height map’s Sampling UV.

    – it could change a degree of the shadow.

– Modulate : Shadow’s Color

– X Buffer / Y Buffer : The area of the shadow.

    – changing now recommeded, unless you edit this shader code.

– Time : It works like changing the light’s position.

– N Dot : The number of dot in specific area.

 

thank you.

Shader code
shader_type canvas_item;
render_mode blend_mix;

uniform sampler2D height_Map : source_color; // Height Map ( Gradient Field )
uniform vec2 uv_Add = vec2(0.0, 0.0);
uniform float uv_Mul = 1.2;
uniform vec4 modulate : source_color;
uniform float x_buffer = 2.0;
uniform float y_buffer = 1.0;
uniform float time = 11.0;
uniform float N_dot = 32.0;
varying vec2 input;
varying float intensity; 

void vertex() {	
	VERTEX.x *= x_buffer; 
	VERTEX.y *= y_buffer;
}

void fragment() {
	input.x = 3.0 * cos((time - 18.0) * 3.14 / 12.0);
	input.y = 2.0 * (sin((time - 18.0) * 3.14 / 12.0) + 1.0);
	intensity = clamp(cos((time - 12.0) * 3.14 / 12.0), 0.0, 1.0);
	
	vec2 uv_Origin = UV * 2.0 - 1.0;	
	uv_Origin.x *= x_buffer;
	uv_Origin.x /= 1.0;
	uv_Origin.y *= y_buffer;
	uv_Origin.y /= 1.0;                  
	uv_Origin = (uv_Origin + 1.0) / 2.0; 
	//COLOR = texture(TEXTURE, uv_Origin); //FOR ORIGINAL IMAGE DEBUGGING

	vec2 uv_Trans = uv_Origin;
	uv_Trans.x += 0.15 * input.x;
	uv_Trans.y -= 0.1 * input.y;  
	
	float val = texture(height_Map, uv_Trans * uv_Mul + uv_Add).r;
	vec2 uv_Final = uv_Origin * val + uv_Trans * (1.0 - val);
	
	vec2 t = uv_Origin * texture(height_Map, uv_Origin).r + uv_Final * (1.0 - texture(height_Map, uv_Origin).r);
	uv_Final.x = floor(uv_Final.x * N_dot) / N_dot + 0.5 * TEXTURE_PIXEL_SIZE.x; 
	uv_Final.y = floor(uv_Final.y * N_dot) / N_dot + 0.5 * TEXTURE_PIXEL_SIZE.y;

	vec4 shadow = vec4(modulate.rgb, texture(TEXTURE, uv_Final).a) * intensity * val; 
	vec4 col = texture(TEXTURE, uv_Origin);
	
	COLOR = mix(shadow, col, col.a);
	//COLOR = texture(height_Map, uv_Trans * uv_Mul + uv_Add); //FOR HEIGHT MAP ( GRADIENT FIELD ) DEBUGGING
}
Live Preview
Tags
shader
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments