Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crt_material.tres
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

[resource]
shader = ExtResource( 1 )
shader_param/blend_color = Color( 1, 1, 1, 1 )
shader_param/boost = 1.2
shader_param/vignette_opacity = 0.35
shader_param/vignette_opacity = 0.3
shader_param/scanline_speed = 1.0
shader_param/show_grille = true
shader_param/show_scanlines = true
shader_param/show_vignette = true
shader_param/show_curvature = true

shader_param/screen_size = Vector2( 320, 180 )
50 changes: 26 additions & 24 deletions crt_shader.shader
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
shader_type canvas_item;
render_mode blend_mul;
render_mode blend_mix;

uniform vec4 blend_color : hint_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float boost : hint_range(1.0, 1.5, 0.05) = float(1.2);
uniform float vignette_opacity : hint_range(0.1, 0.5, 0.05) = float(0.3);
uniform float boost : hint_range(1.0, 1.5, 0.01) = float(1.2);
uniform float vignette_opacity : hint_range(0.1, 0.5, 0.01) = float(0.3);
uniform float scanline_speed : hint_range(0.0, 1.0, 0.01) = float(1.0);
uniform bool show_grille = true; // Grille only works in Stretch Mode: 2D.
uniform bool show_scanlines = true;
uniform bool show_vignette = true;
uniform bool show_curvature = true;
uniform vec2 screen_size = vec2(480.0, 270.0);

vec2 CRTCurveUV(vec2 uv) {
if(show_curvature) {
Expand All @@ -19,7 +20,7 @@ vec2 CRTCurveUV(vec2 uv) {
return uv;
}

void DrawVignette(inout vec4 color, vec2 uv) {
void DrawVignette(inout vec3 color, vec2 uv) {
if(show_vignette) {
float vignette = uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y);
vignette = clamp(pow(16.0 * vignette, vignette_opacity), 0.0, 1.0);
Expand All @@ -29,31 +30,32 @@ void DrawVignette(inout vec4 color, vec2 uv) {
}
}

void DrawScanline(inout vec4 color, vec2 uv, float time) {
float scanline = clamp(0.95 + 0.05 * cos(3.14 * (uv.y + 0.008 * time) * 240.0 * 1.0), 0.0, 1.0);
float grille = 0.85 + 0.15 * clamp(1.5 * cos(3.14 * uv.x * 640.0 * 1.0), 0.0, 1.0);

if(show_scanlines && !show_grille) {
color *= scanline * boost;
} else if(!show_scanlines && show_grille) {
color *= grille * boost;
} else if(show_scanlines && show_grille) {
color *= scanline * grille * boost;
} else {
color *= boost;
void DrawScanline(inout vec3 color, vec2 uv, float time) {
float scanline = clamp(0.95 + 0.05 * sin(3.14159 * (uv.y + 0.008 * time) * screen_size.y), 0.0, 1.0);
float grille = 0.85 + 0.15 * clamp(1.5 * sin(3.14159 * uv.x * screen_size.x), 0.0, 1.0);

if(show_scanlines) {
color *= scanline
}

if(show_grille) {
color *= grille
}

color *= boost;
}

void fragment() {
vec4 color = blend_color;

vec2 screen_crtUV = CRTCurveUV(SCREEN_UV);
vec3 color = texture(SCREEN_TEXTURE, screen_crtUV).rgb;

vec2 crtUV = CRTCurveUV(UV);
if (crtUV.x < 0.0 || crtUV.x > 1.0 || crtUV.y < 0.0 || crtUV.y > 1.0) {
color = vec4(0.0, 0.0, 0.0, 1.0);
color = vec3(0.0, 0.0, 0.0);
}

DrawVignette(color, crtUV);
DrawScanline(color, UV, TIME);

COLOR = vec4(color);
DrawScanline(color, crtUV, TIME * scanline_speed);
COLOR = vec4(color, 1.0);
}
8 changes: 6 additions & 2 deletions demo_scene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
[node name="background" type="ColorRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0.8, 0.74902, 0.501961, 1 )

[node name="icon" type="Sprite" parent="."]
position = Vector2( 153.033, 91.1485 )
position = Vector2( 71.3842, 70.2482 )
texture = ExtResource( 1 )

[node name="icon2" type="Sprite" parent="."]
position = Vector2( 246.99, 107.356 )
texture = ExtResource( 1 )

[node name="CRTShader" type="ColorRect" parent="."]
material = ExtResource( 2 )
anchor_right = 1.0
anchor_bottom = 1.0