color banding effect

This commit is contained in:
Spencer Killen 2022-12-18 18:08:25 -07:00
parent 8f03e9993d
commit ab003700b3
Signed by: sjkillen
GPG Key ID: F307025B65C860BA
6 changed files with 136 additions and 10 deletions

View File

@ -1,2 +1,5 @@
https://godotshaders.com/shader/color-reduction-and-dither/ https://godotshaders.com/shader/color-reduction-and-dither/
https://godotshaders.com/shader/retro-dither/ https://godotshaders.com/shader/retro-dither/
https://godotshaders.com/shader/hsv-adjustment/
https://gist.github.com/msbarry/cd98f928542f5152111a

Binary file not shown.

View File

@ -0,0 +1,2 @@
36673
/home/squirrel/.local/bin/godot_mono

View File

@ -4,16 +4,12 @@
[sub_resource type="ShaderMaterial" id=1] [sub_resource type="ShaderMaterial" id=1]
shader = ExtResource( 1 ) shader = ExtResource( 1 )
shader_param/param = 12.0
shader_param/anim_period = 1.0
[node name="CanvasLayer" type="CanvasLayer"] [node name="CanvasLayer" type="CanvasLayer"]
[node name="TextureRect" type="TextureRect" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
material = SubResource( 1 ) material = SubResource( 1 )
anchor_left = 0.5 anchor_right = 1.0
anchor_top = 0.5 anchor_bottom = 1.0
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -512.0
margin_top = -300.0
margin_right = 512.0
margin_bottom = 300.0

View File

@ -1,3 +1,127 @@
[gd_resource type="Shader" format=2] [gd_resource type="Shader" format=2]
[resource] [resource]
code = "shader_type canvas_item;
// Color space code from
// HSV: https://godotshaders.com/shader/hsv-adjustment/
// LAB/XYZ: https://gist.github.com/msbarry/cd98f928542f5152111a
// Note: don't use the XYZ space, but sources use it to convert to LAB
uniform float param : hint_range(0.0, 255.0, 1.0);
vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
vec3 rgb2xyz(vec3 c) {
float R = ((c.r > 0.04045) ? pow((( c.r + 0.055 ) / 1.055), 2.4) : (c.r / 12.92)) * 100.0;
float G = ((c.g > 0.04045) ? pow((( c.g + 0.055 ) / 1.055), 2.4) : (c.g / 12.92)) * 100.0;
float B = ((c.b > 0.04045) ? pow((( c.b + 0.055 ) / 1.055), 2.4) : (c.b / 12.92)) * 100.0;
float X = R * 0.4124 + G * 0.3576 + B * 0.1805;
float Y = R * 0.2126 + G * 0.7152 + B * 0.0722;
float Z = R * 0.0193 + G * 0.1192 + B * 0.9505;
return vec3(X, Y, Z);
}
vec3 xyz2rgb(vec3 c) {
float X = c.x / 100.0;
float Y = c.y / 100.0;
float Z = c.z / 100.0;
float R = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
float G = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
float B = X * 0.0557 + Y * -0.2040 + Z * 1.0570;
R = ((R > 0.0031308) ? (1.055 * ( pow( R, 1./2.4 ) ) - 0.055) : (12.92 * R));
G = ((G > 0.0031308) ? (1.055 * ( pow( G, 1./2.4 ) ) - 0.055) : (12.92 * G));
B = ((B > 0.0031308) ? (1.055 * ( pow( B, 1./2.4 ) ) - 0.055) : (12.92 * B));
return vec3(R, G, B);
}
vec3 xyz2lab(vec3 c) {
float X = c.x / 95.047;
float Y = c.y / 100.0;
float Z = c.z / 108.883;
X = ((X > 0.008856) ? (pow( X, 1./3.)) : (( 7.787 * X ) + ( 16./116.)));
Y = ((Y > 0.008856) ? (pow( Y, 1./3.)) : (( 7.787 * Y ) + ( 16./116.)));
Z = ((Z > 0.008856) ? (pow( Z, 1./3.)) : (( 7.787 * Z ) + ( 16./116.)));
float L = ( 116. * Y ) - 16.;
float a = 500. * ( X - Y );
float b = 200. * ( Y - Z );
return vec3(L, a, b);
}
vec3 lab2xyz(vec3 c) {
float L = c.x;
float a = c.y;
float b = c.z;
float Y = ( L + 16. ) / 116.;
float X = a / 500. + Y;
float Z = Y - b / 200.;
Y = ((pow(Y,3.) > 0.008856) ? (pow(Y,3.)) : ((Y - 16. / 116.) / 7.787));
X = ((pow(X,3.) > 0.008856) ? (pow(X,3.)) : ((X - 16. / 116.) / 7.787));
Z = ((pow(Z,3.) > 0.008856) ? (pow(Z,3.)) : ((Z - 16. / 116.) / 7.787));
float ref_X = 95.047;
float ref_Y = 100.0;
float ref_Z = 108.883;
return vec3(ref_X * X, ref_Y * Y, ref_Z * Z);
}
vec3 band_colors(vec3 rgb)
{
return round(rgb * param) / param;
}
vec3 band_colors_preserve_hue_lum(vec3 rgb)
{
vec3 hsv = rgb2hsv(rgb);
hsv.r = round(hsv.r * param) / param;
vec3 lab = xyz2lab(rgb2xyz(rgb));
lab.r = round(lab.r * (param / 100.0)) / (param / 100.0);
// Into HSV
vec2 sv = rgb2hsv(xyz2rgb(lab2xyz(lab))).gb;
hsv.gb = sv;
return hsv2rgb(hsv);
}
uniform float anim_period : hint_range(0.1, 5, 0.01);
void fragment()
{
vec3 rgb = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
vec3 banded_preserved = band_colors_preserve_hue_lum(rgb);
vec3 banded_nostalgia = band_colors(rgb);
float weight = mod(TIME, anim_period) / anim_period;
float double_weight = mod(TIME, anim_period*2.0) / (anim_period*2.0);
weight = double_weight > 0.5 ? (1.-weight) : weight;
vec3 mixed = mix(banded_nostalgia, banded_preserved, weight);
COLOR.rgb = mixed;
}
"

View File

@ -8,6 +8,7 @@
[node name="Spatial" type="Spatial"] [node name="Spatial" type="Spatial"]
[node name="CanvasLayer" parent="." instance=ExtResource( 1 )] [node name="CanvasLayer" parent="." instance=ExtResource( 1 )]
layer = 99
[node name="environment" parent="." instance=ExtResource( 2 )] [node name="environment" parent="." instance=ExtResource( 2 )]
@ -22,5 +23,5 @@ environment = SubResource( 1 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00275469, 5.3306, -0.00586939 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00275469, 5.3306, -0.00586939 )
light_energy = 16.0 light_energy = 16.0
light_indirect_energy = 1.306 light_indirect_energy = 1.306
omni_range = 5.778 omni_range = 8.917
omni_attenuation = 2.73208 omni_attenuation = 2.73208