Search Unity

Convert Menu Shader

Discussion in 'Shaders' started by WebWolfRussian, Apr 12, 2010.

  1. WebWolfRussian

    WebWolfRussian

    Joined:
    Feb 5, 2010
    Posts:
    123
    This is shader from XNA Rasing Game (Menu). Maybe somebody will convert in Unity format?
    See this video http://www.youtube.com/watch?v=TgChURF5fQE
    Code (csharp):
    1. // Project: Rocket Commander, File: PostScreenGlow.fx
    2. // Creation date: 10.11.2005 06:56
    3. // Last modified: 10.11.2005 09:27
    4. // Author: Benjamin Nitschke (abi@exdream.com) (c) 2005
    5. // Note: To test this use FX Composer from NVIDIA!
    6.  
    7. string description = "Post screen shader for the menu in RacingGame with glow and various screen effects";
    8.  
    9. // Glow/bloom with menu effects, adjusted for RacingGameManager.
    10. // Based on PostScreenGlow.fx
    11.  
    12. // This script is only used for FX Composer, most values here
    13. // are treated as constants by the application anyway.
    14. // Values starting with an upper letter are constants.
    15. float Script : STANDARDSGLOBAL
    16. <
    17.         //string UIWidget = "none";
    18.         string ScriptClass = "scene";
    19.         string ScriptOrder = "postprocess";
    20.         string ScriptOutput = "color";
    21.  
    22.         // We just call a script in the main technique.
    23.         //string Script = "Technique=ScreenGlow;";
    24.         string Script = "Technique=ScreenGlow20;";
    25. > = 0.5;
    26.  
    27. const float DownsampleMultiplicator = 0.25f;
    28. const float4 ClearColor : DIFFUSE = { 0.0f, 0.0f, 0.0f, 1.0f};
    29. const float ClearDepth = 1.0f;
    30.  
    31. float GlowIntensity <
    32.     string UIName = "Glow intensity";
    33.     string UIWidget = "slider";
    34.     float UIMin = 0.0f;
    35.     float UIMax = 1.0f;
    36.     float UIStep = 0.1f;
    37. > = 0.25f;//0.7f;//0.25f;//0.5f;//0.40f;
    38.  
    39. // Only used in ps_2_0
    40. float HighlightThreshold <
    41.     string UIName = "Highlight threshold";
    42.     string UIWidget = "slider";
    43.     float UIMin = 0.0f;
    44.     float UIMax = 1.0f;
    45.     float UIStep = 0.01f;
    46. > = 0.925f;//0.975f;//0.2f;//98f;
    47.  
    48. float HighlightIntensity <
    49.     string UIName = "Highlight intensity";
    50.     string UIWidget = "slider";
    51.     float UIMin = 0.0f;
    52.     float UIMax = 1.0f;
    53.     float UIStep = 0.01f;
    54. > = 0.145f;//0.4f;//0.25f;//1.0f;//0.9f;//0.75f;// 0.5f;
    55.  
    56. // Blur Width is used for ps_2_0 (and for ps_1_1 now too)!
    57. float BlurWidth <
    58.     string UIName = "Blur width";
    59.     string UIWidget = "slider";
    60.     float UIMin = 0.0f;
    61.     float UIMax = 10.0f;
    62.     float UIStep = 0.5f;
    63. > = 4.0f;//8.0f;//4.0f;
    64.  
    65. // Render-to-Texture stuff
    66. float2 windowSize : VIEWPORTPIXELSIZE;
    67. const float downsampleScale = 0.25;
    68. float Timer : TIME <string UIWidget="none";>;
    69. float Speed = 0.0032f;
    70. float Speed2 = 0.0016f;
    71. float ScratchIntensity = 0.605f;
    72. float IS = 0.031f;
    73.  
    74. texture sceneMap : RENDERCOLORTARGET
    75. <
    76.     float2 ViewportRatio = { 1.0, 1.0 };
    77.     int MIPLEVELS = 1;
    78. >;
    79. sampler sceneMapSampler = sampler_state
    80. {
    81.     texture = <sceneMap>;
    82.     AddressU  = CLAMP;
    83.     AddressV  = CLAMP;
    84.     AddressW  = CLAMP;
    85.     MIPFILTER = NONE;
    86.     MINFILTER = LINEAR;
    87.     MAGFILTER = LINEAR;
    88. };
    89.  
    90. texture downsampleMap : RENDERCOLORTARGET
    91. <
    92.     float2 ViewportRatio = { 0.25, 0.25 };
    93.     int MIPLEVELS = 1;
    94. >;
    95. sampler downsampleMapSampler = sampler_state
    96. {
    97.     texture = <downsampleMap>;
    98.     AddressU  = CLAMP;        
    99.     AddressV  = CLAMP;
    100.     AddressW  = CLAMP;
    101.     MIPFILTER = NONE;
    102.     MINFILTER = LINEAR;
    103.     MAGFILTER = LINEAR;
    104. };
    105.  
    106. texture blurMap1 : RENDERCOLORTARGET
    107. <
    108.     float2 ViewportRatio = { 0.25, 0.25 };
    109.     int MIPLEVELS = 1;
    110. >;
    111. sampler blurMap1Sampler = sampler_state
    112. {
    113.     texture = <blurMap1>;
    114.     AddressU  = CLAMP;        
    115.     AddressV  = CLAMP;
    116.     AddressW  = CLAMP;
    117.     MIPFILTER = NONE;
    118.     MINFILTER = LINEAR;
    119.     MAGFILTER = LINEAR;
    120. };
    121.  
    122. texture blurMap2 : RENDERCOLORTARGET
    123. <
    124.     float2 ViewportRatio = { 0.25, 0.25 };
    125.     int MIPLEVELS = 1;
    126. >;
    127. sampler blurMap2Sampler = sampler_state
    128. {
    129.     texture = <blurMap2>;
    130.     AddressU  = CLAMP;        
    131.     AddressV  = CLAMP;
    132.     AddressW  = CLAMP;
    133.     MIPFILTER = NONE;
    134.     MINFILTER = LINEAR;
    135.     MAGFILTER = LINEAR;
    136. };
    137.  
    138. texture noiseMap : Diffuse
    139. <
    140.         string UIName = "Noise texture";
    141.         string ResourceName = "noise128x128.dds";
    142. >;
    143. sampler noiseMapSampler = sampler_state
    144. {
    145.     texture = <noiseMap>;
    146.     AddressU  = WRAP;        
    147.     AddressV  = WRAP;
    148.     AddressW  = WRAP;
    149.     MIPFILTER = LINEAR;
    150.     MINFILTER = LINEAR;
    151.     MAGFILTER = LINEAR;
    152. };
    153.  
    154. // Returns luminance value of col to convert color to grayscale
    155. float Luminance(float3 col)
    156. {
    157.         return dot(col, float3(0.3, 0.59, 0.11));
    158. } // Luminance(.)
    159.  
    160. struct VB_OutputPosTexCoord
    161. {
    162.         float4 pos      : POSITION;
    163.     float2 texCoord : TEXCOORD0;
    164. };
    165.  
    166. struct VB_OutputPos2TexCoords
    167. {
    168.         float4 pos         : POSITION;
    169.     float2 texCoord[2] : TEXCOORD0;
    170. };
    171.  
    172. struct VB_OutputPos3TexCoords
    173. {
    174.         float4 pos         : POSITION;
    175.     float2 texCoord[3] : TEXCOORD0;
    176. };
    177.  
    178. struct VB_OutputPos4TexCoords
    179. {
    180.         float4 pos         : POSITION;
    181.     float2 texCoord[4] : TEXCOORD0;
    182. };
    183.  
    184. struct VB_OutputPos3TexCoordsWithColor
    185. {
    186.         float4 pos         : POSITION;
    187.     float2 texCoord[3] : TEXCOORD0;
    188.     float color        : TEXCOORD3;
    189. };
    190.  
    191. float4 PS_Display(
    192.         VB_OutputPosTexCoord In,
    193.         uniform sampler2D tex) : COLOR
    194. {  
    195.         float4 outputColor = tex2D(tex, In.texCoord);
    196.         // Display color
    197.         return outputColor;
    198.         //return float4(1, 0, 0, 1);
    199. } // PS_Display(..)
    200.  
    201. float4 PS_DisplayAlpha(
    202.         VB_OutputPosTexCoord In,
    203.         uniform sampler2D tex) : COLOR
    204. {
    205.         float4 outputColor = tex2D(tex, In.texCoord);
    206.         // Just display alpha
    207.         return float4(outputColor.a, outputColor.a, outputColor.a, 0.0f);
    208. } // PS_DisplayAlpha(..)
    209.  
    210. /////////////////////////////
    211. // ps_1_1 shader functions //
    212. /////////////////////////////
    213.  
    214. // Generate texture coordinates to only 2 sample neighbours (can't do more in ps)
    215. VB_OutputPos2TexCoords VS_DownSample11(
    216.         float4 pos      : POSITION,
    217.         float2 texCoord : TEXCOORD0)
    218. {
    219.         VB_OutputPos2TexCoords Out = (VB_OutputPos2TexCoords)0;
    220.         float2 texelSize = DownsampleMultiplicator /
    221.                 (windowSize * downsampleScale);
    222.         float2 s = texCoord;
    223.         Out.pos = pos;
    224. #if 1
    225.         Out.texCoord[0] = s - float2(-1, -1)*texelSize;
    226.         Out.texCoord[1] = s - float2(+1, +1)*texelSize;
    227. #else
    228.         Out.texCoord[0] = s - float2(0, 0)*texelSize;
    229.         Out.texCoord[1] = s - float2(+2, +2)*texelSize;
    230. #endif
    231.         return Out;
    232. } // VS_DownSample11(..)
    233.  
    234. float4 PS_DownSample11(
    235.         VB_OutputPos2TexCoords In,
    236.         uniform sampler2D tex) : COLOR
    237. {
    238.         float4 c;
    239.  
    240.         // sub sampling (can't do more in ps_1_1)
    241.         c = tex2D(tex, In.texCoord[0])/2;
    242.         c += tex2D(tex, In.texCoord[1])/2;
    243.        
    244.         // store hilights in alpha, can't use smoothstep version!
    245.         // Fake it with highly optimized version using 80% as treshold.
    246.         float l = Luminance(c.rgb);
    247.         float treshold = 0.75f;
    248.         if (l < treshold)
    249.                 c.a = 0;
    250.         else
    251.         {
    252.                 l = l-treshold;
    253.                 l = l+l+l+l; // bring 0..0.25 back to 0..1
    254.                 c.a = l;
    255.         } // else
    256.  
    257.         return c;
    258. } // PS_DownSample11(..)
    259.  
    260. VB_OutputPos4TexCoords VS_SimpleBlur(
    261.         uniform float2 direction,
    262.         float4 pos      : POSITION,
    263.         float2 texCoord : TEXCOORD0)
    264. {
    265.         VB_OutputPos4TexCoords Out = (VB_OutputPos4TexCoords)0;
    266.         Out.pos = pos;
    267.         float2 texelSize = 1.0f / windowSize;
    268.  
    269.         Out.texCoord[0] = texCoord + texelSize*(float2(2.0f, 2.0f)+direction*(-3.0f));
    270.         Out.texCoord[1] = texCoord + texelSize*(float2(2.0f, 2.0f)+direction*(-1.0f));
    271.         Out.texCoord[2] = texCoord + texelSize*(float2(2.0f, 2.0f)+direction*(+1.0f));
    272.         Out.texCoord[3] = texCoord + texelSize*(float2(2.0f, 2.0f)+direction*(+3.0f));
    273.  
    274.         return Out;
    275. } // VS_SimpleBlur(..)
    276.  
    277. float4 PS_SimpleBlur(
    278.         VB_OutputPos4TexCoords In,
    279.         uniform sampler2D tex) : COLOR
    280. {
    281.         float4 OutputColor = 0;
    282.         OutputColor += tex2D(tex, In.texCoord[0])/4;
    283.         OutputColor += tex2D(tex, In.texCoord[1])/4;
    284.         OutputColor += tex2D(tex, In.texCoord[2])/4;
    285.         OutputColor += tex2D(tex, In.texCoord[3])/4;
    286.         return OutputColor;///4;
    287. } // PS_SimpleBlur(..)
    288.  
    289. VB_OutputPos2TexCoords VS_ScreenQuad(
    290.         float4 pos      : POSITION,
    291.         float2 texCoord : TEXCOORD0)
    292. {
    293.         VB_OutputPos2TexCoords Out;
    294.         float2 texelSize = 1.0 /
    295.                 (windowSize * downsampleScale);
    296.         Out.pos = pos;
    297.         // Don't use bilinear filtering
    298.         Out.texCoord[0] = texCoord + texelSize*0.5;
    299.         Out.texCoord[1] = texCoord + texelSize*0.5;
    300.         return Out;
    301. } // VS_ScreenQuad(..)
    302.  
    303. VB_OutputPos3TexCoords VS_ScreenQuadSampleUp(
    304.         float4 pos      : POSITION,
    305.         float2 texCoord : TEXCOORD0)
    306. {
    307.         VB_OutputPos3TexCoords Out;
    308.         float2 texelSize = 1.0 / windowSize;
    309.         Out.pos = pos;
    310.         // Don't use bilinear filtering
    311.         Out.texCoord[0] = texCoord + texelSize*0.5f;
    312.         Out.texCoord[1] = texCoord + texelSize*0.5f/downsampleScale;
    313.         Out.texCoord[2] = texCoord + (1.0/128.0f)*0.5f;
    314.         return Out;
    315. } // VS_ScreenQuadSampleUp(..)
    316.  
    317. static float flashPS11 = 1.0f;
    318. VB_OutputPos3TexCoordsWithColor VS_ComposeFinalImage11(
    319.         float4 pos      : POSITION,
    320.         float2 texCoord : TEXCOORD0)
    321. {
    322.         VB_OutputPos3TexCoordsWithColor Out;
    323.         float2 texelSize = 1.0 / windowSize;
    324.         Out.pos = pos;
    325.         // Scratch texture
    326.         half flash = 1.0;
    327.         if(frac(Timer/10)<0.1)
    328.                 flash = 2.0*(0.55+0.45*sin(Timer*3.14f*2));
    329.  
    330.         if (flash != 1.0f)
    331.                 texCoord.x += (flash-1.5f)/40.0f *
    332.                         cos(Timer+texCoord.y*1.8f);
    333.                
    334.         float Side = (Timer*Speed2);
    335.         float ScanLine = (Timer*Speed);
    336.        
    337.         // Don't use bilinear filtering
    338.         Out.texCoord[0] = texCoord + texelSize*0.5f;
    339.         Out.texCoord[1] = texCoord + texelSize*0.5f/downsampleScale;
    340.         Out.texCoord[2] = float2(texCoord.x/5+Side*2, ScanLine);
    341.         //Out.texCoord[2].x *= 0.75f*ScratchIntensity/IS;
    342.         Out.texCoord[2].x *= 4;
    343.         Out.color = flash-1;
    344.         flashPS11 = flash;
    345.        
    346.         return Out;
    347. } // VS_ScreenQuadSampleUp(..)
    348.  
    349. float4 PS_ComposeFinalImage11(
    350.         VB_OutputPos3TexCoordsWithColor In,
    351.         uniform sampler2D sceneSampler,
    352.         uniform sampler2D blurredSceneSampler) : COLOR
    353. {
    354.         float4 orig = tex2D(sceneSampler, In.texCoord[0]);
    355.         float4 blur = tex2D(blurredSceneSampler, In.texCoord[1]);
    356.         float4 scratch = tex2D(noiseMapSampler, In.texCoord[2]);//.x;
    357.         float flash = In.color;
    358.        
    359.         // Add scratches
    360.   orig += saturate(1-(2 * scratch)) * 0.2f;
    361.  
    362.         float4 ret =
    363.                 0.8f * orig + flash +
    364.                 0.5f * blur +
    365.                 HighlightIntensity*blur.a;
    366.                
    367.   return ret;
    368. } // PS_ComposeFinalImage(...)
    369.  
    370. float4 PS_ComposeFinalImage20(
    371.         VB_OutputPos3TexCoords In,
    372.         uniform sampler2D sceneSampler,
    373.         uniform sampler2D blurredSceneSampler) : COLOR
    374. {
    375.         half flash = 1.0;
    376.         if(frac(Timer/10)<0.075)
    377.                 flash = 2.0*(0.55+0.45*sin(Timer*3.14f*2));
    378.                
    379.         float2 texCoord = In.texCoord[0];
    380.         if (flash != 1.0f)
    381.                 texCoord.x += (flash-1.5f)/40.0f *
    382.                         cos(Timer*7+texCoord.y*25.18f);
    383.                
    384.         float4 orig = tex2D(sceneSampler, texCoord);
    385.         float4 blur = tex2D(blurredSceneSampler, In.texCoord[1]);
    386.         float Side = (Timer*Speed2);
    387.         float ScanLine = (Timer*Speed);
    388.         float2 s = float2(texCoord.x/5+Side,ScanLine);
    389.         float scratch = tex2D(noiseMapSampler,s).x;
    390.        
    391.         // Add scratches
    392.         scratch = 2.0f*(scratch - ScratchIntensity)/IS;
    393.         scratch = 1.0-abs(1.0f-scratch);
    394.         //scratch = scratch * 100.0f;
    395.         scratch = max(0,scratch) * 0.5f *
    396.                 (0.55f+0.45f*sin(Timer*4.0)); // /2.5f));
    397.     orig *= 1+float4(scratch.xxx,0);
    398.  
    399.         float4 ret =
    400.                 0.8f * orig +
    401.                 0.5f * blur +
    402.                 HighlightIntensity * blur.a;
    403.        
    404.   ret *= flash;
    405.  
    406.   // Change colors a bit, sub 20% red and add 25% blue (photoshop values)
    407.   // Here the values are -4% and +5%
    408.   ret.rgb = float3(
    409.     ret.r+0.054f,
    410.     ret.g-0.021f,
    411.     ret.b-0.035f+(flash-1)/3);
    412.        
    413.   // Change brightness -5% and contrast +10%
    414.   ret.rgb = ret.rgb * 0.95f;
    415.   ret.rgb = (ret.rgb - float3(0.5, 0.5, 0.5)) * 1.10f +
    416.     float3(0.5, 0.5, 0.5);
    417.  
    418.   return ret;
    419. } // PS_ComposeFinalImage(...)
    420.  
    421. // Bloom technique for ps_1_1 (not that powerful, but looks still gooood)
    422. technique ScreenGlow
    423. <
    424.         // Script stuff is just for FX Composer
    425.         string Script =
    426.                 "ClearSetDepth=ClearDepth;"
    427.                 "RenderColorTarget=sceneMap;"
    428.                 //never used anyway: "RenderDepthStencilTarget=DepthMap;"
    429.                 "ClearSetColor=ClearColor;"
    430.                 "ClearSetDepth=ClearDepth;"
    431.                 "Clear=Color;"
    432.                 "Clear=Depth;"
    433.                 "ScriptSignature=color;"
    434.                 "ScriptExternal=;"
    435.                 "Pass=DownSample;"
    436.                 "Pass=GlowBlur1;"
    437.                 "Pass=GlowBlur2;"
    438.                 "Pass=ComposeFinalScene;";
    439. >
    440. {
    441.         // Sample full render area down to (1/4, 1/4) of its size!
    442.         pass DownSample
    443.         <
    444.                 string Script =
    445.                         "RenderColorTarget0=downsampleMap;"
    446.                         "ClearSetColor=ClearColor;"
    447.                         "Clear=Color;"
    448.                         "Draw=Buffer;";
    449.         >
    450.         {
    451.                 // Disable alpha testing, else most pixels will be skipped
    452.                 // because of the highlight HDR technique tricks used here!
    453.                 AlphaTestEnable = false;
    454.                 //cullmode = none;
    455.                 //ZEnable = false;
    456.                 //ZWriteEnable = false;
    457.                 VertexShader = compile vs_1_1 VS_DownSample11();
    458.                 PixelShader  = compile ps_1_1 PS_DownSample11(sceneMapSampler);
    459.         } // pass DownSample
    460.  
    461.         // Blur everything to make the glow effect.
    462.         pass GlowBlur1
    463.         <
    464.                 string Script =
    465.                         "RenderColorTarget0=blurMap1;"
    466.                         "ClearSetColor=ClearColor;"
    467.                         "Clear=Color;"
    468.                         "Draw=Buffer;";
    469.         >
    470.         {
    471.                 //cullmode = none;
    472.                 //ZEnable = false;
    473.                 //ZWriteEnable = false;
    474.                 VertexShader = compile vs_1_1 VS_SimpleBlur(float2(2, 0));
    475.                 PixelShader  = compile ps_1_1 PS_SimpleBlur(downsampleMapSampler);
    476.         } // pass GlowBlur1
    477.  
    478.         pass GlowBlur2
    479.         <
    480.                 string Script =
    481.                         "RenderColorTarget0=blurMap2;"
    482.                         "ClearSetColor=ClearColor;"
    483.                         "Clear=Color;"
    484.                         "Draw=Buffer;";
    485.         >
    486.         {
    487.                 //cullmode = none;
    488.                 //ZEnable = false;
    489.                 //ZWriteEnable = false;
    490.                 VertexShader = compile vs_1_1 VS_SimpleBlur(float2(0, 2));
    491.                 PixelShader  = compile ps_1_1 PS_SimpleBlur(blurMap1Sampler);
    492.         } // pass GlowBlur2
    493.  
    494.         // And compose the final image with the Blurred Glow and the original image.
    495.         pass ComposeFinalScene
    496.         <
    497.                 string Script =
    498.                         "RenderColorTarget0=;"
    499.                         "Draw=Buffer;";        
    500.         >
    501.         {
    502.                 //cullmode = none;
    503.                 //ZEnable = false;
    504.                 //ZWriteEnable = false;
    505.                 // Save 1 pass by combining the radial blur effect and the compose pass.
    506.                 // This pass is not as fast as the previous passes (they were done
    507.                 // in 1/16 of the original screen size and executed very fast).
    508.                 VertexShader = compile vs_1_1 VS_ComposeFinalImage11();
    509.                 PixelShader  = compile ps_1_1 PS_ComposeFinalImage11(
    510.                         sceneMapSampler, blurMap2Sampler);
    511.         } // pass ComposeFinalScene
    512. } // technique ScreenGlow11
    513.  
    514. //////////////////
    515. // ps_2_0 stuff //
    516. //////////////////
    517.  
    518. // Works only on ps_2_0 and up
    519. struct VB_OutputPos8TexCoords
    520. {
    521.     float4 pos         : POSITION;
    522.     float2 texCoord[8] : TEXCOORD0;
    523. };
    524.  
    525. VB_OutputPos4TexCoords VS_DownSample20(
    526.         float4 pos : POSITION,
    527.         float2 texCoord : TEXCOORD0)
    528. {
    529.         VB_OutputPos4TexCoords Out;
    530.         float2 texelSize = DownsampleMultiplicator /
    531.                 (windowSize * downsampleScale);
    532.         float2 s = texCoord;
    533.         Out.pos = pos;
    534. #if 1
    535.         Out.texCoord[0] = s + float2(-1, -1)*texelSize;
    536.         Out.texCoord[1] = s + float2(+1, +1)*texelSize;
    537.         Out.texCoord[2] = s + float2(+1, -1)*texelSize;
    538.         Out.texCoord[3] = s + float2(+1, +1)*texelSize;
    539. #else
    540.         Out.texCoord[0] = s + float2(-2, -2)*texelSize;
    541.         Out.texCoord[1] = s + float2(+2, +2)*texelSize;
    542.         Out.texCoord[2] = s + float2(+2, -2)*texelSize;
    543.         Out.texCoord[3] = s + float2(+2, +2)*texelSize;
    544. #endif
    545.         return Out;
    546. } // VS_DownSample20(..)
    547.  
    548. float4 PS_DownSample20(
    549.         VB_OutputPos4TexCoords In,
    550.         uniform sampler2D tex) : COLOR
    551. {
    552.         float4 c;
    553.  
    554.         // box filter (only for ps_2_0)
    555.         c = tex2D(tex, In.texCoord[0])/4;
    556.         c += tex2D(tex, In.texCoord[1])/4;
    557.         c += tex2D(tex, In.texCoord[2])/4;
    558.         c += tex2D(tex, In.texCoord[3])/4;
    559.        
    560.         // store hilights in alpha, can't use smoothstep version!
    561.         // Fake it with highly optimized version using 80% as treshold.
    562.         float l = Luminance(c.rgb);
    563.         float treshold = 0.75f;
    564.         if (l < treshold)
    565.                 c.a = 0;
    566.         else
    567.         {
    568.                 l = l-treshold;
    569.                 l = l+l+l+l; // bring 0..0.25 back to 0..1
    570.                 c.a = l;
    571.         } // else
    572.  
    573.         return c;
    574. } // PS_DownSample20(..)
    575.  
    576. // Blur downsampled map
    577. VB_OutputPos8TexCoords VS_Blur20(
    578.         uniform float2 direction,
    579.         float4 pos : POSITION,
    580.         float2 texCoord : TEXCOORD0)
    581. {
    582.         VB_OutputPos8TexCoords Out = (VB_OutputPos8TexCoords)0;
    583.     Out.pos = pos;
    584.  
    585.         float2 texelSize = BlurWidth / windowSize;
    586.   float2 s = texCoord - texelSize*(7-1)*0.5*direction;
    587.   for (int i=0; i<7; i++)
    588.   {
    589.         Out.texCoord[i] = s + texelSize*i*direction;
    590.   } // for
    591.  
    592.         return Out;
    593. } // VS_Blur20(..)
    594.  
    595. // blur filter weights
    596. const half weights7[7] =
    597. {
    598.         0.05,
    599.         0.1,
    600.         0.2,
    601.         0.3,
    602.         0.2,
    603.         0.1,
    604.         0.05,
    605. };    
    606.  
    607. float4 PS_Blur20(
    608.         VB_OutputPos8TexCoords In,
    609.         uniform sampler2D tex) : COLOR
    610. {
    611.     float4 c = 0;
    612.    
    613.     // this loop will be unrolled by compiler
    614.     for(int i=0; i<7; i++)
    615.     {
    616.         c += tex2D(tex, In.texCoord[i]) * weights7[i];
    617.         }
    618.     return c;
    619. } // PS_Blur20(..)
    620.  
    621. // Same for ps_2_0, looks better and allows more control over the parameters.
    622. technique ScreenGlow20
    623. <
    624.         string Script =
    625.                 "ClearSetDepth=ClearDepth;"
    626.                 "RenderColorTarget=sceneMap;"
    627.                 //never used anyway: "RenderDepthStencilTarget=DepthMap;"
    628.                 "ClearSetColor=ClearColor;"
    629.                 "ClearSetDepth=ClearDepth;"
    630.                 "Clear=Color;"
    631.                 "Clear=Depth;"
    632.                 "ScriptSignature=color;"
    633.                 "ScriptExternal=;"
    634.                 "Pass=DownSample;"
    635.                 "Pass=GlowBlur1;"
    636.                 "Pass=GlowBlur2;"
    637.                 "Pass=ComposeFinalScene;";
    638. >
    639. {
    640.         // Sample full render area down to (1/4, 1/4) of its size!
    641.         pass DownSample
    642.         <
    643.                 string Script =
    644.                         "RenderColorTarget0=downsampleMap;"
    645.                         "ClearSetColor=ClearColor;"
    646.                         "Clear=Color;"
    647.                         "Draw=Buffer;";
    648.         >
    649.         {
    650.                 // Disable alpha testing, else most pixels will be skipped
    651.                 // because of the highlight HDR technique tricks used here!
    652.                 AlphaTestEnable = false;
    653.                 VertexShader = compile vs_1_1 VS_DownSample20();
    654.                 PixelShader  = compile ps_2_0 PS_DownSample20(sceneMapSampler);
    655.         } // pass DownSample
    656.  
    657.         pass GlowBlur1
    658.         <
    659.                 string Script =
    660.                         "RenderColorTarget0=blurMap1;"
    661.                         "ClearSetColor=ClearColor;"
    662.                         "Clear=Color;"
    663.                         "Draw=Buffer;";
    664.         >
    665.         {
    666.                 VertexShader = compile vs_2_0 VS_Blur20(float2(1, 0));
    667.                 PixelShader  = compile ps_2_0 PS_Blur20(downsampleMapSampler);
    668.         } // pass GlowBlur1
    669.  
    670.         pass GlowBlur2
    671.         <
    672.                 string Script =
    673.                         "RenderColorTarget0=blurMap2;"
    674.                         "ClearSetColor=ClearColor;"
    675.                         "Clear=Color;"
    676.                         "Draw=Buffer;";
    677.         >
    678.         {
    679.                 VertexShader = compile vs_2_0 VS_Blur20(float2(0, 1));
    680.                 PixelShader  = compile ps_2_0 PS_Blur20(blurMap1Sampler);
    681.         } // pass GlowBlur2
    682.  
    683.         // And compose the final image with the Blurred Glow and the original image.
    684.         pass ComposeFinalScene
    685.         <
    686.                 string Script =
    687.                         "RenderColorTarget0=;"
    688.                         "Draw=Buffer;";        
    689.         >
    690.         {
    691.                 // This pass is not as fast as the previous passes (they were done
    692.                 // in 1/16 of the original screen size and executed very fast).
    693.                 VertexShader = compile vs_1_1 VS_ScreenQuadSampleUp();
    694.                 PixelShader  = compile ps_2_0 PS_ComposeFinalImage20(
    695.                         sceneMapSampler, blurMap2Sampler);
    696.         } // pass ComposeFinalScene
    697. } // technique ScreenGlow20