SwarmParticle.fx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //-----------------------------------------------------------------------------
  2. // ©2006 Electronic Arts Inc
  3. //-----------------------------------------------------------------------------
  4. #define USE_INTERACTIVE_LIGHTS 1
  5. #define SUPPORT_FOG 1
  6. #define SUPPORT_GLOBAL_LIGHTS 1
  7. #include "Common.fxh"
  8. // ----------------------------------------------------------------------------
  9. // Phase Offset
  10. // ----------------------------------------------------------------------------
  11. float3 PhaseOffset
  12. <
  13. string UIWidget = "None";
  14. string SasBindAddress = "Particle.Draw.PhaseOffset";
  15. > = float3( 0.0, 0.0, 0.0 );
  16. // ----------------------------------------------------------------------------
  17. // Phase Opacity
  18. // ----------------------------------------------------------------------------
  19. float PhaseOpacity
  20. <
  21. string UIWidget = "None";
  22. string SasBindAddress = "Particle.Draw.PhaseOpacity";
  23. > = 1.0;
  24. // ----------------------------------------------------------------------------
  25. // Transforms
  26. // ----------------------------------------------------------------------------
  27. float4x4 View : View;
  28. float4x3 ViewInverse : ViewInverse;
  29. float4x4 WorldViewProjection : WorldViewProjection;
  30. // ----------------------------------------------------------------------------
  31. // Fake motion blur opacity values
  32. // ----------------------------------------------------------------------------
  33. float OpaqueSpeed
  34. <
  35. string UIName = "Opaque Speed";
  36. float UIMin = 0.0f;
  37. float UIMax = 10000.0f;
  38. > = 0.0f;
  39. float TransparentSpeed
  40. <
  41. string UIName = "Transparent Speed";
  42. float UIMin = 0.0f;
  43. float UIMax = 10000.0f;
  44. > = 100.0f;
  45. // ----------------------------------------------------------------------------
  46. // Speed Stretch Amount
  47. // ----------------------------------------------------------------------------
  48. float SpeedStretchAmount
  49. <
  50. string UIName = "Speed Stretch Amount";
  51. float UIMin = 0.0f;
  52. float UIMax = 10000.0f;
  53. > = 1.0f;
  54. // ----------------------------------------------------------------------------
  55. // Diffuse Texture
  56. // ----------------------------------------------------------------------------
  57. SAMPLER_2D_BEGIN( DiffuseTexture,
  58. string UIWidget = "None";
  59. string SasBindAddress = "Particle.Draw.Texture";
  60. )
  61. MinFilter = Linear;
  62. MagFilter = Linear;
  63. MipFilter = Linear;
  64. AddressU = Wrap;
  65. AddressV = Wrap;
  66. SAMPLER_2D_END
  67. // ----------------------------------------------------------------------------
  68. // Environment Texture
  69. // ----------------------------------------------------------------------------
  70. SAMPLER_2D_BEGIN( EnvironmentTexture,
  71. string UIWidget = "None";
  72. string SasBindAddress = "Particle.Draw.DetailTexture";
  73. )
  74. MinFilter = Linear;
  75. MagFilter = Linear;
  76. MipFilter = Linear;
  77. AddressU = Wrap;
  78. AddressV = Wrap;
  79. SAMPLER_2D_END
  80. // ----------------------------------------------------------------------------
  81. // Shroud
  82. // ----------------------------------------------------------------------------
  83. ShroudSetup Shroud
  84. <
  85. string UIWidget = "None";
  86. string SasBindAddress = "Terrain.Shroud";
  87. > = DEFAULT_SHROUD;
  88. SAMPLER_2D_BEGIN( ShroudTexture,
  89. string UIWidget = "None";
  90. string SasBindAddress = "Terrain.Shroud.Texture";
  91. string ResourceName = "ShaderPreviewShroud.dds";
  92. )
  93. MinFilter = Linear;
  94. MagFilter = Linear;
  95. MipFilter = Linear;
  96. AddressU = Clamp;
  97. AddressV = Clamp;
  98. SAMPLER_2D_END
  99. //-----------------------------------------------------------------------------
  100. // Vertex Shader structure
  101. //-----------------------------------------------------------------------------
  102. struct VSInput
  103. {
  104. float3 Position : POSITION;
  105. float2 DiffuseTexCoord : TEXCOORD0;
  106. float3 Velocity : TEXCOORD1;
  107. };
  108. struct VSOutput
  109. {
  110. float4 Position : POSITION;
  111. float4 Color : COLOR0;
  112. float4 LightColor_Fog : COLOR1;
  113. float2 DiffuseTexCoord : TEXCOORD1;
  114. float2 EnvironmentTexCoord : TEXCOORD2;
  115. float2 ShroudTexCoord : TEXCOORD3;
  116. };
  117. //-----------------------------------------------------------------------------
  118. // Vertex Shader
  119. //-----------------------------------------------------------------------------
  120. VSOutput VS( VSInput Input )
  121. {
  122. VSOutput Output;
  123. // Get the center of the particle, in view space
  124. float4 view_pos_center = mul( float4( Input.Position, 1), View );
  125. view_pos_center.w = 1;
  126. // Get the velocity, in view space
  127. float4 view_velocity_offset = float4( Input.Position + Input.Velocity, 1 );
  128. view_velocity_offset = mul( view_velocity_offset, View );
  129. float3 view_velocity = view_velocity_offset.xyz - view_pos_center.xyz;
  130. float speed = length( Input.Velocity );
  131. // Offset the vertex from the center based on it's size and velocity
  132. // We use the normalized UV space(0->1) to determine which corner
  133. // of the particle we're computing
  134. float3 velocity_vec_y = normalize( view_velocity );
  135. float3 velocity_vec_x = normalize( cross( velocity_vec_y, view_pos_center ) );
  136. float2 normalized_offset = ( Input.DiffuseTexCoord * float2( 2, 2 ) ) - float2( 1, 1 );
  137. float half_size = 0.5; // Size is currently set to 1 for all Swarm particle systems
  138. float4 vertex_view_pos = view_pos_center;
  139. vertex_view_pos.xyz += normalized_offset.x * half_size * velocity_vec_x;
  140. vertex_view_pos.xyz += ( half_size + ( speed * SpeedStretchAmount ) ) * ( normalized_offset.y * velocity_vec_y );
  141. vertex_view_pos.w = 1;
  142. // Fake motion blur by fading out the particle the faster it's moving
  143. float fade_interpolant = ( speed - OpaqueSpeed ) / ( TransparentSpeed - OpaqueSpeed );
  144. fade_interpolant = clamp( fade_interpolant, 0, 1 );
  145. float motion_blur_fade = 1 - fade_interpolant; //float motion_blur_fade = lerp( 1, 0, fade_interpolant );
  146. float opacity = motion_blur_fade * PhaseOpacity;
  147. // Get the final vertex position, in world space
  148. float3 world_position = mul( vertex_view_pos, ViewInverse );
  149. world_position += PhaseOffset;
  150. // Set basic outputs
  151. Output.Position = mul( float4( world_position, 1 ), WorldViewProjection );
  152. Output.LightColor_Fog.xyz = float3((DirectionalLight[0].Color + DirectionalLight[1].Color + DirectionalLight[2].Color)* .7);
  153. Output.Color = float4( 1, 1, 1, opacity );
  154. Output.DiffuseTexCoord = Input.DiffuseTexCoord;
  155. // Build normal for particle vertex
  156. static const float flattenNormal = .2; // 0 = totally flat, 1 = sphere normal
  157. float3 normal = float3((Input.DiffuseTexCoord - 0.5) * 2 * flattenNormal, 0);
  158. normal.z = sqrt(1.0 - normal.x * normal.x + normal.y * normal.y);
  159. float3x3 particleToViewSpace = float3x3(velocity_vec_x, velocity_vec_y, cross(velocity_vec_x, velocity_vec_y));
  160. float3 vertexWorldNormal = normalize(mul(normal, particleToViewSpace));
  161. Output.EnvironmentTexCoord = vertexWorldNormal.xy * 0.5 + 0.5;
  162. // Calculate Shroud UVs
  163. Output.ShroudTexCoord = CalculateShroudTexCoord( Shroud, world_position );
  164. // Calculate fog
  165. Output.LightColor_Fog.w = CalculateFog(Fog, world_position, ViewInverse[3]);
  166. return Output;
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Pixel Shader
  170. //-----------------------------------------------------------------------------
  171. float4 PS( VSOutput Input ) : Color
  172. {
  173. // Get the base color
  174. float4 diffuseColor = float4(.45,.9,5,1);
  175. float4 diffuse_texture = tex2D( SAMPLER(DiffuseTexture), Input.DiffuseTexCoord );
  176. float4 Color = diffuse_texture * Input.Color * diffuseColor * 2;
  177. // Apply environment texture
  178. float4 environment_texture = tex2D( SAMPLER(EnvironmentTexture), Input.EnvironmentTexCoord );
  179. // Color.xyz += environment_texture.xyz * Input.LightColor_Fog;
  180. //Apply fog
  181. float fogStrength = Input.LightColor_Fog.w;
  182. Color.xyz = lerp(Fog.Color, Color.xyz, fogStrength);
  183. // Apply shroud
  184. float shroud = tex2D( SAMPLER(ShroudTexture), Input.ShroudTexCoord ).x;
  185. shroud = BiasShroudValueForEffects( shroud );
  186. Color.xyz *= shroud;
  187. return Color;
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Techniques
  191. //-----------------------------------------------------------------------------
  192. technique Default
  193. {
  194. pass pass0
  195. {
  196. VertexShader = compile VS_2_0 VS();
  197. PixelShader = compile PS_2_0 PS();
  198. ZEnable = true;
  199. ZWriteEnable = false;
  200. ZFunc = ZFUNC_INFRONT;
  201. AlphaBlendEnable = true;
  202. CullMode = none;
  203. // SrcBlend = SrcAlpha;
  204. // DestBlend = InvSrcAlpha;
  205. SrcBlend = One;
  206. DestBlend = One;
  207. }
  208. }