Added balatroesque gunk material

This commit is contained in:
Rob Kelly 2025-03-02 16:46:50 -07:00
parent a89f7e24c5
commit 2128203b17
3 changed files with 136 additions and 67 deletions

View File

@ -0,0 +1,23 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://kfyacbbge0xi"]
[ext_resource type="Shader" path="res://src/shaders/gunk_2.gdshader" id="1_nrrfp"]
[ext_resource type="Texture2D" uid="uid://8cm835h4gxwe" path="res://assets/debug_mask.png" id="2_kh76g"]
[resource]
render_priority = 0
shader = ExtResource("1_nrrfp")
shader_parameter/color_1 = Color(0, 0.101961, 0.301961, 1)
shader_parameter/color_2 = Color(0.1289, 0.123, 0.3, 1)
shader_parameter/color_3 = Color(0, 0.039, 0.13, 1)
shader_parameter/spin_rotation = 0.0
shader_parameter/offset = Vector2(-0.35, 0.235)
shader_parameter/contrast = 2.0
shader_parameter/spin_amount = 7.98
shader_parameter/pixel_filter = 700.0
shader_parameter/convolution_factor = 0.05
shader_parameter/roughness = 0.15
shader_parameter/specular_contribution = 0.2
shader_parameter/uv_scale = Vector2(1, 1)
shader_parameter/time_scale = 0.01
shader_parameter/edge_bleed = 0.1
shader_parameter/gunk_mask = ExtResource("2_kh76g")

File diff suppressed because one or more lines are too long

108
src/shaders/gunk_2.gdshader Normal file
View File

@ -0,0 +1,108 @@
shader_type spatial;
render_mode cull_back;
uniform vec4 color_1: source_color = vec4(0.0, 0.03, 0.1, 1.0);
uniform vec4 color_2: source_color = vec4(0.0, 0.1, 0.3, 1.0);
uniform vec4 color_3: source_color = vec4(0.1, 0.35, 0.4, 1.0);
uniform highp float spin_rotation;
uniform highp vec2 offset = vec2(0.0, 0.0);
uniform highp float contrast = 2.0;
uniform highp float spin_amount = 0.36;
uniform highp float pixel_filter = 700.0;
#define SPIN_EASE 1.0
uniform float convolution_factor = 0.05;
uniform float roughness: hint_range(0.0, 1.0) = 0.15;
uniform float specular_contribution = 0.2;
// Used ONLY by the gunk, does not affect the gunk mask.
uniform vec2 uv_scale = vec2(1.0);
uniform float time_scale = 1.0;
uniform float edge_bleed = 0.1;
uniform sampler2D gunk_mask;
float brightness(vec3 color) {
return 0.2 * color.r + 0.7 * color.g + 0.1 * color.b;
}
// Balatro-esque effect
vec4 effect(highp vec2 scaled_uv) {
// Pixellate
highp vec2 uv = floor(scaled_uv.xy * pixel_filter) / pixel_filter - 0.5 - offset;
highp float uv_len = length(uv);
// Adding in a center swirl, changes with time. Only applies meaningfully if `spin_amount` != 0
highp float speed = 0.2 * SPIN_EASE * spin_rotation + 302.2;
highp float new_px_angle = atan(uv.y, uv.x) + speed - 20.0 * SPIN_EASE * uv_len;
uv = uv_len * vec2(cos(new_px_angle), sin(new_px_angle));
// Now add the paint effect to the swirled uvec2
uv *= 30.0;
speed = TIME * time_scale;
highp vec2 uv2 = vec2(uv.x + uv.y);
for(int i = 0; i < 5; i++) {
uv2 += sin(max(uv.x, uv.y)) + uv;
uv += 0.5 * vec2(cos(5.1123314 + 0.353 * uv2.y + 0.131121 * speed), sin(uv2.x - 0.113 * speed));
uv -= cos(uv.x + uv.y) - 1.0 * sin(0.711 * uv.x - uv.y);
}
// Adjust contrast & clamp on [0, 2]
highp float contrast_mod = 0.25 * contrast + 0.5 * spin_amount + 1.2;
highp float paint_res = clamp(0.035 * contrast_mod * length(uv), 0.0, 2.0);
highp float c1p = max(0.0, 1.0 - contrast_mod * abs(1.0 - paint_res));
highp float c2p = max(0.0, 1.0 - contrast_mod * abs(paint_res));
highp float c3p = 1.0 - min(1.0, c1p + c2p);
return 0.3 * color_1 / contrast + (color_1 * c1p + color_2 * c2p + vec4(c3p * color_3.rgb, c3p * color_1.a)) * (1.0 - 0.3 / contrast);
}
float hardstep(float value) {
float x = clamp(value, 0.0, 1.0);
return 0.5 * tanh( (20.0 * x - 10.0) * inversesqrt(x - x * x) ) + 0.5;
}
void fragment() {
vec2 local_uv = UV * uv_scale;
vec4 effect_color = effect(local_uv);
float value = brightness(effect_color.rgb);
float roughness_mix = value * roughness;
ALBEDO = effect_color.rgb;
ROUGHNESS = roughness_mix;
SPECULAR = 0.5 * inversesqrt(specular_contribution);
// Convolutional normal map
float sample_step = convolution_factor / pixel_filter;
float n = sqrt(brightness(effect(vec2(local_uv.x, local_uv.y - sample_step)).rgb));
float s = sqrt(brightness(effect(vec2(local_uv.x, local_uv.y + sample_step)).rgb));
float e = sqrt(brightness(effect(vec2(local_uv.x + sample_step, local_uv.y)).rgb));
float w = sqrt(brightness(effect(vec2(local_uv.x - sample_step, local_uv.y)).rgb));
float x = 0.5 * (w - e) + 0.5;
float y = 0.5 * (s - n) + 0.5;
NORMAL_MAP = vec3(x, y, clamp(1.0 - abs(x - 0.5) + abs(y - 0.5), 0.5, 1));
float mask = texture(gunk_mask, UV).r;
// soften edges
NORMAL_MAP *= smoothstep(1.0, 0.0, mask);
/*
// Hard edge
if(mask + edge_bleed < 0.5) {
ALPHA = 1.0;
} else {
ALPHA = 0.0;
}
*/
// Hardish edge
ALPHA = hardstep(1.0 - mask + edge_bleed);
}