summary refs log tree commit diff stats
path: root/shaders/ntsc.fragment
blob: d6ae904476bf0d95d01172610cd02aeac3523449 (plain) (blame)
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
#version 330 core

in vec2 UV;

out vec4 color;

uniform sampler2D curFrameSampler;
uniform sampler2D NTSCArtifactSampler;
uniform sampler2D prevFrameSampler;

const float Tuning_Sharp = 0.25;
const vec4 Tuning_Persistence = vec4(1.0) * 0.5;
const float Tuning_Bleed = 0.5;
uniform float Tuning_NTSC; // 0.0
uniform float NTSCLerp;

const vec2 RcpScrWidth = vec2(1.0f / 320.f, 0.0f);
const vec2 RcpScrHeight = vec2(0.0f, 1.0f / 200.0f);

float Brightness(vec4 InVal)
{
  return dot(InVal, vec4(0.299, 0.587, 0.114, 0.0));
}

const float SharpWeight[3] = float[3](1.0, -0.3162277, 0.1);

void main()
{
  vec4 NTSCArtifact1 = texture(NTSCArtifactSampler, UV);
  vec4 NTSCArtifact2 = texture(NTSCArtifactSampler, UV + RcpScrHeight);
  vec4 NTSCArtifact = mix(NTSCArtifact1, NTSCArtifact2, NTSCLerp);
  vec4 TunedNTSC = NTSCArtifact * Tuning_NTSC;
  
  vec2 LeftUV = UV - RcpScrWidth;
  vec2 RightUV = UV + RcpScrWidth;
  
  vec4 Cur_Left = texture(curFrameSampler, LeftUV);
  vec4 Cur_Local = texture(curFrameSampler, UV);
  vec4 Cur_Right = texture(curFrameSampler, RightUV);
  
  vec4 Prev_Left = texture(prevFrameSampler, LeftUV);
  vec4 Prev_Local = texture(prevFrameSampler, UV);
  vec4 Prev_Right = texture(prevFrameSampler, RightUV);
  
  Cur_Local = clamp(Cur_Local + (((Cur_Left - Cur_Local) + (Cur_Right - Cur_Local)) * TunedNTSC), vec4(0,0,0,0), vec4(1,1,1,1));
  
  float curBrt = Brightness(Cur_Local);
  float offset = 0;
  
  for (int i=0; i<3; ++i)
  {
    vec2 StepSize = (RcpScrWidth * float(i+1));
    vec4 neighborleft = texture(curFrameSampler, UV - StepSize);
    vec4 neighborright = texture(curFrameSampler, UV + StepSize);
    
    float NBrtL = Brightness(neighborleft);
    float NBrtR = Brightness(neighborright);
    offset += ((curBrt - NBrtL) + (curBrt - NBrtR)) * SharpWeight[i];
  }
  
  Cur_Local = clamp(Cur_Local + (offset * Tuning_Sharp * mix(vec4(1,1,1,1), NTSCArtifact, Tuning_NTSC)), vec4(0,0,0,0), vec4(1,1,1,1));
  Cur_Local = clamp(max(Cur_Local, Tuning_Persistence * (1.0 / (1.0 + (2.0 * Tuning_Bleed))) * (Prev_Local + ((Prev_Left + Prev_Right) * Tuning_Bleed))), vec4(0,0,0,0), vec4(1,1,1,1));
  
  color = vec4(Cur_Local.rgb, 1.0);
}