-
Notifications
You must be signed in to change notification settings - Fork 1
/
crt.frag
132 lines (113 loc) · 4.35 KB
/
crt.frag
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* CRT shader
*
* Copyright (C) 2010, 2011 cgwg, Themaister and DOLLS
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
uniform sampler2D rubyTexture;
uniform vec2 rubyInputSize;
uniform vec2 rubyOutputSize;
uniform vec2 rubyTextureSize;
varying vec2 texCoord;
varying vec2 one;
varying float mod_factor;
// Enable screen curvature.
#define CURVATURE
// Controls the intensity of the barrel distortion used to emulate the
// curvature of a CRT. 0.0 is perfectly flat, 1.0 is annoyingly
// distorted, higher values are increasingly ridiculous.
#define distortion 0.08
// Simulate a CRT gamma of 2.4.
#define inputGamma 2.4
// Compensate for the standard sRGB gamma of 2.2.
#define outputGamma 2.2
// Macros.
#define TEX2D(c) pow((texture2D(rubyTexture, (c)) * gl_Color), vec4(inputGamma))
#define PI 3.141592653589
// Apply radial distortion to the given coordinate.
vec2 radialDistortion(vec2 coord)
{
coord *= rubyTextureSize / rubyInputSize;
vec2 cc = coord - 0.5;
float dist = dot(cc, cc) * distortion;
return (coord + cc * (1.0 + dist) * dist) * rubyInputSize / rubyTextureSize;
}
// Calculate the influence of a scanline on the current pixel.
//
// 'distance' is the distance in texture coordinates from the current
// pixel to the scanline in question.
// 'color' is the colour of the scanline at the horizontal location of
// the current pixel.
vec4 scanlineWeights(float distance, vec4 color)
{
// The "width" of the scanline beam is set as 2*(1 + x^4) for
// each RGB channel.
vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
// The "weights" lines basically specify the formula that gives
// you the profile of the beam, i.e. the intensity as
// a function of distance from the vertical center of the
// scanline. In this case, it is gaussian if width=2, and
// becomes nongaussian for larger widths. Ideally this should
// be normalized so that the integral across the beam is
// independent of its width. That is, for a narrower beam
// "weights" should have a higher peak at the center of the
// scanline than for a wider beam.
vec4 weights = vec4(distance / 0.3);
return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
}
void main()
{
// Here's a helpful diagram to keep in mind while trying to
// understand the code:
//
// | | | | |
// -------------------------------
// | | | | |
// | 01 | 11 | 21 | 31 | <-- current scanline
// | | @ | | |
// -------------------------------
// | | | | |
// | 02 | 12 | 22 | 32 | <-- next scanline
// | | | | |
// -------------------------------
// | | | | |
//
// Each character-cell represents a pixel on the output
// surface, "@" represents the current pixel (always somewhere
// in the bottom half of the current scan-line, or the top-half
// of the next scanline). The grid of lines represents the
// edges of the texels of the underlying texture.
// Texture coordinates of the texel containing the active pixel.
#ifdef CURVATURE
vec2 xy = radialDistortion(texCoord);
#else
vec2 xy = texCoord;
#endif
// Of all the pixels that are mapped onto the texel we are
// currently rendering, which pixel are we currently rendering?
vec2 ratio_scale = xy * rubyTextureSize - vec2(0.5);
vec2 uv_ratio = fract(ratio_scale);
// Snap to the center of the underlying texel.
xy.y = (floor(ratio_scale.y) + 0.5) / rubyTextureSize.y;
// Calculate the effective colour of the current and next
// scanlines at the horizontal location of the current pixel.
vec4 col = TEX2D(xy);
vec4 col2 = TEX2D(xy + vec2(0.0, one.y));
// Calculate the influence of the current and next scanlines on
// the current pixel.
vec4 weights = scanlineWeights(uv_ratio.y, col);
vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
vec3 mul_res = (col * weights + col2 * weights2).rgb;
// dot-mask emulation:
// Output pixels are alternately tinted green and magenta.
vec3 dotMaskWeights = mix(
vec3(1.0, 0.7, 1.0),
vec3(0.7, 1.0, 0.7),
floor(mod(mod_factor, 2.0))
);
mul_res *= dotMaskWeights;
gl_FragColor = vec4(pow(mul_res, vec3(1.0 / outputGamma)), 1.0);
}