From a7f1486ec35b076fe5ba2371db7d608ec8cec3ed Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Sat, 31 Aug 2024 04:53:27 +0500 Subject: [PATCH] Index Map (#1093) * Index Map * Formatting * improve description a bit. --- src/Classes/ShaderLoader.gd | 21 +++++++++++++++++++ src/Shaders/Effects/IndexMap.gdshader | 18 ++++++++++++++++ .../LayerEffects/LayerEffectsSettings.gd | 1 + 3 files changed, 40 insertions(+) create mode 100644 src/Shaders/Effects/IndexMap.gdshader diff --git a/src/Classes/ShaderLoader.gd b/src/Classes/ShaderLoader.gd index 85e9025c4..d8bf8aa97 100644 --- a/src/Classes/ShaderLoader.gd +++ b/src/Classes/ShaderLoader.gd @@ -15,10 +15,31 @@ static func create_ui_for_shader_uniforms( ) -> void: var code := shader.code.split("\n") var uniforms: PackedStringArray = [] + var description: String = "" + var descriprion_began := false for line in code: + ## Management of "end" tags + if line.begins_with("// (end DESCRIPTION)"): + descriprion_began = false + if descriprion_began: + description += "\n" + line.strip_edges() + + ## Detection of uniforms if line.begins_with("uniform"): uniforms.append(line) + ## Management of "begin" tags + elif line.begins_with("// (begin DESCRIPTION)"): + descriprion_began = true + ## Validation of begin/end tags + if descriprion_began == true: ## Description started but never ended. treat it as an error + print("Shader description started but never finished. Assuming empty description") + description = "" + if not description.is_empty(): + parent_node.tooltip_text = str( + "Description:\n", description.replace("//", "").strip_edges() + ) + for uniform in uniforms: # Example uniform: # uniform float parameter_name : hint_range(0, 255) = 100.0; diff --git a/src/Shaders/Effects/IndexMap.gdshader b/src/Shaders/Effects/IndexMap.gdshader new file mode 100644 index 000000000..b834b1772 --- /dev/null +++ b/src/Shaders/Effects/IndexMap.gdshader @@ -0,0 +1,18 @@ +// Authored by Variable (6 May 2022) +shader_type canvas_item; +render_mode unshaded; + +uniform sampler2D map_texture : filter_nearest; // The map texture + +// (begin DESCRIPTION) +// The Red and Green values (0-255) of tool color will be treated as an x and y position +// respectively instead of color components. +// When you draw, color will be taken from the x-y position in the "Map Texture". +// (end DESCRIPTION) +void fragment(){ + vec4 col = texture(TEXTURE, UV); + vec2 map_size = vec2(textureSize(map_texture, 0)); + vec2 lookup_uv = vec2(round(col.x * 255.0)/(map_size.x), round(col.y * 255.0)/(map_size.y)); + vec4 index_color = texture(map_texture, lookup_uv); + COLOR = index_color; +} diff --git a/src/UI/Timeline/LayerEffects/LayerEffectsSettings.gd b/src/UI/Timeline/LayerEffects/LayerEffectsSettings.gd index ef41fb278..3985dc75b 100644 --- a/src/UI/Timeline/LayerEffects/LayerEffectsSettings.gd +++ b/src/UI/Timeline/LayerEffects/LayerEffectsSettings.gd @@ -23,6 +23,7 @@ var effects: Array[LayerEffect] = [ LayerEffect.new("Pixelize", preload("res://src/Shaders/Effects/Pixelize.gdshader")), LayerEffect.new("Posterize", preload("res://src/Shaders/Effects/Posterize.gdshader")), LayerEffect.new("Gradient Map", preload("res://src/Shaders/Effects/GradientMap.gdshader")), + LayerEffect.new("Index Map", preload("res://src/Shaders/Effects/IndexMap.gdshader")), ] @onready var enabled_button: CheckButton = $VBoxContainer/HBoxContainer/EnabledButton