summaryrefslogtreecommitdiff
path: root/Assets/Shaders/crt_shader.gdshader
blob: 5c698a665b6a022bd479014593bdd4dfceb11c14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Godot 3 2D CRT Shader.
A 2D shader for Godot 3 simulating a CRT..
Author: hiulit
Repository: https://github.com/hiulit/Godot-3-2D-CRT-Shader
Issues: https://github.com/hiulit/Godot-3-2D-CRT-Shader/issues
License: MIT https://github.com/hiulit/Godot-3-2D-CRT-Shader/blob/master/LICENSE
*/

shader_type canvas_item;


uniform bool chromatic_abberation_enabled = true;
uniform float chromatic_abberation_amount : hint_range(0.0, 10.0, 0.01) = 4.0;

uniform bool vignette_enabled = true;
uniform float vignette_opacity : hint_range(0.0, 1.0, 0.01) = 0.2;

uniform bool scan_lines_enabled = true;
uniform float scan_lines_count : hint_range(0, 180, 1) = 180;
uniform float scan_lines_opacity : hint_range(0.0, 1.0, 0.01) = 1.0;

uniform float boost_intensity : hint_range(1.0, 2.0, 0.01) = 1.2;

const float PI = 3.14159265359;

/**
 * Apply chromatic abberation the the given texture.
 */
vec3 chromatic_abberation(sampler2D tex, vec2 uv, vec2 screen_size) {
	float adjusted_amount = chromatic_abberation_amount / screen_size.x;

	vec3 color;

	color.r = texture(tex, vec2(clamp(uv.x + adjusted_amount, 0.0, 1.0), uv.y)).r;
	color.g = texture(tex,                                                  uv).g;
	color.b = texture(tex, vec2(clamp(uv.x - adjusted_amount, 0.0, 1.0), uv.y)).b;

	return color;
}

/**
 * Apply a vignette.
 */
vec3 vignette(vec3 color, vec2 uv, vec2 screen_size) {
	float vignette = uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y);
	vignette = clamp(pow((screen_size.x / 4.0) * vignette, vignette_opacity), 0.0, 1.0);
	return color * vignette;
}

/**
 * Apply scan lines
 */
vec3 scan_lines(vec3 color, vec2 uv) {
	float strength = clamp(sin(uv.y * scan_lines_count * PI * 2.0), 0.65, 0.85);
	vec3 scan_line = vec3(pow(strength, scan_lines_opacity));
	return color * scan_line * boost_intensity;
}

void fragment() {
	vec3 color = texture(SCREEN_TEXTURE, SCREEN_UV).rgb;
	vec2 screen_size = 1.0 / SCREEN_PIXEL_SIZE;

	color = mix(
		color,
		chromatic_abberation(SCREEN_TEXTURE, SCREEN_UV, screen_size),
		float(chromatic_abberation_enabled)
	);

	color = mix(
		color,
		vignette(color, SCREEN_UV, screen_size),
		float(vignette_enabled)
	);

	color = mix(
		color,
		scan_lines(color, SCREEN_UV),
		float(scan_lines_enabled)
	);

	COLOR = vec4(color, 1.0);
}