add CRT shader

This commit is contained in:
Chris Cromer 2022-08-03 21:38:13 -04:00
parent 4267332d70
commit 457915a46a
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
5 changed files with 106 additions and 2 deletions

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://Main.gdns" type="Script" id=1]
[ext_resource path="res://levels/Level2.tscn" type="PackedScene" id=2]
[ext_resource path="res://monitor/Monitor.tscn" type="PackedScene" id=3]
[ext_resource path="res://shaders/crt/crt.tscn" type="PackedScene" id=4]
[node name="Main" type="Node"]
pause_mode = 2
@ -13,3 +14,5 @@ level = ExtResource( 2 )
pause_mode = 1
[node name="Monitor" parent="." instance=ExtResource( 3 )]
[node name="CRT" parent="." instance=ExtResource( 4 )]

View File

@ -19,7 +19,7 @@ config/icon="res://icon.png"
window/size/width=512
window/size/height=288
window/stretch/mode="viewport"
window/stretch/mode="2d"
window/stretch/aspect="keep"
[editor]

View File

@ -0,0 +1,73 @@
shader_type canvas_item;
const float PI = 3.14159;
uniform vec2 resolution = vec2(1024.0, 576.0);
uniform bool show_curve = false;
uniform bool show_vignette = false;
uniform bool show_horizontal_scan_lines = true;
uniform bool show_vertical_scan_lines = false;
uniform bool gray_scale = false;
uniform float curvature_x_amount : hint_range(3.0, 15.0, 0.01) = 6.0;
uniform float curvature_y_amount : hint_range(3.0, 15.0, 0.01) = 6.0;
uniform vec4 corner_color : hint_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform float vignette_size : hint_range(1, 300, 0.1) = 4.0;
uniform float vignette_opacity : hint_range(0.0, 1.0, 0.01) = 1.0;
const float brightness = 3.0;
vec2 uv_curve(vec2 uv) {
if (show_curve) {
uv = uv * 2.0 - 1.0;
vec2 offset = abs(uv.yx) / vec2(curvature_x_amount, curvature_y_amount);
uv = uv + uv * offset * offset;
uv = uv * 0.5 + 0.5;
}
return uv;
}
void fragment() {
vec2 screen_uv = uv_curve(SCREEN_UV);
vec3 color = texture(SCREEN_TEXTURE, screen_uv).rgb;
if (show_horizontal_scan_lines || show_vertical_scan_lines) {
color.r = texture(SCREEN_TEXTURE, uv_curve(SCREEN_UV) + vec2(SCREEN_PIXEL_SIZE.x * 0.0), 0.0).r;
color.g = texture(SCREEN_TEXTURE, uv_curve(SCREEN_UV) + vec2(SCREEN_PIXEL_SIZE.x * 0.0), 0.0).g;
color.b = texture(SCREEN_TEXTURE, uv_curve(SCREEN_UV) + vec2(SCREEN_PIXEL_SIZE.x * 0.0), 0.0).b;
}
if (show_vignette) {
float vignette = UV.x * UV.y * (1.0 - UV.x) * (1.0 - UV.y);
vignette = clamp(pow((resolution.x / vignette_size) * vignette, vignette_opacity), 0.0, 1.0);
color *= vignette;
}
if (show_horizontal_scan_lines) {
float s = sin(uv_curve(SCREEN_UV).y * resolution.y / 2.0 * PI);
s = (s * 0.5 + 0.5) * 0.9 + 0.1;
vec4 scan_line = vec4(vec3(pow(s, 0.25)), 1.0);
color *= scan_line.rgb;
}
if (show_vertical_scan_lines) {
float s = sin(uv_curve(SCREEN_UV).x * resolution.x / 2.0 * PI);
s = (s * 0.5 + 0.5) * 0.9 + 0.1;
vec4 scan_line = vec4(vec3(pow(s, 0.25)), 1.0);
color *= scan_line.rgb;
}
if (show_curve) {
if (screen_uv.x < 0.0 || screen_uv.x > 1.0 || screen_uv.y < 0.0 || screen_uv.y > 1.0) {
color = corner_color.rgb;
}
}
if (gray_scale) {
float avg = (color.r + color.g + color.b) / brightness;
color = vec3(avg);
}
COLOR = vec4(color, 1.0);
}

View File

@ -0,0 +1,17 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=2]
[ext_resource path="res://shaders/crt/crt.shader" type="Shader" id=1]
[resource]
shader = ExtResource( 1 )
shader_param/resolution = Vector2( 1280, 720 )
shader_param/show_curve = false
shader_param/show_vignette = false
shader_param/show_horizontal_scan_lines = true
shader_param/show_vertical_scan_lines = false
shader_param/gray_scale = false
shader_param/curvature_x_amount = 6.0
shader_param/curvature_y_amount = 6.0
shader_param/corner_color = Color( 0, 0, 0, 1 )
shader_param/vignette_size = 4.0
shader_param/vignette_opacity = 1.0

View File

@ -0,0 +1,11 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://shaders/crt/crt.tres" type="Material" id=1]
[node name="CRT" type="CanvasLayer"]
[node name="ColorRect" type="ColorRect" parent="."]
material = ExtResource( 1 )
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2