Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TransitionManager effect #118

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class_name ThreadUtility
extends Node

## Load next scene from path using a thread
## Load a resource from path using a thread
static func load_resource(path:String, receive_callback:Callable)->void:
# create thread function to load resource and shut down the tread
var _tread_load:Callable = func (path:String, callback:Callable, thread:Thread)->PackedScene:
Expand Down
26 changes: 26 additions & 0 deletions addons/top_down/resources/materials/transition.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
shader_type canvas_item;
// https://godotshaders.com/shader/pixel-melt/

// Animate from 0 to 1
uniform float progress: hint_range(0.0, 1.0) = 0.0;

// How jagged each band of melting pixels are
uniform float meltiness: hint_range(0.0, 16.0) = 1.0;

float psuedo_rand(float x) {
return mod(x * 2048103.0 + cos(x * 1912.0), 1.0);
}

void fragment() {
vec2 uv = UV;

// Move pixels near the top faster
uv.y -= progress / UV.y;

// Created jagged edges for each pixel on the x-axis
uv.y -= progress * meltiness * psuedo_rand(UV.x - mod(UV.x, TEXTURE_PIXEL_SIZE.x));

COLOR = texture(TEXTURE, uv);

COLOR.a = step(0.0, uv.y);
}
8 changes: 8 additions & 0 deletions addons/top_down/resources/materials/transition_material.tres
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://dfinp0iie47mi"]

[ext_resource type="Shader" path="res://addons/top_down/resources/materials/transition.gdshader" id="1_p1h5q"]

[resource]
shader = ExtResource("1_p1h5q")
shader_parameter/progress = 0.0
shader_parameter/meltiness = 1.0
56 changes: 56 additions & 0 deletions addons/top_down/scenes/autoloads/transition.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[gd_scene load_steps=6 format=3 uid="uid://bxj12cduwftxm"]

[ext_resource type="Script" path="res://addons/top_down/scripts/game/TransitionManager.gd" id="1_b8a2a"]
[ext_resource type="Material" uid="uid://dfinp0iie47mi" path="res://addons/top_down/resources/materials/transition_material.tres" id="2_ha7dd"]

[sub_resource type="Animation" id="Animation_g4wgy"]
resource_name = "transition"
length = 0.5
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("TextureRect:material:shader_parameter/progress")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.5),
"transitions": PackedFloat32Array(2, 1),
"update": 0,
"values": [0.0, 1.0]
}

[sub_resource type="Animation" id="Animation_y5efb"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("TextureRect:material:shader_parameter/progress")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.0]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_vk8ru"]
_data = {
"RESET": SubResource("Animation_y5efb"),
"transition": SubResource("Animation_g4wgy")
}

[node name="Transition" type="CanvasLayer" node_paths=PackedStringArray("texture_rect")]
layer = 5
script = ExtResource("1_b8a2a")
texture_rect = NodePath("TextureRect")

[node name="TextureRect" type="TextureRect" parent="."]
material = ExtResource("2_ha7dd")
offset_right = 40.0
offset_bottom = 40.0

[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_vk8ru")
}
4 changes: 3 additions & 1 deletion addons/top_down/scripts/game/PlayerSpawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ func on_player_scene_entry()->void:

func on_scene_transition()->void:
player_instance_resource.parent_reference_resource.node.remove_child(player_reference.node)

#get_tree().change_scene_to_file(scene_transition_resource.next_scene_path)
ThreadUtility.load_resource(scene_transition_resource.next_scene_path, get_tree().change_scene_to_packed)
#ThreadUtility.load_resource(scene_transition_resource.next_scene_path, get_tree().change_scene_to_packed)
Transition.change_scene(scene_transition_resource.next_scene_path)
42 changes: 42 additions & 0 deletions addons/top_down/scripts/game/TransitionManager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class_name TransitionManager
extends CanvasLayer

@export var texture_rect:TextureRect
@export var transition_time:float = 1.0

var game_resolution:Vector2i
var tween:Tween

func _ready()->void:
visible = false
game_resolution = get_viewport().content_scale_size

func change_scene(path:String)->void:
# wait for rendering everything on a screen
await RenderingServer.frame_post_draw

# get texture from the screen
var _image:Image = get_viewport().get_texture().get_image()
_image.resize(game_resolution.x, game_resolution.y, Image.INTERPOLATE_NEAREST)
var _image_texture:ImageTexture = ImageTexture.create_from_image(_image)
texture_rect.texture = _image_texture

get_tree().current_scene.visible = false
visible = true
transition_progress(0.0)

ThreadUtility.load_resource(path, scene_loaded)

## Set transition in motion
func scene_loaded(scene:PackedScene)->void:
assert(scene != null)
get_tree().change_scene_to_packed(scene)

if tween != null:
tween.kill()
tween = create_tween()
tween.tween_method(transition_progress, 0.0, 1.0, transition_time).set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_CUBIC)
tween.finished.connect(set_visible.bind(false))

func transition_progress(t:float)->void:
(texture_rect.material as ShaderMaterial).set_shader_parameter("progress", t)
4 changes: 3 additions & 1 deletion addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ func pressed()->void:
# Avoid triggering multiple times
# Deffered because this function is called by that signal
button.pressed.disconnect.call_deferred(pressed)

#get_tree().change_scene_to_file(scene_path)
ThreadUtility.load_resource(scene_path, get_tree().change_scene_to_packed)
#ThreadUtility.load_resource(scene_path, get_tree().change_scene_to_packed)
Transition.change_scene(scene_path)
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config/icon="res://icon.svg"
SoundManager="*res://addons/top_down/scenes/autoloads/sound_manager.tscn"
Music="*res://addons/top_down/scenes/autoloads/music.tscn"
LoadManager="*res://addons/great_games_library/nodes/LoadManager/LoadManager.gd"
Transition="*res://addons/top_down/scenes/autoloads/transition.tscn"

[display]

Expand Down