Trail.fx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-----------------------------------------------------------------------------
  2. // ©2006 Electronic Arts Inc
  3. //-----------------------------------------------------------------------------
  4. #define SUPPORT_FOG 1
  5. #include "Common.fxh"
  6. #include "CommonParticle.fxh"
  7. #if defined(_WW3D_)
  8. #if !defined(USE_INDIRECT_CONSTANT)
  9. float4x4 ViewProjection
  10. <
  11. string UIWidget = "None";
  12. string SasBindAddress = "Sas.Camera.WorldToProjection";
  13. >;
  14. float3 EyePosition
  15. <
  16. string UIWidget = "None";
  17. string SasBindAddress = "Sas.Camera.Position";
  18. >;
  19. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  20. float4x4 GetViewProjection()
  21. {
  22. return ViewProjection;
  23. }
  24. float3 GetEyePosition()
  25. {
  26. return EyePosition;
  27. }
  28. #else // #if defined(_WW3D_)
  29. float4x4 Projection : Projection;
  30. float4x4 View : View;
  31. float4x3 ViewI : ViewInverse;
  32. float4x4 GetViewProjection()
  33. {
  34. return mul(View, Projection);
  35. }
  36. float3 GetEyePosition()
  37. {
  38. return ViewI[3];
  39. }
  40. #endif // #if defined(_WW3D_)
  41. // ----------------------------------------------------------------------------
  42. // Diffuse Texture
  43. // ----------------------------------------------------------------------------
  44. SAMPLER_2D_BEGIN( DiffuseTexture,
  45. string UIWidget = "None";
  46. string SasBindAddress = "Particle.Draw.Texture";
  47. )
  48. MinFilter = Linear;
  49. MagFilter = Linear;
  50. MipFilter = Linear;
  51. AddressU = Clamp;
  52. AddressV = Clamp;
  53. SAMPLER_2D_END
  54. // ----------------------------------------------------------------------------
  55. // Fog
  56. // ----------------------------------------------------------------------------
  57. // Variationgs for handling fog in the pixel shader
  58. static const int FogMode_Disabled = 0;
  59. static const int FogMode_Opaque = 1;
  60. static const int FogMode_Additive = 2;
  61. // ----------------------------------------------------------------------------
  62. // Shroud
  63. // ----------------------------------------------------------------------------
  64. ShroudSetup Shroud
  65. <
  66. string UIWidget = "None";
  67. string SasBindAddress = "Terrain.Shroud";
  68. > = DEFAULT_SHROUD;
  69. SAMPLER_2D_BEGIN( ShroudTexture,
  70. string UIWidget = "None";
  71. string SasBindAddress = "Terrain.Shroud.Texture";
  72. string ResourceName = "ShaderPreviewShroud.dds";
  73. )
  74. MinFilter = Linear;
  75. MagFilter = Linear;
  76. MipFilter = Linear;
  77. AddressU = Clamp;
  78. AddressV = Clamp;
  79. SAMPLER_2D_END
  80. //-----------------------------------------------------------------------------
  81. // Vertex Shader structure
  82. //-----------------------------------------------------------------------------
  83. struct VSInput
  84. {
  85. float3 Position : POSITION;
  86. float4 Color : COLOR0;
  87. float2 DiffuseTexCoord : TEXCOORD0;
  88. };
  89. struct VSOutput
  90. {
  91. float4 Position : POSITION;
  92. float4 Color : COLOR0;
  93. float3 Fog : COLOR1; // This is just a scalar, but PS1.1 can't replicate-swizzle, so replicate scalar into a vector in vertex shader
  94. float2 DiffuseTexCoord : TEXCOORD0;
  95. float2 ShroudTexCoord : TEXCOORD1;
  96. };
  97. //-----------------------------------------------------------------------------
  98. // Vertex Shader
  99. //-----------------------------------------------------------------------------
  100. VSOutput VS( VSInput Input )
  101. {
  102. VSOutput Output;
  103. // Input Position is already in world space
  104. Output.Position = mul( float4( Input.Position, 1 ), GetViewProjection() );
  105. Output.Color = Input.Color;
  106. Output.DiffuseTexCoord = Input.DiffuseTexCoord;
  107. // Calculate fog
  108. Output.Fog = CalculateFog( Fog, Input.Position, GetEyePosition() ).xxx;
  109. // Calculate Shroud UVs
  110. // Note: Input is already in world space
  111. Output.ShroudTexCoord = CalculateShroudTexCoord( Shroud, Input.Position );
  112. return Output;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Pixel Shader
  116. //-----------------------------------------------------------------------------
  117. float4 PS( VSOutput Input, uniform int fog_mode ) COLORTARGET
  118. {
  119. // Get the base color
  120. float4 diffuse_texture = tex2D( SAMPLER(DiffuseTexture), Input.DiffuseTexCoord );
  121. float4 color = diffuse_texture * Input.Color;
  122. // Apply fog
  123. if( fog_mode == FogMode_Opaque )
  124. {
  125. color.xyz = lerp( Fog.Color, color.xyz, Input.Fog );
  126. }
  127. else if( fog_mode == FogMode_Additive )
  128. {
  129. color.xyz *= Input.Fog;
  130. }
  131. // Apply shroud
  132. float shroud = tex2D( SAMPLER(ShroudTexture), Input.ShroudTexCoord ).x;
  133. shroud = BiasShroudValueForEffects( shroud );
  134. color.xyz *= shroud;
  135. return color;
  136. }
  137. //-----------------------------------------------------------------------------
  138. float4 PS_Xenon( VSOutput In ) : COLOR
  139. {
  140. return PS( In, Fog.IsEnabled ? ((Draw.ShaderType == ShaderType_Additive || Draw.ShaderType == ShaderType_AdditiveAlphaTest || Draw.ShaderType == ShaderType_Multiply) ? FogMode_Additive : FogMode_Opaque) : FogMode_Disabled );
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Techniques
  144. //-----------------------------------------------------------------------------
  145. #define PS_ShaderType \
  146. compile PS_2_0 PS(FogMode_Disabled), \
  147. compile PS_2_0 PS(FogMode_Opaque), \
  148. compile PS_2_0 PS(FogMode_Additive)
  149. DEFINE_ARRAY_MULTIPLIER( PS_Multiplier_Final = 3 );
  150. #if SUPPORTS_SHADER_ARRAYS
  151. pixelshader PS_Array[PS_Multiplier_Final] =
  152. {
  153. PS_ShaderType
  154. };
  155. #endif
  156. //-----------------------------------------------------------------------------
  157. technique Default
  158. {
  159. pass pass0
  160. <
  161. USE_EXPRESSION_EVALUATOR("Particle")
  162. >
  163. {
  164. VertexShader = compile VS_2_0 VS();
  165. PixelShader = ARRAY_EXPRESSION_PS( PS_Array,
  166. Fog.IsEnabled ? ((Draw.ShaderType == ShaderType_Additive || Draw.ShaderType == ShaderType_AdditiveAlphaTest || Draw.ShaderType == ShaderType_Multiply) ? FogMode_Additive : FogMode_Opaque) : FogMode_Disabled,
  167. compile PS_VERSION PS_Xenon() );
  168. ZEnable = true;
  169. ZWriteEnable = false;
  170. ZFunc = ZFUNC_INFRONT;
  171. CullMode = none;
  172. SETUP_ALPHA_BLEND_AND_TEST(Draw.ShaderType);
  173. }
  174. }