Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. //
  2. // PUBLIC DOMAIN CRT STYLED SCAN-LINE SHADER
  3. //
  4. // by Timothy Lottes
  5. //
  6. // This is more along the style of a really good CGA arcade monitor.
  7. // With RGB inputs instead of NTSC.
  8. // The shadow mask example has the mask rotated 90 degrees for less chromatic aberration.
  9. //
  10. // Left it unoptimized to show the theory behind the algorithm.
  11. //
  12. // It is an example what I personally would want as a display option for pixel art games.
  13. // Please take and use, change, or whatever.
  14. //
  15.  
  16. // -- config -- //
  17. #pragma parameter hardScan "hardScan" -8.0 -20.0 0.0 1.0 // default, minimum, maximum, optional step
  18. #pragma parameter hardPix "hardPix" -3.0 -20.0 0.0 1.0
  19. #pragma parameter warpX "warpX" 0.031 0.0 0.125 0.01
  20. #pragma parameter warpY "warpY" 0.041 0.0 0.125 0.01
  21. #pragma parameter maskDark "maskDark" 0.5 0.0 2.0 0.1
  22. #pragma parameter maskLight "maskLight" 1.5 0.0 2.0 0.1
  23. #pragma parameter scaleInLinearGamma "scaleInLinearGamma" 1.0 0.0 1.0 1.0
  24. #pragma parameter shadowMask "shadowMask" 1.0 0.0 1.0 1.0
  25. #pragma parameter brightboost "brightness" 1.0 0.0 2.0 0.05
  26. #pragma parameter hardBloomPix "bloom-x soft" -1.5 -2.0 -0.5 0.1
  27. #pragma parameter hardBloomScan "bloom-y soft" -2.0 -4.0 -1.0 0.1
  28. #pragma parameter bloomAmount "bloom amt" 0.15 0.0 1.0 0.05
  29.  
  30. #ifdef PARAMETER_UNIFORM // If the shader implementation understands #pragma parameters, this is defined.
  31. uniform float hardScan;
  32. uniform float hardPix;
  33. uniform float warpX;
  34. uniform float warpY;
  35. uniform float maskDark;
  36. uniform float maskLight;
  37. uniform float scaleInLinearGamma;
  38. uniform float shadowMask;
  39. uniform float brightboost;
  40. uniform float hardBloomScan;
  41. uniform float hardBloomPix;
  42. uniform float bloomAmount;
  43. #else
  44. #define hardScan -8.0
  45. #define hardPix -3.0
  46. #define warpX 0.031
  47. #define warpY 0.041
  48. #define maskDark 0.5
  49. #define maskLight 1.5
  50. #define scaleInLinearGamma 1
  51. #define shadowMask 1
  52. #define brightboost 1
  53. #define hardBloomScan -2.0
  54. #define hardBloomPix -1.5
  55. #define bloomAmount 1.0/16.0
  56.  
  57. #endif
  58. // ------------- //
  59.  
  60. void main_vertex
  61. (
  62. float4 position : POSITION,
  63. out float4 oPosition : POSITION,
  64. uniform float4x4 modelViewProj,
  65. float2 tex : TEXCOORD,
  66. out float2 oTex : TEXCOORD
  67. )
  68. {
  69. oPosition = mul(modelViewProj, position);
  70. oTex = tex;
  71. }
  72.  
  73. struct input
  74. {
  75. float2 video_size;
  76. float2 texture_size;
  77. float2 output_size;
  78. float frame_count;
  79. float frame_direction;
  80. float frame_rotation;
  81. };
  82.  
  83. input IN_global;
  84. sampler2D s0_global;
  85.  
  86. #define warp float2(warpX,warpY)
  87.  
  88. //------------------------------------------------------------------------
  89.  
  90. // sRGB to Linear.
  91. // Assuing using sRGB typed textures this should not be needed.
  92. float ToLinear1(float c)
  93. {
  94. if (scaleInLinearGamma==0) return c;
  95. return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);
  96. }
  97. float3 ToLinear(float3 c)
  98. {
  99. if (scaleInLinearGamma==0) return c;
  100. return float3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));
  101. }
  102.  
  103. // Linear to sRGB.
  104. // Assuing using sRGB typed textures this should not be needed.
  105. float ToSrgb1(float c)
  106. {
  107. if (scaleInLinearGamma==0) return c;
  108. return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);
  109. }
  110.  
  111. float3 ToSrgb(float3 c)
  112. {
  113. if (scaleInLinearGamma==0) return c;
  114. return float3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));
  115. }
  116.  
  117. // Nearest emulated sample given floating point position and texel offset.
  118. // Also zero's off screen.
  119. float3 Fetch(float2 pos,float2 off){
  120. pos=(floor(pos*IN_global.texture_size.xy+off)+float2(0.5,0.5))/IN_global.texture_size.xy;
  121. return ToLinear(brightboost * tex2D(s0_global,pos.xy).rgb);}
  122.  
  123. // Distance in emulated pixels to nearest texel.
  124. float2 Dist(float2 pos){pos=pos*IN_global.texture_size.xy;return -((pos-floor(pos))-float2(0.5,0));}
  125.  
  126. // 1D Gaussian.
  127. float Gaus(float pos,float scale){return exp2(scale*pos*pos);}
  128.  
  129. // 3-tap Gaussian filter along horz line.
  130. float3 Horz3(float2 pos,float off){
  131. float3 b=Fetch(pos,float2(-1.0,off));
  132. float3 c=Fetch(pos,float2( 0.0,off));
  133. float3 d=Fetch(pos,float2( 1.0,off));
  134. float dst=Dist(pos).x;
  135. // Convert distance to weight.
  136. float scale=hardPix;
  137. float wb=Gaus(dst-1.0,scale);
  138. float wc=Gaus(dst+0.0,scale);
  139. float wd=Gaus(dst+1.0,scale);
  140. // Return filtered sample.
  141. return (b*wb+c*wc+d*wd)/(wb+wc+wd);}
  142.  
  143. // 5-tap Gaussian filter along horz line.
  144. float3 Horz5(float2 pos,float off){
  145. float3 a=Fetch(pos,float2(-2.0,off));
  146. float3 b=Fetch(pos,float2(-1.0,off));
  147. float3 c=Fetch(pos,float2( 0.0,off));
  148. float3 d=Fetch(pos,float2( 1.0,off));
  149. float3 e=Fetch(pos,float2( 2.0,off));
  150. float dst=Dist(pos).x;
  151. // Convert distance to weight.
  152. float scale=hardPix;
  153. float wa=Gaus(dst-2.0,scale);
  154. float wb=Gaus(dst-1.0,scale);
  155. float wc=Gaus(dst+0.0,scale);
  156. float wd=Gaus(dst+1.0,scale);
  157. float we=Gaus(dst+2.0,scale);
  158. // Return filtered sample.
  159. return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);}
  160.  
  161. // 7-tap Gaussian filter along horz line.
  162. float3 Horz7(float2 pos,float off){
  163. float3 a=Fetch(pos,float2(-3.0,off));
  164. float3 b=Fetch(pos,float2(-2.0,off));
  165. float3 c=Fetch(pos,float2(-1.0,off));
  166. float3 d=Fetch(pos,float2( 0.0,off));
  167. float3 e=Fetch(pos,float2( 1.0,off));
  168. float3 f=Fetch(pos,float2( 2.0,off));
  169. float3 g=Fetch(pos,float2( 3.0,off));
  170. float dst=Dist(pos).x;
  171. // Convert distance to weight.
  172. float scale=hardBloomPix;
  173. float wa=Gaus(dst-3.0,scale);
  174. float wb=Gaus(dst-2.0,scale);
  175. float wc=Gaus(dst-1.0,scale);
  176. float wd=Gaus(dst+0.0,scale);
  177. float we=Gaus(dst+1.0,scale);
  178. float wf=Gaus(dst+2.0,scale);
  179. float wg=Gaus(dst+3.0,scale);
  180. // Return filtered sample.
  181. return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);}
  182.  
  183. // Return scanline weight.
  184. float Scan(float2 pos,float off){
  185. float dst=Dist(pos).y;
  186. return Gaus(dst+off,hardScan);}
  187.  
  188. // Return scanline weight for bloom.
  189. float BloomScan(float2 pos,float off){
  190. float dst=Dist(pos).y;
  191. return Gaus(dst+off,hardBloomScan);}
  192.  
  193. // Allow nearest three lines to effect pixel.
  194. float3 Tri(float2 pos){
  195. float3 a=Horz3(pos,-1.0);
  196. float3 b=Horz5(pos, 0.0);
  197. float3 c=Horz3(pos, 1.0);
  198. float wa=Scan(pos,-1.0);
  199. float wb=Scan(pos, 0.0);
  200. float wc=Scan(pos, 1.0);
  201. return a*wa+b*wb+c*wc;}
  202.  
  203. // Small bloom.
  204. float3 Bloom(float2 pos){
  205. float3 a=Horz5(pos,-2.0);
  206. float3 b=Horz7(pos,-1.0);
  207. float3 c=Horz7(pos, 0.0);
  208. float3 d=Horz7(pos, 1.0);
  209. float3 e=Horz5(pos, 2.0);
  210. float wa=BloomScan(pos,-2.0);
  211. float wb=BloomScan(pos,-1.0);
  212. float wc=BloomScan(pos, 0.0);
  213. float wd=BloomScan(pos, 1.0);
  214. float we=BloomScan(pos, 2.0);
  215. return a*wa+b*wb+c*wc+d*wd+e*we;}
  216.  
  217. // Distortion of scanlines, and end of screen alpha.
  218. float2 Warp(float2 pos){
  219. pos=pos*2.0-1.0;
  220. pos*=float2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
  221. return pos*0.5+0.5;}
  222.  
  223. // Very compressed TV style shadow mask.
  224. float3 Mask1(float2 pos){
  225. float line=maskLight;
  226. float odd=0.0;
  227. if(frac(pos.x/6.0)<0.5)odd=1.0;
  228. if(frac((pos.y+odd)/2.0)<0.5)line=maskDark;
  229. pos.x=frac(pos.x/3.0);
  230. float3 mask1=float3(maskDark,maskDark,maskDark);
  231. if(pos.x<0.333)mask1.r=maskLight;
  232. else if(pos.x<0.666)mask1.g=maskLight;
  233. else mask1.b=maskLight;
  234. mask1*=line;
  235. return mask1;}
  236.  
  237. // Aperture-grille.
  238. float3 Mask2(float2 pos){
  239. pos.x=frac(pos.x/3.0);
  240. float3 mask2=float3(maskDark,maskDark,maskDark);
  241. if(pos.x<0.333)mask2.r=maskLight;
  242. else if(pos.x<0.666)mask2.g=maskLight;
  243. else mask2.b=maskLight;
  244. return mask2;}
  245.  
  246. // Stretched VGA style shadow mask (same as prior shaders).
  247. float3 Mask3(float2 pos){
  248. pos.x+=pos.y*3.0;
  249. float3 mask3=float3(maskDark,maskDark,maskDark);
  250. pos.x=frac(pos.x/6.0);
  251. if(pos.x<0.333)mask3.r=maskLight;
  252. else if(pos.x<0.666)mask3.g=maskLight;
  253. else mask3.b=maskLight;
  254. return mask3;}
  255.  
  256. // VGA style shadow mask.
  257. float3 Mask4(float2 pos){
  258. pos.xy=floor(pos.xy*float2(1.0,0.5));
  259. pos.x+=pos.y*3.0;
  260. float3 mask4=float3(maskDark,maskDark,maskDark);
  261. pos.x=frac(pos.x/6.0);
  262. if(pos.x<0.333)mask4.r=maskLight;
  263. else if(pos.x<0.666)mask4.g=maskLight;
  264. else mask4.b=maskLight;
  265. return mask4;}
  266.  
  267. float4 main_fragment (float2 tex : TEXCOORD, uniform sampler2D s0 : TEXUNIT0, uniform input IN) : COLOR
  268. {
  269. float2 pos=Warp(tex.xy*(IN.texture_size.xy/IN.video_size.xy))*(IN.video_size.xy/IN.texture_size.xy);
  270. s0_global = s0;
  271. IN_global = IN;
  272. float3 outColor = Tri(pos);
  273.  
  274. //Add Bloom
  275. outColor.rgb+=Bloom(pos)*bloomAmount;
  276.  
  277. if(shadowMask)
  278. outColor.rgb*=Mask1(floor(tex.xy*(IN.texture_size.xy/IN.video_size.xy)*IN.output_size.xy)+float2(0.5,0.5));
  279.  
  280. return float4(ToSrgb(outColor.rgb),1.0);
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement