From 457915a46a408969e02610b0f5acf18d2cd310ba Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 3 Aug 2022 21:38:13 -0400 Subject: [PATCH] add CRT shader --- godot/Main.tscn | 5 ++- godot/project.godot | 2 +- godot/shaders/crt/crt.shader | 73 ++++++++++++++++++++++++++++++++++++ godot/shaders/crt/crt.tres | 17 +++++++++ godot/shaders/crt/crt.tscn | 11 ++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 godot/shaders/crt/crt.shader create mode 100644 godot/shaders/crt/crt.tres create mode 100644 godot/shaders/crt/crt.tscn diff --git a/godot/Main.tscn b/godot/Main.tscn index 480435f..2b91627 100644 --- a/godot/Main.tscn +++ b/godot/Main.tscn @@ -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 )] diff --git a/godot/project.godot b/godot/project.godot index b52d977..bc9a5b7 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -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] diff --git a/godot/shaders/crt/crt.shader b/godot/shaders/crt/crt.shader new file mode 100644 index 0000000..aa146a4 --- /dev/null +++ b/godot/shaders/crt/crt.shader @@ -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); +} \ No newline at end of file diff --git a/godot/shaders/crt/crt.tres b/godot/shaders/crt/crt.tres new file mode 100644 index 0000000..7215bcc --- /dev/null +++ b/godot/shaders/crt/crt.tres @@ -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 diff --git a/godot/shaders/crt/crt.tscn b/godot/shaders/crt/crt.tscn new file mode 100644 index 0000000..bfdef34 --- /dev/null +++ b/godot/shaders/crt/crt.tscn @@ -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 -- 2.30.2