Fixes the drop shadow shader to not rotate with the sprite

master
TechieDamien 2025-08-02 19:32:49 +01:00
parent 3a19474966
commit 2f1950eaf3
Signed by: TechieDamien
GPG Key ID: 2ACE3574E164B780
2 changed files with 14 additions and 8 deletions

View File

@ -7,6 +7,6 @@ var last_rotation_mult = 1
func _process(delta: float) -> void: func _process(delta: float) -> void:
var head = get_tree().get_first_node_in_group("Head") var head = get_tree().get_first_node_in_group("Head")
if head is SnakePart: if head is SnakePart:
modulate = head.COLOURS[[head.colour_index % len(head.COLOURS)]] modulate = head.COLOURS[head.colour_index % len(head.COLOURS)]
last_rotation_mult = head.colour_index + 1 last_rotation_mult = head.colour_index + 1
rotate(delta * ROTATION_SPEED * last_rotation_mult) rotate(delta * ROTATION_SPEED * last_rotation_mult)

View File

@ -2,13 +2,18 @@ shader_type canvas_item;
uniform vec4 drop_shadow_color : source_color = vec4(vec3(0), float(0.5)); uniform vec4 drop_shadow_color : source_color = vec4(vec3(0), float(0.5));
uniform vec2 shadow_offset = vec2(float(0), float(0.1)); uniform vec2 shadow_offset = vec2(float(0), float(0.1));
varying vec2 actual_shadow_offset;
varying vec4 modulate; varying vec4 modulate;
void vertex() { void vertex() {
// Calculate the sprite's rotation angle from the MODEL_MATRIX (rotation matrix).
float sprite_rotation = atan(MODEL_MATRIX[0][1], MODEL_MATRIX[0][0]);
actual_shadow_offset = mat2(vec2(cos(sprite_rotation), -sin(sprite_rotation)), vec2(sin(sprite_rotation), cos(sprite_rotation))) * shadow_offset;
modulate = COLOR; modulate = COLOR;
float max_offset = abs(shadow_offset.x); float max_offset = abs(actual_shadow_offset.x);
if (abs(shadow_offset.y) > abs(shadow_offset.x)) { if (abs(actual_shadow_offset.y) > abs(actual_shadow_offset.x)) {
max_offset = abs(shadow_offset.y); max_offset = abs(actual_shadow_offset.y);
} }
VERTEX *= float(1) + float(2) * max_offset; VERTEX *= float(1) + float(2) * max_offset;
} }
@ -23,16 +28,17 @@ vec4 mixcolor(vec4 colA, vec4 colB) {
return vec4((colA.rgb + colB.a * (colB.rgb - colA.rgb)), colA.a + colB.a); return vec4((colA.rgb + colB.a * (colB.rgb - colA.rgb)), colA.a + colB.a);
} }
void fragment() { void fragment() {
float max_offset = abs(shadow_offset.x); float max_offset = abs(actual_shadow_offset.x);
if (abs(shadow_offset.y) > abs(shadow_offset.x)) { if (abs(actual_shadow_offset.y) > abs(actual_shadow_offset.x)) {
max_offset = abs(shadow_offset.y); max_offset = abs(actual_shadow_offset.y);
} }
vec2 uv = UV * float(float(1) + float(2) * max_offset) - vec2(max_offset); vec2 uv = UV * float(float(1) + float(2) * max_offset) - vec2(max_offset);
vec4 original_color = sample_texture(TEXTURE, uv) * modulate; vec4 original_color = sample_texture(TEXTURE, uv) * modulate;
vec4 shadow_color = vec4(drop_shadow_color.rgb, sample_texture(TEXTURE, uv - shadow_offset).a * drop_shadow_color.a); vec4 shadow_color = vec4(drop_shadow_color.rgb, sample_texture(TEXTURE, uv - actual_shadow_offset).a * drop_shadow_color.a);
if (shadow_color.a > float(0)) { if (shadow_color.a > float(0)) {
COLOR = mixcolor(shadow_color, original_color); COLOR = mixcolor(shadow_color, original_color);
} else { } else {
COLOR = original_color; COLOR = original_color;
} }
//COLOR = vec4(vec3(sprite_rotation), 1);
} }