Rain.fx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //-----------------------------------------------------------------------------
  2. // ©2006 Electronic Arts Inc
  3. //-----------------------------------------------------------------------------
  4. #include "Common.fxh"
  5. #include "Gamma.fxh"
  6. float4x4 World : World;
  7. float4x4 View : View;
  8. float4x3 ViewInverse : ViewInverse;
  9. float4x4 Projection : Projection;
  10. float Time : Time;
  11. // ----------------------------------------------------------------------------
  12. // Rain Box Size
  13. // ----------------------------------------------------------------------------
  14. float RainBoxHeight
  15. <
  16. string UIName = "Rain Box Width";
  17. float UIMin = 0.0;
  18. float UIMax = 10000.0;
  19. > = 200.0;
  20. // ----------------------------------------------------------------------------
  21. // Size
  22. // ----------------------------------------------------------------------------
  23. float MinWidth
  24. <
  25. string UIName = "Min Width";
  26. float UIMin = 0.0;
  27. float UIMax = 10000.0;
  28. > = 0.5;
  29. float MaxWidth
  30. <
  31. string UIName = "Max Width";
  32. float UIMin = 0.0;
  33. float UIMax = 10000.0;
  34. > = 1.5;
  35. float MinHeight
  36. <
  37. string UIName = "Min Height";
  38. float UIMin = 0.0;
  39. float UIMax = 10000.0;
  40. > = 5.0;
  41. float MaxHeight
  42. <
  43. string UIName = "Max Height";
  44. float UIMin = 0.0;
  45. float UIMax = 10000.0;
  46. > = 15.0;
  47. // ----------------------------------------------------------------------------
  48. // Speed
  49. // ----------------------------------------------------------------------------
  50. float MinSpeed
  51. <
  52. string UIName = "Min Speed";
  53. float UIMin = 0.0;
  54. float UIMax = 10000.0;
  55. > = 50.0;
  56. float MaxSpeed
  57. <
  58. string UIName = "Max Speed";
  59. float UIMin = 0.0;
  60. float UIMax = 10000.0;
  61. > = 150.0;
  62. // ----------------------------------------------------------------------------
  63. // Alpha
  64. // ----------------------------------------------------------------------------
  65. float MinAlpha
  66. <
  67. string UIName = "Min Alpha";
  68. float UIMin = 0.0;
  69. float UIMax = 1.0;
  70. > = 0.1;
  71. float MaxAlpha
  72. <
  73. string UIName = "Max Alpha";
  74. float UIMin = 0.0;
  75. float UIMax = 1.0;
  76. > = 0.5;
  77. // ----------------------------------------------------------------------------
  78. // Wind
  79. // ----------------------------------------------------------------------------
  80. float WindStrength
  81. <
  82. string UIName = "Wind Strength";
  83. float UIMin = 0.0;
  84. float UIMax = 10000.0;
  85. > = 1.0;
  86. // ----------------------------------------------------------------------------
  87. // Diffuse Texture
  88. // ----------------------------------------------------------------------------
  89. SAMPLER_2D_BEGIN( DiffuseTexture,
  90. string UIName = "Diffuse Texture";
  91. )
  92. MinFilter = Linear;
  93. MagFilter = Linear;
  94. MipFilter = Linear;
  95. AddressU = Wrap;
  96. AddressV = Wrap;
  97. SAMPLER_2D_END
  98. // ----------------------------------------------------------------------------
  99. // Shroud
  100. // ----------------------------------------------------------------------------
  101. ShroudSetup Shroud
  102. <
  103. string UIWidget = "None";
  104. string SasBindAddress = "Terrain.Shroud";
  105. > = DEFAULT_SHROUD;
  106. SAMPLER_2D_BEGIN( ShroudTexture,
  107. string UIWidget = "None";
  108. string SasBindAddress = "Terrain.Shroud.Texture";
  109. string ResourceName = "ShaderPreviewShroud.dds";
  110. )
  111. MinFilter = Linear;
  112. MagFilter = Linear;
  113. MipFilter = Linear;
  114. AddressU = Clamp;
  115. AddressV = Clamp;
  116. SAMPLER_2D_END
  117. //-----------------------------------------------------------------------------
  118. // Vertex Shader structure
  119. //-----------------------------------------------------------------------------
  120. struct VSInput
  121. {
  122. float3 Position : POSITION;
  123. float4 Width_Height_Speed_Alpha_Interpolants : COLOR0;
  124. float2 DiffuseTexCoord : TEXCOORD0;
  125. };
  126. struct VSOutput
  127. {
  128. float4 Position : POSITION;
  129. float4 Color : COLOR0;
  130. float2 DiffuseTexCoord : TEXCOORD0;
  131. float2 ShroudTexCoord : TEXCOORD1;
  132. };
  133. //-----------------------------------------------------------------------------
  134. // Vertex Shader
  135. //-----------------------------------------------------------------------------
  136. VSOutput VS( VSInput Input )
  137. {
  138. /*
  139. // These values are just here so you can override the values in XML for rapid tweaking
  140. RainBoxHeight = 200;
  141. MinWidth = 0.5;
  142. MaxWidth = 1.5;
  143. MinHeight = 5;
  144. MaxHeight = 15;
  145. MinSpeed = 50;
  146. MaxSpeed = 150;
  147. MinAlpha = 0.1;
  148. MaxAlpha = 0.5;
  149. */
  150. VSOutput Out;
  151. // Get the values for this particle based on it's random seed data
  152. float width_interpolant = Input.Width_Height_Speed_Alpha_Interpolants.x;
  153. float height_interpolant = Input.Width_Height_Speed_Alpha_Interpolants.y;
  154. float speed_interpolant = Input.Width_Height_Speed_Alpha_Interpolants.z;
  155. float alpha_interpolant = Input.Width_Height_Speed_Alpha_Interpolants.w;
  156. float width = lerp( MinWidth, MaxWidth, width_interpolant );
  157. float height = lerp( MinHeight, MaxHeight, height_interpolant );
  158. float speed = lerp( MinSpeed, MaxSpeed, speed_interpolant );
  159. float alpha = lerp( MinAlpha, MaxAlpha, alpha_interpolant );
  160. // Get the center of the particle, in world space
  161. float3 world_pos_center = mul( float4( Input.Position, 1 ), World ).xyz;
  162. world_pos_center.z -= speed * Time;
  163. world_pos_center.z = RainBoxHeight + fmod( world_pos_center.z, RainBoxHeight );
  164. // Offset the vertex vertically in world space
  165. float2 normalized_offset = Input.DiffuseTexCoord * - float2( 0.5, 0.5 );
  166. float vertical_offset = height * normalized_offset.y;
  167. world_pos_center.z += vertical_offset;
  168. // Transform the vertex into view space
  169. float3 vertex_view_pos = mul( float4( world_pos_center, 1 ), View ).xyz;
  170. // Offset the vertex horizontally in view space
  171. float horizontal_offset = width * normalized_offset.x;
  172. vertex_view_pos.x += horizontal_offset;
  173. // Get the final position
  174. Out.Position = mul( float4( vertex_view_pos, 1 ), Projection );
  175. // Transfer UVs
  176. Out.DiffuseTexCoord = Input.DiffuseTexCoord;
  177. // Color
  178. Out.Color = float4( 1, 1, 1, alpha );
  179. // Calculate Shroud UVs
  180. float3 world_position = mul( float4( vertex_view_pos, 1 ), ViewInverse ).xyz;
  181. Out.ShroudTexCoord = CalculateShroudTexCoord( Shroud, world_position );
  182. return Out;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Pixel Shader
  186. //-----------------------------------------------------------------------------
  187. float4 PS(VSOutput Input, uniform bool applyGammaCorrection) : Color
  188. {
  189. float4 color = Input.Color;
  190. // Apply Diffuse Texture
  191. float4 diffuseTexture = tex2D( SAMPLER(DiffuseTexture), Input.DiffuseTexCoord );
  192. if (applyGammaCorrection)
  193. {
  194. diffuseTexture.xyz = GammaToLinear(diffuseTexture.xyz);
  195. }
  196. color *= diffuseTexture;
  197. // Apply shroud
  198. float shroud = tex2D( SAMPLER(ShroudTexture), Input.ShroudTexCoord ).x;
  199. color.w *= shroud;
  200. return color;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Techniques
  204. //-----------------------------------------------------------------------------
  205. technique Default
  206. {
  207. pass pass0
  208. {
  209. VertexShader = compile VS_2_0 VS();
  210. PixelShader = compile PS_2_0 PS(true);
  211. ZEnable = false;
  212. ZWriteEnable = false;
  213. ZFunc = ZFUNC_INFRONT;
  214. AlphaBlendEnable = true;
  215. CullMode = none;
  216. SrcBlend = SrcAlpha;
  217. DestBlend = InvSrcAlpha;
  218. }
  219. }
  220. #if ENABLE_LOD
  221. technique Default_M
  222. {
  223. pass pass0
  224. {
  225. VertexShader = compile VS_2_0 VS();
  226. PixelShader = compile PS_2_0 PS(false);
  227. ZEnable = false;
  228. ZWriteEnable = false;
  229. ZFunc = ZFUNC_INFRONT;
  230. AlphaBlendEnable = true;
  231. CullMode = none;
  232. SrcBlend = SrcAlpha;
  233. DestBlend = InvSrcAlpha;
  234. }
  235. }
  236. #endif // if ENABLE_LOD