Skip to content
This repository has been archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
Load additional images via //!load directive - closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticByte committed Jun 9, 2024
1 parent 932c1e2 commit 88a6460
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/ImageViewport.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ extends SubViewport

var image_original_tex: ImageTexture
var image_result: Image
var load_uniform_regex: RegEx

func _ready():
load_uniform_regex = RegEx.new()
load_uniform_regex.compile(r'\/\/!load\s(\w*)\s(.*)')

func set_original_image(image: Image):
image_original_tex = ImageTexture.create_from_image(image)
Expand All @@ -16,6 +21,14 @@ func update():
image_sprite.texture = image_original_tex
var mat = ShaderMaterial.new()
mat.shader = Globals.shader
# load images from //!load directives and apply them to
# the material as shader parameters
for m in load_uniform_regex.search_all(Globals.shader.code):
var u_image = Image.load_from_file(m.strings[2])
mat.set_shader_parameter(
m.strings[1], # uniform param name
ImageTexture.create_from_image(u_image))
# assign material
image_sprite.material = mat
# Get viewport texture
await RenderingServer.frame_post_draw # for good measure
Expand Down
3 changes: 2 additions & 1 deletion src/presets/Presets.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const dir = "res://src/presets/shaders/"
@onready var presets = {
"Empty": load(dir + "empty.gdshader"),
"Greyscale": load(dir + "greyscale.gdshader"),
"Lowpass": load(dir + "lowpass.gdshader")
"Lowpass": load(dir + "lowpass.gdshader"),
"Mix": load(dir + "mix.gdshader")
}

var default_preset: String = "Empty"
8 changes: 8 additions & 0 deletions src/presets/shaders/mix.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shader_type canvas_item;

//!load img2 ./icon.png
uniform sampler2D img2: repeat_enable, filter_nearest;

void fragment() {
COLOR = mix(COLOR, texture(img2, UV), .5);
}

0 comments on commit 88a6460

Please sign in to comment.