B2K24

Untitled

Jan 19th, 2015
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // Scanline & Shadowmask Effect
  3. //-----------------------------------------------------------------------------
  4.  
  5. texture DiffuseTexture;
  6.  
  7. sampler DiffuseSampler = sampler_state
  8. {
  9. Texture = <DiffuseTexture>;
  10. MipFilter = LINEAR;
  11. MinFilter = LINEAR;
  12. MagFilter = LINEAR;
  13. AddressU = CLAMP;
  14. AddressV = CLAMP;
  15. AddressW = CLAMP;
  16. };
  17.  
  18. texture ShadowTexture;
  19.  
  20. sampler ShadowSampler = sampler_state
  21. {
  22. Texture = <ShadowTexture>;
  23. MipFilter = LINEAR;
  24. MinFilter = LINEAR;
  25. MagFilter = LINEAR;
  26. AddressU = WRAP;
  27. AddressV = WRAP;
  28. AddressW = WRAP;
  29. };
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Vertex Definitions
  33. //-----------------------------------------------------------------------------
  34.  
  35. struct VS_OUTPUT
  36. {
  37. float4 Position : POSITION;
  38. float4 Color : COLOR0;
  39. float2 TexCoord : TEXCOORD0;
  40. };
  41.  
  42. struct VS_INPUT
  43. {
  44. float4 Position : POSITION;
  45. float4 Color : COLOR0;
  46. float2 TexCoord : TEXCOORD0;
  47. float2 Unused : TEXCOORD1;
  48. };
  49.  
  50. struct PS_INPUT
  51. {
  52. float4 Color : COLOR0;
  53. float2 TexCoord : TEXCOORD0;
  54. };
  55.  
  56. //-----------------------------------------------------------------------------
  57. // Scanline & Shadowmask Vertex Shader
  58. //-----------------------------------------------------------------------------
  59.  
  60. uniform float2 ScreenDims;
  61. uniform float2 SourceDims;
  62. uniform float2 SourceRect;
  63.  
  64. VS_OUTPUT vs_main(VS_INPUT Input)
  65. {
  66. VS_OUTPUT Output = (VS_OUTPUT)0;
  67.  
  68. Output.Position = float4(Input.Position.xyz, 1.0f);
  69. Output.Position.xy /= ScreenDims;
  70. Output.Position.y = 1.0f - Output.Position.y;
  71. Output.Position.xy -= 0.5f;
  72. Output.Position *= float4(2.0f, 2.0f, 1.0f, 1.0f);
  73. Output.Color = Input.Color;
  74. Output.TexCoord = Input.TexCoord + 0.5f / SourceDims;
  75.  
  76. return Output;
  77. }
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Post-Processing Pixel Shader
  81. //-----------------------------------------------------------------------------
  82.  
  83. uniform float PI = 3.14159265f;
  84. uniform float Epsilon = 0.00000001f;
  85.  
  86. uniform float CurvatureAmount = 0.0f;
  87. uniform float PincushionAmount = 0.0f;
  88.  
  89. uniform float ScanlineAlpha = 1.0f;
  90. uniform float ScanlineScale = 1.0f;
  91. uniform float ScanlineBrightScale = 1.0f;
  92. // uniform float ScanlineBrightOffset = 1.0f;
  93. uniform float ScanlineOffset = 1.0f;
  94. uniform float ScanlineHeight = 0.5f;
  95.  
  96. uniform float ShadowAlpha = 0.0f;
  97. uniform float2 ShadowCount = float2(320.0f, 240.0f);
  98. uniform float2 ShadowUV = float2(0.375f, 0.375f);
  99. uniform float2 ShadowDims = float2(8.0f, 8.0f);
  100.  
  101. uniform float3 Power = float3(1.0f, 1.0f, 1.0f);
  102. uniform float3 Floor = float3(0.0f, 0.0f, 0.0f);
  103.  
  104. static const float MagicConst = 0.5f;
  105.  
  106. float4 ps_main(PS_INPUT Input) : COLOR
  107. {
  108. float2 UsedArea = 1.0f / SourceRect;
  109. float2 HalfRect = SourceRect * 0.5f;
  110. float2 R2 = 1.0f / pow(length(UsedArea), 2.0f);
  111.  
  112. // Warning: Magic constant
  113. float2 CoordCorrection0 = float2(UsedArea.x * (MagicConst) / SourceDims.x, UsedArea.y * (MagicConst) / SourceDims.y);
  114. float2 CoordCorrection1 = float2(UsedArea.x * (1.0f + MagicConst) / SourceDims.x, UsedArea.y * (1.0f + MagicConst) / SourceDims.y);
  115.  
  116. // Reduce Pincushion Amount
  117. float CurvatureAmountReduced = CurvatureAmount * 0.25f;
  118.  
  119. // -- Screen Pincushion Calculation --
  120. float2 PinUnitCoord = Input.TexCoord * UsedArea * 2.0f - 1.0f;
  121. float CurvatureR2 = pow(length(PinUnitCoord), 2.0f) * R2;
  122. float2 CurvatureCurve = PinUnitCoord * CurvatureAmountReduced * CurvatureR2;
  123. float2 BaseCoord = Input.TexCoord;
  124. float2 ScanCoord = BaseCoord - 0.5f / ScreenDims;
  125.  
  126. BaseCoord -= HalfRect;
  127. BaseCoord *= 1.0f - CurvatureAmountReduced * UsedArea * 0.2f; // Warning: Magic constant
  128. BaseCoord += HalfRect;
  129. BaseCoord += CurvatureCurve;
  130.  
  131. ScanCoord -= HalfRect;
  132. ScanCoord *= 1.0f - CurvatureAmountReduced * UsedArea * 0.2f; // Warning: Magic constant
  133. ScanCoord += HalfRect;
  134. ScanCoord += CurvatureCurve;
  135.  
  136. float4 BaseTexel = tex2D(DiffuseSampler, BaseCoord);
  137.  
  138. // BaseTexel.r = 0.5f; // debug
  139. // BaseTexel.g = 0.5f; // debug
  140. // BaseTexel.b = 0.5f; // debug
  141. // BaseTexel.a = 1.00f; // debug
  142.  
  143. // -- Alpha Clipping (1px border in drawd3d does not work for some reason) --
  144. clip((BaseCoord < 1.0f / SourceDims) ? -1 : 1);
  145. clip((BaseCoord > (SourceRect + 1.0f / SourceDims)) ? -1 : 1);
  146.  
  147. // -- Scanline Simulation --
  148. float InnerSine = ScanCoord.y * SourceDims.y * ScanlineScale;
  149. float ScanBrightMod = sin(InnerSine * PI + ScanlineOffset * SourceDims.y);
  150. float3 ScanBrightness = lerp(1.0f, (pow(ScanBrightMod * ScanBrightMod, ScanlineHeight) * ScanlineBrightScale + 1.0f) * 0.5f, ScanlineAlpha);
  151. float3 Scanned = BaseTexel.rgb * ScanBrightness;
  152.  
  153. // -- Color Compression (increasing the floor of the signal without affecting the ceiling) --
  154. Scanned = Floor + (1.0f - Floor) * Scanned;
  155.  
  156. float2 ShadowMaskSize = float2(
  157. ShadowCount.x < 0.0f ? SourceDims.x * SourceRect.x / ShadowCount.x * -1.0f : ShadowCount.x,
  158. ShadowCount.y < 0.0f ? SourceDims.y * SourceRect.y / ShadowCount.y * -1.0f : ShadowCount.y);
  159. float2 ShadowFrac = frac(BaseCoord * ShadowMaskSize * UsedArea);
  160. float2 ShadowCoord = ShadowFrac * ShadowUV + 1.5f / ShadowDims;
  161. float3 ShadowTexel = lerp(1.0f, tex2D(ShadowSampler, ShadowCoord).rgb, ShadowAlpha);
  162.  
  163. // -- Final Pixel --
  164. float4 Output = float4(Scanned * ShadowTexel, BaseTexel.a) * Input.Color;
  165.  
  166. Output.r = pow(Output.r, Power.r);
  167. Output.g = pow(Output.g, Power.g);
  168. Output.b = pow(Output.b, Power.b);
  169. Output.a = 1.0f;
  170.  
  171. // Output.r = 0.25f; // debug
  172. // Output.g = 0.25f; // debug
  173. // Output.b = 0.25f; // debug
  174.  
  175. float2 DefaultUsedArea = float2(10.0f / 7.0f, 1.0f);
  176. // float2 DefaultUsedArea = float2(4.0f / 3.0f, 1.0f);
  177.  
  178. float2 CurvatureCoord = Input.TexCoord;
  179. CurvatureCoord -= HalfRect;
  180. CurvatureCoord *= 1.0f - CurvatureAmountReduced * UsedArea * MagicConst; // Warning: Magic constant
  181. // CurvatureCoord += HalfRect;
  182. CurvatureCoord += CurvatureCurve;
  183.  
  184. // CurvatureCoord *= UsedArea * DefaultUsedArea;
  185. // CurvatureCoord *= UsedArea * DefaultUsedArea * 1.5f;
  186. CurvatureCoord *= UsedArea * DefaultUsedArea * 1.25f;
  187.  
  188. // -- Vignetting --
  189. float2 VignetteCoord = CurvatureCoord;
  190.  
  191. float VignetteAmount = 1.0f - saturate(dot(VignetteCoord, VignetteCoord));
  192. float VignetteDarkness = pow(1.0f - PincushionAmount, 2.0f);
  193. float VignetteRadius = PincushionAmount * 2.0f + 1.0f;
  194. float Vignette = pow(VignetteAmount, VignetteRadius) + VignetteDarkness;
  195.  
  196. Output.rgb *= saturate(Vignette);
  197.  
  198. // Output.r = 0.25f; // debug
  199. // Output.g = 0.25f; // debug
  200. // Output.b = 0.25f; // debug
  201.  
  202. // -- Light Spot --
  203. float2 SpotCoord = CurvatureCoord;
  204. SpotCoord += float2(-0.25f, 0.25f) * DefaultUsedArea;
  205.  
  206. // float SpotAmount = 1.0f - saturate(pow(dot(SpotCoord, SpotCoord), 0.5f));
  207. // float SpotRadius = CurvatureAmount * 2.0f + 4.0f;
  208. // float Spot = pow(SpotAmount, SpotRadius) * 0.25f;
  209. float SpotAmount = 1.0f - saturate(pow(dot(SpotCoord, SpotCoord), 0.25f));
  210. float SpotRadius = CurvatureAmount * 4.0f + 2.0f;
  211. float Spot = pow(SpotAmount, SpotRadius) * 0.5f;
  212.  
  213. // -- Light Reflection --
  214. float2 ReflectionCoord = BaseCoord * UsedArea;
  215.  
  216. ReflectionCoord.x = 1.0f + CoordCorrection1.x - ReflectionCoord.x;
  217. ReflectionCoord.y = 0.0f + CoordCorrection0.y + ReflectionCoord.y;
  218.  
  219. float ReflectionAmount = pow(ReflectionCoord.x * ReflectionCoord.y, 0.25f);
  220. float Reflection = ReflectionAmount * 0.125f;
  221.  
  222. Output.rgb += (saturate(Reflection) + saturate(Spot)) * CurvatureAmount;
  223.  
  224. // -- Round Corners --
  225. float2 CornerCoords = BaseCoord * UsedArea;
  226.  
  227. float radius = 0.0f;
  228.  
  229. if (CurvatureAmount > 0.0f)
  230. {
  231. radius = pow(CurvatureAmount, 0.125f) * 0.125f;
  232. }
  233.  
  234. float threshold = pow(radius, 4.0f);
  235.  
  236. if (
  237. (CornerCoords.x - CoordCorrection0.x) * (CornerCoords.y - CoordCorrection0.y) <= threshold ||
  238. (1.0f + CoordCorrection1.x - CornerCoords.x) * (CornerCoords.y - CoordCorrection0.y) <= threshold ||
  239. (CornerCoords.x - CoordCorrection0.x) * (1.0f + CoordCorrection1.y - CornerCoords.y) <= threshold ||
  240. (1.0f + CoordCorrection1.x - CornerCoords.x) * (1.0f + CoordCorrection1.y - CornerCoords.y) <= threshold
  241. )
  242. {
  243. Output.rgb = float3(0.0f, 0.0f, 0.0f);
  244. }
  245.  
  246. return Output;
  247. }
  248.  
  249. //-----------------------------------------------------------------------------
  250. // Scanline & Shadowmask Effect
  251. //-----------------------------------------------------------------------------
  252.  
  253. technique ScanMaskTechnique
  254. {
  255. pass Pass0
  256. {
  257. Lighting = FALSE;
  258.  
  259. //Sampler[0] = <DiffuseSampler>;
  260.  
  261. VertexShader = compile vs_3_0 vs_main();
  262. PixelShader = compile ps_3_0 ps_main();
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment