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

Alternative Background Transition Implementation #1952

Merged
merged 14 commits into from
Dec 15, 2023
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
Expand Up @@ -25,7 +25,3 @@ func _update_background(argument:String, time:float) -> void:
else:
image_node.texture = null
color_node.color = Color.from_string(argument, Color.TRANSPARENT)


func _should_do_background_update(argument:String) -> bool:
return false
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cl6g6ymkhjven"]

[ext_resource type="Script" path="res://addons/dialogic/Modules/Background/default_background.gd" id="1_nkdrp"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/Background/DefaultBackgroundScene/default_background.gd" id="1_nkdrp"]

[node name="DefaultBackground" type="Control"]
layout_mode = 3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_push_transitions.gd"

func _fade() -> void:
var shader := setup_push_shader()
shader.set_shader_parameter('final_offset', Vector2.DOWN)
tween_shader_progress().set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_push_transitions.gd"

func _fade() -> void:
var shader := setup_push_shader()
shader.set_shader_parameter('final_offset', Vector2.LEFT)
tween_shader_progress().set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_push_transitions.gd"

func _fade() -> void:
var shader := setup_push_shader()
shader.set_shader_parameter('final_offset', Vector2.RIGHT)
tween_shader_progress().set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_push_transitions.gd"

func _fade() -> void:
var shader := setup_push_shader()
shader.set_shader_parameter('final_offset', Vector2.UP)
tween_shader_progress().set_trans(Tween.TRANS_SINE).set_ease(Tween.EASE_IN_OUT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extends DialogicBackgroundTransition


func _fade() -> void:
var shader := set_shader()
shader.set_shader_parameter("wipe_texture", load(this_folder.path_join("simple_fade.tres")))

shader.set_shader_parameter("feather", 1)

shader.set_shader_parameter("previous_background", prev_texture)
shader.set_shader_parameter("next_background", next_texture)

tween_shader_progress()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="GradientTexture2D" load_steps=2 format=3 uid="uid://qak7mr560k0i"]

[sub_resource type="Gradient" id="Gradient_skd6w"]
offsets = PackedFloat32Array(1)
colors = PackedColorArray(0.423651, 0.423651, 0.423651, 1)

[resource]
gradient = SubResource("Gradient_skd6w")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_swipe_transitions.gd"

func _fade() -> void:
var shader := setup_swipe_shader()
var texture :GradientTexture2D = shader.get_shader_parameter('wipe_texture')
texture.fill_from = Vector2.DOWN
texture.fill_to = Vector2.RIGHT
tween_shader_progress()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_swipe_transitions.gd"

func _fade() -> void:
var shader := setup_swipe_shader()
var texture :GradientTexture2D = shader.get_shader_parameter('wipe_texture')

texture.fill_from = Vector2.ZERO
texture.fill_to = Vector2.RIGHT

tween_shader_progress()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends "res://addons/dialogic/Modules/Background/Transitions/simple_swipe_transitions.gd"

func _fade() -> void:
var shader := setup_swipe_shader()
var texture :GradientTexture2D = shader.get_shader_parameter('wipe_texture')
texture.fill_from = Vector2.RIGHT
texture.fill_to = Vector2.ZERO
tween_shader_progress()
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class_name DialogicBackgroundTransition
extends Node

## Helper
var this_folder : String = get_script().resource_path.get_base_dir()


## Set before _fade() is called, will be the root node of the previous bg scene.
var prev_scene: Node
## Set before _fade() is called, will be the viewport texture of the previous bg scene.
var prev_texture: ViewportTexture

## Set before _fade() is called, will be the root node of the upcoming bg scene.
var next_scene: Node
## Set before _fade() is called, will be the viewport texture of the upcoming bg scene.
var next_texture: ViewportTexture

## Set before _fade() is called, will be the requested time for the fade
var time: float

## Set before _fade() is called, will be the background holder (TextureRect)
var bg_holder: DialogicNode_BackgroundHolder


signal transition_finished


## To be overridden by transitions
func _fade() -> void:
pass


func set_shader(path_to_shader:String=DialogicUtil.get_module_path('Background').path_join("Transitions/default_transition_shader.gdshader")) -> ShaderMaterial:
if bg_holder:
if path_to_shader.is_empty():
bg_holder.material = null
bg_holder.color = Color.TRANSPARENT
return null
bg_holder.material = ShaderMaterial.new()
bg_holder.material.shader = load(path_to_shader)
return bg_holder.material
return null


func tween_shader_progress(progress_parameter:="progress") -> PropertyTweener:
if !bg_holder:
return

if !bg_holder.material is ShaderMaterial:
return

bg_holder.material.set_shader_parameter("progress", 0.0)
var tween := create_tween()
var tweener := tween.tween_property(bg_holder, "material:shader_parameter/progress", 1.0, time)
tween.tween_callback(emit_signal.bind('transition_finished'))
return tweener
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ uniform sampler2D previous_background : source_color, hint_default_transparent;
uniform sampler2D next_background : source_color, hint_default_transparent;

// The texture used to determine how far along the progress has to be for bending in the new background.
uniform sampler2D whipe_texture : source_color;
uniform sampler2D wipe_texture : source_color;
// The size of the trailing smear of the transition.
uniform float feather : hint_range(0.0, 1.0, 0.0001) = 0.1;
// Determines if the whipe texture should keep it's aspect ratio when scaled to the screen's size.
// Determines if the wipe texture should keep it's aspect ratio when scaled to the screen's size.
uniform bool keep_aspect_ratio = false;

void fragment() {
vec2 frag_coord = UV;
if(keep_aspect_ratio) {
vec2 ratio = (SCREEN_PIXEL_SIZE.x > SCREEN_PIXEL_SIZE.y) // determine how to scale
vec2 ratio = (SCREEN_PIXEL_SIZE.x > SCREEN_PIXEL_SIZE.y) // determine how to scale
? vec2(SCREEN_PIXEL_SIZE.y / SCREEN_PIXEL_SIZE.x, 1) // fit to width
: vec2(1, SCREEN_PIXEL_SIZE.x / SCREEN_PIXEL_SIZE.y); // fit to height
frag_coord *= ratio;

frag_coord *= ratio;
frag_coord += ((vec2(1,1) - ratio) / 2.0);
}

// get the blend factor between the previous and next background.
float alpha = (texture(whipe_texture, frag_coord).r) - progress;
float alpha = (texture(wipe_texture, frag_coord).r) - progress;
float blend_factor = 1. - smoothstep(0., feather, alpha + (feather * (1. -progress)));

vec4 old_frag = texture(previous_background, UV);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
shader_type canvas_item;

uniform vec2 final_offset = vec2(0,-1);
uniform float progress: hint_range(0.0, 1.0);
uniform sampler2D previous_background: source_color, hint_default_transparent;
uniform sampler2D next_background: source_color, hint_default_transparent;


void fragment() {
vec2 uv = UV + final_offset * progress*vec2(-1, -1);

if (uv.x < 1.0 && uv.x > 0.0 && uv.y < 1.0 && uv.y > 0.0){
COLOR = texture(previous_background, uv, 1);
} else {
COLOR = texture(next_background, uv-final_offset*vec2(-1,-1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extends DialogicBackgroundTransition

func setup_push_shader() -> ShaderMaterial:
var shader := set_shader(DialogicUtil.get_module_path('Background').path_join("Transitions/push_transition_shader.gdshader"))

shader.set_shader_parameter("previous_background", prev_texture)
shader.set_shader_parameter("next_background", next_texture)

return shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="GradientTexture2D" load_steps=2 format=3 uid="uid://cweb3y3xc4uw0"]

[sub_resource type="Gradient" id="Gradient_skd6w"]
colors = PackedColorArray(0, 0, 0, 1, 0.991164, 0.991164, 0.991164, 1)

[resource]
gradient = SubResource("Gradient_skd6w")
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extends DialogicBackgroundTransition

func setup_swipe_shader() -> ShaderMaterial:
var shader := set_shader()
shader.set_shader_parameter("wipe_texture", load(
DialogicUtil.get_module_path('Background').path_join("Transitions/simple_swipe_gradient.tres")
))

shader.set_shader_parameter("feather", 0.3)

shader.set_shader_parameter("previous_background", prev_texture)
shader.set_shader_parameter("next_background", next_texture)

return shader

This file was deleted.

32 changes: 25 additions & 7 deletions addons/dialogic/Modules/Background/event_background.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var scene: String = ""
var argument: String = ""
## The time the fade animation will take. Leave at 0 for instant change.
var fade: float = 0.0
## Name of the transition to use.
var transition: String = ""


################################################################################
Expand All @@ -29,7 +31,8 @@ func _execute() -> void:
var time_per_event: float = Dialogic.Input.auto_skip.time_per_event
final_fade_duration = min(fade, time_per_event)

dialogic.Backgrounds.update_background(scene, argument, final_fade_duration)
dialogic.Backgrounds.update_background(scene, argument, final_fade_duration, transition)

finish()


Expand All @@ -55,9 +58,11 @@ func get_shortcode() -> String:
func get_shortcode_parameters() -> Dictionary:
return {
#param_name : property_info
"scene" : {"property": "scene", "default": ""},
"arg" : {"property": "argument", "default": ""},
"fade" : {"property": "fade", "default": 0},
"scene" : {"property": "scene", "default": ""},
"arg" : {"property": "argument", "default": ""},
"fade" : {"property": "fade", "default": 0},
"transition" : {"property": "transition", "default": "",
"suggestions": get_transition_suggestions},
}


Expand All @@ -72,10 +77,23 @@ func build_event_editor():
'placeholder': "No background",
'editor_icon':["Image", "EditorIcons"]},
'scene == ""')
add_header_edit('argument', ValueType.SINGLELINE_TEXT, {'left_text':'Argument:'}, 'scene != ""')
add_body_edit("fade", ValueType.FLOAT, {'left_text':'Fade Time:'})
add_body_edit("scene", ValueType.FILE,
add_header_edit("scene", ValueType.FILE,
{'left_text' :'Scene:',
'file_filter':'*.tscn, *.scn; Scene Files',
'placeholder': "Default scene",
'editor_icon':["PackedScene", "EditorIcons"]})
add_body_edit('argument', ValueType.SINGLELINE_TEXT, {'left_text':'Argument:'}, 'scene != ""')
add_body_edit("transition", ValueType.COMPLEX_PICKER,
{'left_text':'Transition:',
'empty_text':'Simple Fade',
'suggestions_func':get_transition_suggestions,
'editor_icon':["PopupMenu", "EditorIcons"]})
add_body_edit("fade", ValueType.FLOAT, {'left_text':'Fade Time:'})


func get_transition_suggestions(filter:String="") -> Dictionary:
var transitions := DialogicResourceUtil.list_special_resources_of_type("BackgroundTransition")
var suggestions := {}
for i in transitions:
suggestions[DialogicUtil.pretty_name(i)] = {'value': DialogicUtil.pretty_name(i), 'editor_icon': ["PopupMenu", "EditorIcons"]}
return suggestions
5 changes: 4 additions & 1 deletion addons/dialogic/Modules/Background/index.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extends DialogicIndexer
func _get_events() -> Array:
return [this_folder.path_join('event_background.gd')]


func _get_subsystems() -> Array:
return [{'name':'Backgrounds', 'script':this_folder.path_join('subsystem_backgrounds.gd')}]


func _get_special_resources() -> Array[Dictionary]:
return list_special_resources("Transitions/Defaults", "BackgroundTransition", ".gd")
2 changes: 0 additions & 2 deletions addons/dialogic/Modules/Background/node_background_holder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ extends ColorRect

func _ready():
add_to_group('dialogic_background_holders')
if material == null:
material = load("res://addons/dialogic/Modules/Background/default_background_transition.tres")
Loading