Advertisement
Guest User

Untitled

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