Advertisement
Guest User

Bloom shader

a guest
Aug 10th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1.  
  2. float BloomThreshold;
  3. float BloomIntensity;
  4. float BaseIntensity;
  5.  
  6. float BloomSaturation;
  7. float BaseSaturation;
  8.  
  9. float sceneX, sceneY;
  10.  
  11. texture procScene;
  12. texture blur;
  13.  
  14. sampler scene = sampler_state
  15. {
  16.     texture = <procScene>;
  17. };
  18.  
  19. sampler TextureSampler2 = sampler_state
  20. {
  21.     texture = <blur>;
  22. };
  23.  
  24.  
  25. #define SAMPLE_COUNT 15
  26.  
  27. float2 SampleOffsetsX[SAMPLE_COUNT];
  28. float2 SampleOffsetsY[SAMPLE_COUNT];
  29. float SampleWeights[SAMPLE_COUNT];
  30.  
  31.  
  32. float4 AdjustSaturation(float4 color, float saturation)
  33. {
  34.    
  35.     float grey = dot(color, float3(0.3, 0.59, 0.2));
  36.  
  37.     return lerp(grey, color, saturation);
  38. }
  39.  
  40.  
  41. float4 Extract(float2 texCoord : TEXCOORD0) : COLOR0
  42. {
  43.     // Look up the original image color.
  44.     float4 c = tex2D(scene, texCoord);
  45.  
  46.     // Adjust it to keep only values brighter than the specified threshold.
  47.    
  48.     return saturate((c - BloomThreshold) / (1 - BloomThreshold));
  49. }
  50.  
  51. float4 BlurHorz(float2 texCoord : TEXCOORD0) : COLOR0
  52. {
  53.     float4 c = 0;
  54.    
  55.     for (int i = 0; i < SAMPLE_COUNT; i++)
  56.     {
  57.         c += tex2D(TextureSampler2, texCoord + SampleOffsetsX[i]) * SampleWeights[i];
  58.     }
  59.    
  60.     return c;
  61. }
  62.  
  63. float4 BlurVert(float2 texCoord : TEXCOORD0) : COLOR0
  64. {
  65.     float4 c = 0;
  66.    
  67.     for (int i = 0; i < SAMPLE_COUNT; i++)
  68.     {
  69.         c += tex2D(TextureSampler2, texCoord + SampleOffsetsY[i]) * SampleWeights[i];
  70.     }
  71.    
  72.     return c;
  73. }
  74.  
  75. float4 Combine (float2 texCoord : TEXCOORD0) : COLOR0
  76. {
  77.    
  78.     float4 bloom = tex2D(TextureSampler2, texCoord);
  79.     float4 base = tex2D(scene, texCoord);
  80.    
  81.     bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
  82.     base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
  83.    
  84.     base *= (1 - saturate(bloom));
  85.    
  86.     return base + bloom;
  87. }
  88.  
  89.  
  90. technique Bloom
  91. {
  92.     pass Pass1
  93.     {  
  94.         PixelShader = compile ps_2_0 Extract();
  95.     }
  96.     pass Pass2
  97.    
  98.     {
  99.         PixelShader = compile ps_2_0 BlurHorz();
  100.     }
  101.     pass Pass3
  102.    
  103.     {
  104.         PixelShader = compile ps_2_0 BlurVert();
  105.     }
  106.     pass Pass4
  107.    
  108.     {
  109.         PixelShader = compile ps_2_0 Combine();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement