1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-31 07:29:49 +00:00

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.
This commit is contained in:
Emmanouil Papadeas 2023-11-06 22:51:21 +02:00
parent 5b67880a9b
commit ec57e5173d
2 changed files with 20 additions and 3 deletions

View file

@ -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:

View file

@ -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")