From d290d00b12b5175e8de4cb26cad4d8d2c46dc59c Mon Sep 17 00:00:00 2001 From: XTRA Date: Tue, 8 Oct 2024 20:55:57 +0300 Subject: [PATCH] Implement dynamic shader selection --- assets/shaders/cromatic-abberation.gdshader | 6 +- assets/shaders/invert.gdshader | 7 ++ assets/shaders/pixelate.gdshader | 4 +- assets/shaders/pixelsort.gdshader | 28 ++++++++ .../random-pool/cromatic-abberation.gdshader | 44 ++++++++++++ assets/shaders/random-pool/invert.gdshader | 7 ++ .../shaders/random-pool/kaleidoscope.gdshader | 57 +++++++++++++++ assets/shaders/random-pool/pixelate.gdshader | 12 ++++ assets/shaders/random-pool/pixelsort.gdshader | 28 ++++++++ entities/cool-orb.tscn | 2 +- entities/kaleido_scope.tscn | 69 +++++++++++++++++-- 11 files changed, 252 insertions(+), 12 deletions(-) create mode 100644 assets/shaders/invert.gdshader create mode 100644 assets/shaders/pixelsort.gdshader create mode 100644 assets/shaders/random-pool/cromatic-abberation.gdshader create mode 100644 assets/shaders/random-pool/invert.gdshader create mode 100644 assets/shaders/random-pool/kaleidoscope.gdshader create mode 100644 assets/shaders/random-pool/pixelate.gdshader create mode 100644 assets/shaders/random-pool/pixelsort.gdshader diff --git a/assets/shaders/cromatic-abberation.gdshader b/assets/shaders/cromatic-abberation.gdshader index eb28432..f1b7307 100644 --- a/assets/shaders/cromatic-abberation.gdshader +++ b/assets/shaders/cromatic-abberation.gdshader @@ -3,9 +3,9 @@ shader_type canvas_item; uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap; -uniform float chaos : hint_range(0., 32.) = 1.; -uniform float radius : hint_range(0., 1.) = 0.5; -uniform float attenuation : hint_range(1., 5.) = 2.; +uniform float chaos : hint_range(0., 32.) = 15.; +uniform float radius : hint_range(0., 1.) = 0.8; +uniform float attenuation : hint_range(1., 5.) = 4.; varying vec2 amount_r; varying vec2 amount_g; diff --git a/assets/shaders/invert.gdshader b/assets/shaders/invert.gdshader new file mode 100644 index 0000000..2c9e725 --- /dev/null +++ b/assets/shaders/invert.gdshader @@ -0,0 +1,7 @@ +shader_type canvas_item; + +void fragment() { + vec3 new_color = texture(TEXTURE, SCREEN_UV, 0.0).rgb; + new_color = vec3(1.0) - new_color; + COLOR.rgb = new_color; +} diff --git a/assets/shaders/pixelate.gdshader b/assets/shaders/pixelate.gdshader index 650771f..a0989b9 100644 --- a/assets/shaders/pixelate.gdshader +++ b/assets/shaders/pixelate.gdshader @@ -1,7 +1,7 @@ shader_type canvas_item; -uniform int x_pixel_size : hint_range(1, 100); -uniform int y_pixel_size : hint_range(1, 100); +uniform int x_pixel_size : hint_range(1, 100) = 3; +uniform int y_pixel_size : hint_range(1, 100) = 1; void fragment() { vec2 correction = TEXTURE_PIXEL_SIZE * vec2(float(x_pixel_size), float(y_pixel_size)) / vec2(2.0); diff --git a/assets/shaders/pixelsort.gdshader b/assets/shaders/pixelsort.gdshader new file mode 100644 index 0000000..a8e4253 --- /dev/null +++ b/assets/shaders/pixelsort.gdshader @@ -0,0 +1,28 @@ +//MADE BY : @ahopness +//LICENSE : CC0 +//COMATIBLE WITH : GLES2, GLES3, WEBGL + +shader_type canvas_item; + + +uniform float sort :hint_range(0.0, 2.6)= .5; + +void fragment(){ + vec2 uv = FRAGCOORD.xy / (1.0 / SCREEN_PIXEL_SIZE).xy; + + // Pseudo Pixel Sorting + float sortThreshold = 1.0 - clamp(sort / 2.6, 0.0, 1.0); + vec2 sortUv = vec2(uv.x, sortThreshold); + + // Curved melting transition + vec2 transitionUV = uv; + transitionUV.y += pow(sort, 2.0 + (sort * 2.0)) * uv.x * fract(sin(dot(vec2(transitionUV.x), vec2(12.9, 78.2)))* 437.5); + COLOR = texture(TEXTURE, transitionUV); + + // Draw pixel sorting effect behind the melting transition + if(transitionUV.y > 1.){ + COLOR = texture(TEXTURE, sortUv); + }else{ + COLOR = texture(TEXTURE, uv); + } +} \ No newline at end of file diff --git a/assets/shaders/random-pool/cromatic-abberation.gdshader b/assets/shaders/random-pool/cromatic-abberation.gdshader new file mode 100644 index 0000000..f1b7307 --- /dev/null +++ b/assets/shaders/random-pool/cromatic-abberation.gdshader @@ -0,0 +1,44 @@ +//shader by CasualGarageCoder, updated to Godot 4 +shader_type canvas_item; + +uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap; + +uniform float chaos : hint_range(0., 32.) = 15.; +uniform float radius : hint_range(0., 1.) = 0.8; +uniform float attenuation : hint_range(1., 5.) = 4.; + +varying vec2 amount_r; +varying vec2 amount_g; +varying vec2 amount_b; + +float rand(vec2 co){ + return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453) - .5; +} + +void vertex() { + vec2 shifted_uv = (UV * 2.) - 1.; + amount_r = normalize( + vec2(rand(TIME * 1.3 * shifted_uv), + rand(TIME * 1.64 * shifted_uv))); + amount_g = normalize( + vec2(rand(TIME * 1.5 * shifted_uv), + rand(TIME * 1.7 * shifted_uv))); + amount_b = normalize( + vec2(rand(TIME * 1.17 * shifted_uv), + rand(TIME * 1.23 * shifted_uv))); +} + +void fragment() { + vec2 chaos_v = vec2(chaos, -chaos) * SCREEN_PIXEL_SIZE; + + float dist = length((UV - vec2(0.5)) * 2.); + float att = clamp(dist / radius, 0., 1.); + + chaos_v *= 1. - pow(att, attenuation); + + COLOR = vec4( + texture(TEXTURE, SCREEN_UV + chaos_v * amount_r).r, + texture(TEXTURE, SCREEN_UV + chaos_v * amount_g).g, + texture(TEXTURE, SCREEN_UV + chaos_v * amount_b).b, + 1.); +} \ No newline at end of file diff --git a/assets/shaders/random-pool/invert.gdshader b/assets/shaders/random-pool/invert.gdshader new file mode 100644 index 0000000..2c9e725 --- /dev/null +++ b/assets/shaders/random-pool/invert.gdshader @@ -0,0 +1,7 @@ +shader_type canvas_item; + +void fragment() { + vec3 new_color = texture(TEXTURE, SCREEN_UV, 0.0).rgb; + new_color = vec3(1.0) - new_color; + COLOR.rgb = new_color; +} diff --git a/assets/shaders/random-pool/kaleidoscope.gdshader b/assets/shaders/random-pool/kaleidoscope.gdshader new file mode 100644 index 0000000..bc6bd0f --- /dev/null +++ b/assets/shaders/random-pool/kaleidoscope.gdshader @@ -0,0 +1,57 @@ +shader_type canvas_item; + + +group_uniforms segments; + uniform float segments: hint_range(2.0, 48.0, 1.0) = 6.0; + uniform bool segmentReflect = true; +group_uniforms; +group_uniforms polar; + uniform vec2 polarOffset = vec2(0.5, 0.5); + uniform float polarAngle: hint_range(0.0, 360.0) = 0.0; +group_uniforms; +group_uniforms source; + uniform vec2 sourceOffset = vec2(0.5, 0.5); + uniform float sourceScale = 2.0; + uniform float sourceAngle: hint_range(0.0, 360.0) = 0.0; +group_uniforms; + + +vec2 simpleRotate(vec2 uv, float rotation) { + return vec2( + cos(rotation) * uv.x + sin(rotation) * uv.y, + cos(rotation) * uv.y - sin(rotation) * uv.x + ); +} + + +vec2 kaleid(vec2 uv, float addAngle, vec2 origin, float segCount, bool doReflect) { + + uv -= origin; + float radius = sqrt(dot(uv, uv)); + float angle = atan(uv.y, uv.x) + addAngle; + + float segmentAngle = TAU / segCount; + angle -= segmentAngle * floor(angle / segmentAngle); + + if (doReflect) { + angle = min(angle, segmentAngle - angle); + } + + return vec2(cos(angle), sin(angle)) * radius; + +} + + +void fragment() { + + vec2 newUV = kaleid(UV, radians(polarAngle), polarOffset, segments, segmentReflect); + + newUV = simpleRotate(newUV, radians(sourceAngle)); + newUV += sourceOffset; + newUV *= sourceScale; + + vec4 finalColor = texture(TEXTURE, fract(newUV * sourceScale - sourceOffset)); + + COLOR = finalColor; + +} \ No newline at end of file diff --git a/assets/shaders/random-pool/pixelate.gdshader b/assets/shaders/random-pool/pixelate.gdshader new file mode 100644 index 0000000..a0989b9 --- /dev/null +++ b/assets/shaders/random-pool/pixelate.gdshader @@ -0,0 +1,12 @@ +shader_type canvas_item; + +uniform int x_pixel_size : hint_range(1, 100) = 3; +uniform int y_pixel_size : hint_range(1, 100) = 1; + +void fragment() { + vec2 correction = TEXTURE_PIXEL_SIZE * vec2(float(x_pixel_size), float(y_pixel_size)) / vec2(2.0); + vec2 texture_uv = floor(UV / TEXTURE_PIXEL_SIZE); + vec2 offset = vec2(float(int(texture_uv.x) % x_pixel_size), float(int(texture_uv.y) % y_pixel_size)); + vec2 target = (texture_uv - offset) * TEXTURE_PIXEL_SIZE; + COLOR = texture(TEXTURE, target + correction ); +} \ No newline at end of file diff --git a/assets/shaders/random-pool/pixelsort.gdshader b/assets/shaders/random-pool/pixelsort.gdshader new file mode 100644 index 0000000..a8e4253 --- /dev/null +++ b/assets/shaders/random-pool/pixelsort.gdshader @@ -0,0 +1,28 @@ +//MADE BY : @ahopness +//LICENSE : CC0 +//COMATIBLE WITH : GLES2, GLES3, WEBGL + +shader_type canvas_item; + + +uniform float sort :hint_range(0.0, 2.6)= .5; + +void fragment(){ + vec2 uv = FRAGCOORD.xy / (1.0 / SCREEN_PIXEL_SIZE).xy; + + // Pseudo Pixel Sorting + float sortThreshold = 1.0 - clamp(sort / 2.6, 0.0, 1.0); + vec2 sortUv = vec2(uv.x, sortThreshold); + + // Curved melting transition + vec2 transitionUV = uv; + transitionUV.y += pow(sort, 2.0 + (sort * 2.0)) * uv.x * fract(sin(dot(vec2(transitionUV.x), vec2(12.9, 78.2)))* 437.5); + COLOR = texture(TEXTURE, transitionUV); + + // Draw pixel sorting effect behind the melting transition + if(transitionUV.y > 1.){ + COLOR = texture(TEXTURE, sortUv); + }else{ + COLOR = texture(TEXTURE, uv); + } +} \ No newline at end of file diff --git a/entities/cool-orb.tscn b/entities/cool-orb.tscn index 8aeb6b3..17d8f04 100644 --- a/entities/cool-orb.tscn +++ b/entities/cool-orb.tscn @@ -17,7 +17,7 @@ void fragment() { ROUGHNESS = 0.2; RIM = 100.0; } - + } //void light() { diff --git a/entities/kaleido_scope.tscn b/entities/kaleido_scope.tscn index 40e8f94..5c3635b 100644 --- a/entities/kaleido_scope.tscn +++ b/entities/kaleido_scope.tscn @@ -1,15 +1,71 @@ -[gd_scene load_steps=22 format=3 uid="uid://b761uk7sjprrp"] +[gd_scene load_steps=23 format=3 uid="uid://b761uk7sjprrp"] [ext_resource type="Shader" path="res://assets/shaders/kaleidoscope.gdshader" id="1_nwvg1"] [ext_resource type="PackedScene" uid="uid://qehbjfkqs5pl" path="res://entities/ParticlesCool.tscn" id="1_yc1r8"] [ext_resource type="PackedScene" uid="uid://br7cnj1spc4rk" path="res://assets/auto_scale.tscn" id="2_7v8f2"] -[ext_resource type="Shader" path="res://assets/shaders/pixelate.gdshader" id="4_lvys8"] +[ext_resource type="Shader" path="res://assets/shaders/pixelsort.gdshader" id="4_v65hu"] [ext_resource type="Shader" path="res://assets/shaders/cromatic-abberation.gdshader" id="5_w16wy"] [sub_resource type="GDScript" id="GDScript_mtsk5"] script/source = "extends Control +@onready var pass3: TextureRect = $RandomPasses/Pass3/TextureRect; +@onready var pass2: TextureRect = $RandomPasses/Pass2/TextureRect; +@onready var pass1: TextureRect = $RandomPasses/Pass1/TextureRect; +var materials: Array[ShaderMaterial]; + +var shaders: Array[Shader]; + +func _ready() -> void: + #TODO: FIX RNG SO IT FEELS LESS RANDOM, SO WE DON'T GET THE EXACT SAME SHOW EVERY TIME WE RUN + + var shader_dir: DirAccess = DirAccess.open(\"res://assets/shaders/random-pool\"); + shader_dir.list_dir_begin() + for file: String in shader_dir.get_files(): + # These should probably be preloaded, but who the fuck cares, the demoparty is in 2 days I don't have time for this + var shader: Shader = load(shader_dir.get_current_dir() + \"/\" + file); + shader.set_meta(\"shader_name\", file.split(\".gdshader\", false)) + shaders.push_back(shader); + + for i: int in 3: + var material: ShaderMaterial = ShaderMaterial.new(); + var shader: Shader = shaders.pick_random(); + material.shader = shader.duplicate(); + + var shader_name: String = \"\".join(shader.get_meta(\"shader_name\")); + + print(shader.get_meta(\"shader_name\")) + match shader_name: + \"pixelate\": + print(\"pixelate\") + material.set_shader_parameter(\"x_pixel_size\", randi_range(1, 5)); + material.set_shader_parameter(\"y_pixel_size\", randi_range(1, 5)); + + \"cromatic-abberation\": + print(\"cromatic-abberation\") #Tiiän et täs on typo FFS, en rupee renamaa tiedostoo + material.set_shader_parameter(\"chaos\", randf_range(10.0, 32.0)); + material.set_shader_parameter(\"radius\", randf_range(0.3, 1.0)); + material.set_shader_parameter(\"attenuation\", randf_range(2.0, 5.0)); + + \"kaleidoscope\": + print(\"kaleidoscope\") + material.set_shader_parameter(\"segments\", float(randi_range(5.0, 8.0))); + + \"pixelsort\": + print(\"pixelsort\") + material.set_shader_parameter(\"sort\", randf_range(0.1, 2.6)); + + _: + print(\"something else\") + pass + + materials.push_back(material); + + pass1.material = materials[0]; + pass2.material = materials[1]; + pass3.material = materials[2]; + func _on_timer_timeout() -> void: $AnimationPlayer.play(\"kaleidoskope_1\"); " @@ -181,9 +237,8 @@ font_size = 104 shadow_size = 0 [sub_resource type="ShaderMaterial" id="ShaderMaterial_riy6b"] -shader = ExtResource("4_lvys8") -shader_parameter/x_pixel_size = 1 -shader_parameter/y_pixel_size = 15 +shader = ExtResource("4_v65hu") +shader_parameter/sort = 1.21 [sub_resource type="ViewportTexture" id="ViewportTexture_ff16u"] viewport_path = NodePath("KaleidoViewport") @@ -197,6 +252,8 @@ shader_parameter/attenuation = 1.5 [sub_resource type="ViewportTexture" id="ViewportTexture_3bfaj"] viewport_path = NodePath("RandomPasses/Pass1") +[sub_resource type="ShaderMaterial" id="ShaderMaterial_j7uru"] + [sub_resource type="ViewportTexture" id="ViewportTexture_rmol4"] viewport_path = NodePath("RandomPasses/Pass2") @@ -319,7 +376,7 @@ texture = SubResource("ViewportTexture_3bfaj") size = Vector2i(1152, 648) [node name="TextureRect" type="TextureRect" parent="RandomPasses/Pass3"] -material = SubResource("ShaderMaterial_wyd4s") +material = SubResource("ShaderMaterial_j7uru") offset_right = 40.0 offset_bottom = 40.0 texture = SubResource("ViewportTexture_rmol4")