From 5df01a91707aa74b984cb35417d7ca8f7ee4cf8e Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Mon, 10 Feb 2025 20:15:45 +0200 Subject: [PATCH] Users can now add custom dithering matrix images Not exposed somewhere in the UI yet, but users can now add custom dithering pattern as images in "user://dither_matrices". Right now only used for gradient generation. --- src/Classes/ShaderLoader.gd | 24 +++++++++++++++++++ src/Main.gd | 6 +++++ src/UI/Dialogs/ImageEffects/GradientDialog.gd | 21 +++------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Classes/ShaderLoader.gd b/src/Classes/ShaderLoader.gd index f749362c4..5f2b58c6c 100644 --- a/src/Classes/ShaderLoader.gd +++ b/src/Classes/ShaderLoader.gd @@ -6,6 +6,30 @@ const BASIS_SLIDERS_TSCN := preload("res://src/UI/Nodes/Sliders/BasisSliders.tsc const GRADIENT_EDIT_TSCN := preload("res://src/UI/Nodes/GradientEdit.tscn") const NOISE_GENERATOR := preload("res://src/UI/Nodes/NoiseGeneratorDialog.tscn") +static var dither_matrices: Array[DitherMatrix] = [ + DitherMatrix.new(preload("res://assets/dither-matrices/bayer2.png"), "Bayer 2x2"), + DitherMatrix.new(preload("res://assets/dither-matrices/bayer4.png"), "Bayer 4x4"), + DitherMatrix.new(preload("res://assets/dither-matrices/bayer8.png"), "Bayer 8x8"), + DitherMatrix.new(preload("res://assets/dither-matrices/bayer16.png"), "Bayer 16x16"), +] + + +class DitherMatrix: + var texture: Texture2D + var name: String + + func _init(_texture: Texture2D, _name: String) -> void: + texture = _texture + name = _name + + +static func load_dither_matrix_from_file(file_path: String) -> void: + var dither_image := Image.load_from_file(file_path) + if is_instance_valid(dither_image): + var dither_tex := ImageTexture.create_from_image(dither_image) + var dither_matrix := DitherMatrix.new(dither_tex, file_path.get_file().get_basename()) + dither_matrices.append(dither_matrix) + static func create_ui_for_shader_uniforms( shader: Shader, diff --git a/src/Main.gd b/src/Main.gd index e98439a2a..ba370bd2c 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -169,6 +169,12 @@ func _init() -> void: DirAccess.make_dir_recursive_absolute("user://backups") Global.shrink = _get_auto_display_scale() _handle_layout_files() + # Load dither matrix images. + var dither_matrices_path := "user://dither_matrices" + if DirAccess.dir_exists_absolute(dither_matrices_path): + for file_name in DirAccess.get_files_at(dither_matrices_path): + var file_path := dither_matrices_path.path_join(file_name) + ShaderLoader.load_dither_matrix_from_file(file_path) func _ready() -> void: diff --git a/src/UI/Dialogs/ImageEffects/GradientDialog.gd b/src/UI/Dialogs/ImageEffects/GradientDialog.gd index a49dcc29b..2652b82fc 100644 --- a/src/UI/Dialogs/ImageEffects/GradientDialog.gd +++ b/src/UI/Dialogs/ImageEffects/GradientDialog.gd @@ -7,13 +7,7 @@ var shader_linear := preload("res://src/Shaders/Effects/Gradients/Linear.gdshade var shader_linear_dither := preload("res://src/Shaders/Effects/Gradients/LinearDithering.gdshader") var shader := shader_linear -var dither_matrices: Array[DitherMatrix] = [ - DitherMatrix.new(preload("res://assets/dither-matrices/bayer2.png"), "Bayer 2x2"), - DitherMatrix.new(preload("res://assets/dither-matrices/bayer4.png"), "Bayer 4x4"), - DitherMatrix.new(preload("res://assets/dither-matrices/bayer8.png"), "Bayer 8x8"), - DitherMatrix.new(preload("res://assets/dither-matrices/bayer16.png"), "Bayer 16x16"), -] -var selected_dither_matrix := dither_matrices[0] +var selected_dither_matrix := ShaderLoader.dither_matrices[0] @onready var options_cont: Container = $VBoxContainer/GradientOptions @onready var gradient_edit: GradientEditNode = $VBoxContainer/GradientEdit @@ -27,22 +21,13 @@ var selected_dither_matrix := dither_matrices[0] @onready var radius_slider := $"%RadiusSlider" as ValueSliderV2 -class DitherMatrix: - var texture: Texture2D - var name: String - - func _init(_texture: Texture2D, _name: String) -> void: - texture = _texture - name = _name - - func _ready() -> void: super._ready() var sm := ShaderMaterial.new() sm.shader = shader preview.set_material(sm) - for matrix in dither_matrices: + for matrix in ShaderLoader.dither_matrices: dithering_option_button.add_item(matrix.name) # Set as in the Animate enum @@ -140,7 +125,7 @@ func _value_v2_changed(_value: Vector2) -> void: func _on_DitheringOptionButton_item_selected(index: int) -> void: if index > 0: shader = shader_linear_dither - selected_dither_matrix = dither_matrices[index - 1] + selected_dither_matrix = ShaderLoader.dither_matrices[index - 1] else: shader = shader_linear update_preview()