Advertisement
Guest User

Untitled

a guest
Oct 12th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. // CG shader
  2. // NES CRT simulation
  3. // by r57shell
  4. // thanks to feos & HardWareMan
  5.  
  6. void main_vertex
  7. (
  8.     float4 position : POSITION,
  9.     out float4 oPosition : POSITION,
  10.     uniform float4x4 modelViewProj,
  11.  
  12.     float2 tex : TEXCOORD,
  13.     out float2 oTex : TEXCOORD
  14. )
  15. {
  16.     oPosition = mul(modelViewProj, position);
  17.     oTex = tex;
  18. }
  19.  
  20. struct input
  21. {
  22.     float2 video_size;
  23.     float2 texture_size;
  24.     float2 output_size;
  25.     float  frame_count;
  26.     float  frame_direction;
  27.     float frame_rotation;
  28. };
  29.  
  30. const float pi = 3.142;
  31.  
  32. const float phase_x = 5./6.*2.;
  33. const float phase_y = 1./6.*2.;
  34.  
  35. vec3 monitor(uniform sampler2D tex : TEXUNIT0, vec2 p, uniform input IN)
  36. {
  37.     float2 size = IN.texture_size;
  38.     vec2 pos = floor(p*size);
  39.     vec2 uv = floor(pos)/size;
  40.     vec4 res = tex2D(tex, uv);
  41.     vec3 yuv = mul(float3x3(
  42.            0.299, 0.587,   0.114,
  43.           -0.299,-0.587,1.-0.114,
  44.         1.-0.299,-0.587,  -0.114), res.xyz);
  45.     float alpha = (p.x*size.x*phase_x+floor(p.y*size.y)*phase_y)*pi;
  46.     if (mod(p.y*size.y, 2.0) < 1.)
  47.         alpha = -alpha;
  48.     yuv.x += dot(yuv.yz*vec2(0.492,0.877),vec2`(sin(alpha),cos(alpha)));
  49.     vec3 rgb = mul(float3x3(
  50.         1.,  0.,                  1.,
  51.         1., -0.1942078364565588, -0.5093696763202726,
  52.         1.,  1.,                  0.), yuv);
  53.  
  54.     return rgb;
  55. }
  56.  
  57. // pos (left corner, sample size)
  58. vec4 monitor_sample(uniform sampler2D tex : TEXUNIT0, vec2 p, vec2 sample, uniform input IN)
  59. {
  60.     // linear interpolation was...
  61.     // now other thing.
  62.     // http://imgur.com/m8Z8trV
  63.     // AT LAST IT WORKS!!!!
  64.     // going to check in retroarch...
  65.     float2 size = IN.texture_size;
  66.     vec2 next = vec2(.25,1.)/size;
  67.     vec2 f = fract(vec2(4.,1.)*size*p);
  68.     sample *= vec2(4.,1.)*size;
  69.     vec2 l;
  70.     vec2 r;
  71.     if (f.x+sample.x < 1.)
  72.     {
  73.         l.x = f.x+sample.x;
  74.         r.x = 0.;
  75.     }
  76.     else
  77.     {
  78.         l.x = 1.-f.x;
  79.         r.x = min(1.,f.x+sample.x-1.);
  80.     }
  81.     if (f.y+sample.y < 1.)
  82.     {
  83.         l.y = f.y+sample.y;
  84.         r.y = 0.;
  85.     }
  86.     else
  87.     {
  88.         l.y = 1.-f.y;
  89.         r.y = min(1.,f.y+sample.y-1.);
  90.     }
  91.     vec3 top = mix(monitor(tex, p, IN), monitor(tex, p+vec2(next.x,0.), IN), r.x/(l.x+r.x));
  92.     vec3 bottom = mix(monitor(tex, p+vec2(0.,next.y), IN), monitor(tex, p+next, IN), r.x/(l.x+r.x));
  93.     return vec4(mix(top,bottom, r.y/(l.y+r.y)),1.0);
  94. }
  95.  
  96. float4 main_fragment(uniform sampler2D tex : TEXUNIT0, float2 coords : TEXCOORD0, uniform input IN) : COLOR
  97. {
  98.     return monitor_sample(tex, coords, 1./IN.output_size, IN);
  99.  
  100.     // difference
  101.     //float zoom = 8;
  102.     //vec4 sampled = monitor_sample(tex, coords, 1./zoom/IN.output_size, IN);
  103.     //vec4 simple = vec4(monitor(tex, coords, IN),1.);
  104.     //return vec4(length(sampled - simple)*5*sin(IN.frame_count/30.))+simple;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement