From ec57e5173d9220f574303377cf21d710f853a0ac Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Mon, 6 Nov 2023 22:51:21 +0200 Subject: [PATCH] Make the canvas preview use the same material as the main canvas except when its animation is being played This is both an optimization and it restores live changes of the canvas preview when the user is drawing, without any extra performance cost, since the material is the same. The only case when the canvas preview is using a different material is when its animation preview is being played, so it can show a different frame than the main canvas. --- src/UI/Canvas/CanvasPreview.gd | 22 +++++++++++++++++++--- src/UI/Canvas/CanvasPreview.tscn | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/UI/Canvas/CanvasPreview.gd b/src/UI/Canvas/CanvasPreview.gd index 476f0470d..ee33d5df7 100644 --- a/src/UI/Canvas/CanvasPreview.gd +++ b/src/UI/Canvas/CanvasPreview.gd @@ -2,12 +2,24 @@ extends Node2D enum Mode { TIMELINE, SPRITESHEET } var mode := Mode.TIMELINE - +## Use this material only when the animation of the canvas preview is playing +## This way we optimize drawing when the frame being shown is the same as the main canvas +var animation_material := material as ShaderMaterial var h_frames := 1 var v_frames := 1 var start_sprite_sheet_frame := 1 var end_sprite_sheet_frame := 1 -var frame_index := 0 +var frame_index := 0: + set(value): + frame_index = value + if mode == Mode.SPRITESHEET: + return + if frame_index == Global.current_project.current_frame: # Animation not playing + if material != Global.canvas.material: + material = Global.canvas.material + else: # The animation of the canvas preview is playing + if material != animation_material: + material = animation_material @onready var animation_timer := $AnimationTimer as Timer @onready var transparent_checker = get_parent().get_node("TransparentChecker") as ColorRect @@ -15,6 +27,7 @@ var frame_index := 0 func _ready() -> void: Global.cel_changed.connect(_cel_changed) + material = Global.canvas.material func _draw() -> void: @@ -29,6 +42,10 @@ func _draw() -> void: animation_timer.wait_time = frame.duration * (1.0 / project.fps) var texture := frame.cels[0].image_texture draw_texture(texture, Vector2.ZERO) # Placeholder so we can have a material here + if material == animation_material: + # Only use a unique material if the animation of the canvas preview is playing + # Otherwise showing a different frame than the main canvas is impossible + _draw_layers() Mode.SPRITESHEET: var image := project.frames[project.current_frame].cels[0].get_image() var slices := _split_spritesheet(image, h_frames, v_frames) @@ -45,7 +62,6 @@ func _draw() -> void: # Placeholder so we can have a material here draw_texture_rect_region(texture, rect, src_rect) transparent_checker.fit_rect(rect) - _draw_layers() func _draw_layers() -> void: diff --git a/src/UI/Canvas/CanvasPreview.tscn b/src/UI/Canvas/CanvasPreview.tscn index 4d6780273..377a84101 100644 --- a/src/UI/Canvas/CanvasPreview.tscn +++ b/src/UI/Canvas/CanvasPreview.tscn @@ -7,6 +7,7 @@ shader = ExtResource("1_28j41") shader_parameter/opacities = null shader_parameter/blend_modes = null +shader_parameter/origins = null [node name="CanvasPreview" type="Node2D"] material = SubResource("ShaderMaterial_21d5l")