-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hit flash effect for enemies using shader
utilized shader for flash effect with a uniform variable `lerp_percent`, being controlled from component scene to lerp the value for animation
- Loading branch information
1 parent
54c3143
commit 7dd79a1
Showing
6 changed files
with
122 additions
and
41 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
2d-survivors-course/scenes/components/hit_flash_component.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extends Node | ||
|
||
@export var health_component: HealthComponent | ||
@export var sprite: Sprite2D | ||
@export var hit_flash_material : ShaderMaterial | ||
|
||
var hit_flash_tween: Tween | ||
|
||
func _ready(): | ||
health_component.health_changed.connect(on_health_changed) | ||
sprite.material = hit_flash_material | ||
|
||
|
||
func on_health_changed(): | ||
if hit_flash_tween !=null and hit_flash_tween.is_valid(): | ||
hit_flash_tween.kill() | ||
|
||
(sprite.material as ShaderMaterial).set_shader_parameter("lerp_percent", 1.0) | ||
|
||
hit_flash_tween = create_tween() | ||
hit_flash_tween.tween_property(sprite.material, "shader_parameter/lerp_percent", 0.0, .25)\ | ||
.set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_CUBIC) |
19 changes: 19 additions & 0 deletions
19
2d-survivors-course/scenes/components/hit_flash_component.gdshader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
shader_type canvas_item; | ||
|
||
//void vertex() { | ||
//// Called for every vertex the material is visible on. | ||
//} | ||
|
||
uniform float lerp_percent: hint_range(0.0, 1.0, 0.1); | ||
|
||
void fragment() { | ||
// Called for every pixel the material is visible on. | ||
vec4 texture_color = texture(TEXTURE, UV); | ||
vec4 final_color = mix(texture_color, vec4(1.0, 1.0, 1.0, texture_color.a), lerp_percent); | ||
COLOR = final_color; | ||
} | ||
|
||
//void light() { | ||
// Called for every pixel for every light affecting the CanvasItem. | ||
// Uncomment to replace the default light processing function with this one. | ||
//} |
8 changes: 8 additions & 0 deletions
8
2d-survivors-course/scenes/components/hit_flash_component.tscn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[gd_scene load_steps=3 format=3 uid="uid://y7fu5m6xj717"] | ||
|
||
[ext_resource type="Script" path="res://scenes/components/hit_flash_component.gd" id="1_p1022"] | ||
[ext_resource type="Material" uid="uid://cp2qrgnhwj747" path="res://scenes/components/hit_flash_component_material.tres" id="2_0ccf8"] | ||
|
||
[node name="HitFlashComponent" type="Node"] | ||
script = ExtResource("1_p1022") | ||
hit_flash_material = ExtResource("2_0ccf8") |
8 changes: 8 additions & 0 deletions
8
2d-survivors-course/scenes/components/hit_flash_component_material.tres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://cp2qrgnhwj747"] | ||
|
||
[ext_resource type="Shader" path="res://scenes/components/hit_flash_component.gdshader" id="1_wsdh0"] | ||
|
||
[resource] | ||
resource_local_to_scene = true | ||
shader = ExtResource("1_wsdh0") | ||
shader_parameter/lerp_percent = 0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters