Advertisement
TorutheRedFox

rain_drop.fx

Jun 2nd, 2022
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. //
  2. // PC Rain Drop Effect
  3. //
  4. #include "global.h"
  5.  
  6. shared texture diffusemap : DiffuseMap;
  7. sampler DIFFUSEMAP_SAMPLER = sampler_state  // backbuffer for screen distortion
  8. {
  9.     Texture = <diffusemap>;
  10.     AddressU = CLAMP;
  11.     AddressV = CLAMP;
  12.     MipFilter = LINEAR;
  13.     MinFilter = LINEAR;
  14.     MagFilter = LINEAR;
  15. };
  16.  
  17. shared texture displacemap : DISPLACEMAP;
  18. sampler DISPLACEMAP_SAMPLER = sampler_state
  19. {
  20.     Texture = <displacemap>;
  21.     AddressU = WRAP;
  22.     AddressV = WRAP;
  23.     MipFilter = LINEAR;
  24.     MinFilter = LINEAR;
  25.     MagFilter = LINEAR;
  26. };
  27.  
  28. sampler DISPLACEMAP_SHADOW_SAMPLER = sampler_state
  29. {
  30.     Texture = <displacemap>;
  31.     AddressU = BORDER;
  32.     AddressV = BORDER;
  33.     BorderColor = 0x00000000;
  34.     MipFilter = LINEAR;
  35.     MinFilter = LINEAR;
  36.     MagFilter = LINEAR;
  37. };
  38.  
  39. shared float4 raindropoffset : RAINDROPOFFSET;
  40. shared float raindropalpha : RAINDROPALPHA;
  41.  
  42. struct VS_INPUT
  43. {
  44.     float4 position : POSITION;
  45.     float4 color    : COLOR;
  46.     float4 tex      : TEXCOORD;
  47.     float4 size     : TEXCOORD1;
  48.     //int4   light_index    : BLENDINDICES;
  49.     int4   light_index  : TEXCOORD2;
  50. };
  51.  
  52. ///////////////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Onscreen rain particle effect
  55. //
  56. struct VtoP_RAIN
  57. {
  58.     float4 position  : POSITION;
  59.     float4 tex       : TEXCOORD0;
  60. };
  61.  
  62. VtoP_RAIN vertex_shader_passthru(const VS_INPUT IN)
  63. {
  64.     VtoP_RAIN OUT;
  65.     OUT.position = screen_position(IN.position);
  66.     OUT.position.w = 1.0f;
  67.     OUT.tex = IN.tex;
  68.     OUT.tex.y = 1-IN.tex.y; // invert it because it's inverted for some reason
  69.  
  70.     return OUT;
  71. }
  72.  
  73. float2 scaleUV(float2 uv, float scale) {
  74.     return ((uv-0.5) / scale) + 0.5;
  75. }
  76.  
  77. float4 pixel_shader_onscreen_distort(const VtoP_RAIN IN) : COLOR0
  78. {  
  79.     // this is an abomination of a formula, but it works
  80.     float rdoffset = floor((((pow(abs(max(max(raindropoffset.r, raindropoffset.b), max(raindropoffset.g, raindropoffset.a))-0.5), 2.2))*5000) % 1.0f) * 4)/4;
  81.     float2 cvBaseAlphaRef = float2(raindropalpha, rdoffset);
  82.    
  83.     float4 distortion = tex2D(DISPLACEMAP_SAMPLER, IN.tex);// * float2(0.2f, 1.0f));
  84.     float2 offset = distortion.gb * raindropoffset.ba + raindropoffset.rg;
  85.     float4 background = tex2D(DIFFUSEMAP_SAMPLER, offset);
  86.    
  87.     // The opacity map has four different raindrop texture tiled horizontally.  The
  88.     // offset into this texure is stored in cvBaseAlphaRef.y
  89.     //
  90.     offset = IN.tex;
  91.     offset.x = (0.2 + ((cvBaseAlphaRef.y + IN.tex.x*0.25) * 0.8));
  92.     float4 opacity = float4(distortion.w, lerp(1, tex2D(DISPLACEMAP_SHADOW_SAMPLER, IN.tex + float2(0,2/32.f)).w, 0.5*distortion.w), 1, 1);//tex2D(DISPLACEMAP_SAMPLER, offset);
  93.    
  94.     float4 result;
  95.     result = background * opacity.y;
  96.     result.w = opacity.r * cvBaseAlphaRef.x;
  97.  
  98.     //result = opacity;
  99.    
  100.     return result;
  101. }
  102.  
  103.  
  104.  
  105. technique sampdisplace <int shader = 1;>
  106. {
  107.     pass p0
  108.     {
  109.         AlphaBlendEnable = TRUE; // has to be manually set here, thanks Black Box
  110.         SrcBlend     = 5; // D3DBLEND_SRCALPHA
  111.         DestBlend    = 6; // D3DBLEND_INVSRCALPHA
  112.         VertexShader = compile vs_1_1 vertex_shader_passthru();
  113.         PixelShader  = compile ps_2_0 pixel_shader_onscreen_distort();
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement