Tracer.fx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2005 Electronic Arts Inc
  3. //
  4. // FX Shader for simple unlit rendering
  5. //////////////////////////////////////////////////////////////////////////////
  6. #define SUPPORT_FOG 1
  7. #include "Common.fxh"
  8. #include "Gamma.fxh"
  9. SAMPLER_2D_BEGIN( Texture_0,
  10. string UIWidget = "None";
  11. string UIName = "None";
  12. string SasBindAddress = "WW3D.MiscTexture";
  13. )
  14. MinFilter = MinFilterBest;
  15. MagFilter = MagFilterBest;
  16. MipFilter = MipFilterBest;
  17. AddressU = Clamp;
  18. AddressV = Clamp;
  19. SAMPLER_2D_END
  20. float4 TexCoordTransform_0
  21. <
  22. string UIName = "UV0 Scl/Move";
  23. string UIWidget = "Spinner";
  24. float UIMin = -100;
  25. float UIMax = 100;
  26. > = float4(1.0, 1.0, 0.0, 0.0);
  27. bool FogEnable
  28. <
  29. string UIName = "Fog Enable";
  30. > = true;
  31. // Variationgs for handling fog in the pixel shader
  32. static const int FogMode_Disabled = 0;
  33. static const int FogMode_Opaque = 1;
  34. static const int FogMode_Additive = 2;
  35. // ----------------------------------------------------------------------------
  36. // Shroud
  37. // ----------------------------------------------------------------------------
  38. ShroudSetup Shroud
  39. <
  40. string UIWidget = "None";
  41. string SasBindAddress = "Terrain.Shroud";
  42. > = DEFAULT_SHROUD;
  43. SAMPLER_2D_BEGIN( ShroudTexture,
  44. string UIWidget = "None";
  45. string SasBindAddress = "Terrain.Shroud.Texture";
  46. string ResourceName = "ShaderPreviewShroud.dds";
  47. )
  48. MinFilter = Linear;
  49. MagFilter = Linear;
  50. MipFilter = Linear;
  51. AddressU = Clamp;
  52. AddressV = Clamp;
  53. SAMPLER_2D_END
  54. // ----------------------------------------------------------------------------
  55. // Transformations
  56. // ----------------------------------------------------------------------------
  57. #if defined(_WW3D_)
  58. #if !defined(USE_INDIRECT_CONSTANT)
  59. float4x4 ViewProjection
  60. <
  61. string UIWidget = "None";
  62. string SasBindAddress = "Sas.Camera.WorldToProjection";
  63. >;
  64. float3 EyePosition
  65. <
  66. string UIWidget = "None";
  67. string SasBindAddress = "Sas.Camera.Position";
  68. >;
  69. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  70. float4x4 GetViewProjection()
  71. {
  72. return ViewProjection;
  73. }
  74. float3 GetEyePosition()
  75. {
  76. return EyePosition;
  77. }
  78. #else // #if defined(_WW3D_)
  79. float4x4 Projection : Projection;
  80. float4x4 View : View;
  81. float4x3 ViewI : ViewInverse;
  82. float4x4 GetViewProjection()
  83. {
  84. return mul(View, Projection);
  85. }
  86. float3 GetEyePosition()
  87. {
  88. return ViewI[3];
  89. }
  90. #endif // #if defined(_WW3D_)
  91. float Time : Time;
  92. // ----------------------------------------------------------------------------
  93. // SHADER: DEFAULT
  94. // ----------------------------------------------------------------------------
  95. struct VSOutput
  96. {
  97. float4 Position : POSITION;
  98. float4 DiffuseColor : COLOR0;
  99. float Fog : COLOR1;
  100. float2 BaseTexCoord : TEXCOORD0;
  101. float2 ShroudTexCoord : TEXCOORD1;
  102. };
  103. VSOutput VS(float3 Position : POSITION, float2 TexCoord0 : TEXCOORD0, float4 Color : COLOR)
  104. {
  105. VSOutput Out;
  106. // Position is already in world space
  107. Out.Position = mul( float4( Position, 1 ), GetViewProjection() );
  108. // Unlit colorization
  109. Out.DiffuseColor = Color;
  110. // Scale with animated offset on texture coordinates 0
  111. Out.BaseTexCoord = TexCoord0 * TexCoordTransform_0.xy + Time * TexCoordTransform_0.zw;
  112. // Calculate fog
  113. Out.Fog = FogEnable ? CalculateFog(Fog, Position, GetEyePosition()) : 1;
  114. // Calculate Shroud UVs
  115. Out.ShroudTexCoord = CalculateShroudTexCoord( Shroud, Position );
  116. return Out;
  117. }
  118. // ----------------------------------------------------------------------------
  119. float4 PS(VSOutput In, uniform int fogMode, uniform bool applyGammaCorrection) COLORTARGET
  120. {
  121. // Get vertex color
  122. float4 color = In.DiffuseColor;
  123. // Apply texture
  124. float4 textureSample = tex2D( SAMPLER(Texture_0), In.BaseTexCoord);
  125. if (applyGammaCorrection)
  126. {
  127. textureSample.xyz = GammaToLinear(textureSample.xyz);
  128. }
  129. color *= textureSample;
  130. // Apply fog
  131. if (fogMode == FogMode_Opaque)
  132. {
  133. color.xyz = lerp(Fog.Color, color.xyz, In.Fog);
  134. }
  135. else if (fogMode == FogMode_Additive)
  136. {
  137. // Fog used with additive blending just needs to reduce the additive influence, not blend towards the fog color
  138. color.xyz *= In.Fog;
  139. }
  140. // Apply shroud
  141. float shroud = tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord ).x;
  142. shroud = BiasShroudValueForEffects( shroud );
  143. color.xyz *= shroud;
  144. return color;
  145. }
  146. // ----------------------------------------------------------------------------
  147. float4 PS_XenonAlpha(VSOutput In) : COLOR
  148. {
  149. return PS(In, Fog.IsEnabled ? FogMode_Opaque : FogMode_Disabled, true);
  150. }
  151. // ----------------------------------------------------------------------------
  152. float4 PS_XenonAdditive(VSOutput In) : COLOR
  153. {
  154. return PS(In, Fog.IsEnabled ? FogMode_Additive : FogMode_Disabled, true);
  155. }
  156. // ----------------------------------------------------------------------------
  157. DEFINE_ARRAY_MULTIPLIER( PS_Multiplier_FogMode = 1 );
  158. #define PS_FogMode \
  159. compile PS_2_0 PS(FogMode_Disabled, true), \
  160. compile PS_2_0 PS(FogMode_Opaque, true), \
  161. compile PS_2_0 PS(FogMode_Additive, true)
  162. DEFINE_ARRAY_MULTIPLIER( PS_Multiplier_Final = PS_Multiplier_FogMode * 3 );
  163. #if SUPPORTS_SHADER_ARRAYS
  164. pixelshader PS_Array[PS_Multiplier_Final] =
  165. {
  166. PS_FogMode
  167. };
  168. #endif
  169. technique Additive
  170. {
  171. pass P0
  172. {
  173. VertexShader = compile VS_2_0 VS();
  174. PixelShader = ARRAY_EXPRESSION_PS( PS_Array,
  175. (Fog.IsEnabled ? FogMode_Additive : FogMode_Disabled) * PS_Multiplier_FogMode,
  176. compile PS_VERSION PS_XenonAdditive()
  177. );
  178. AlphaBlendEnable = true;
  179. AlphaTestEnable = false;
  180. CullMode = None;
  181. ZEnable = true;
  182. ZFunc = ZFUNC_INFRONT;
  183. ZWriteEnable = false;
  184. SrcBlend = One;
  185. DestBlend = One;
  186. }
  187. }
  188. technique Alpha
  189. {
  190. pass P0
  191. {
  192. VertexShader = compile VS_2_0 VS();
  193. PixelShader = ARRAY_EXPRESSION_PS( PS_Array,
  194. (Fog.IsEnabled ? FogMode_Opaque : FogMode_Disabled) * PS_Multiplier_FogMode,
  195. compile PS_VERSION PS_XenonAlpha()
  196. );
  197. AlphaBlendEnable = true;
  198. AlphaTestEnable = false;
  199. CullMode = None;
  200. ZEnable = true;
  201. ZFunc = ZFUNC_INFRONT;
  202. ZWriteEnable = false;
  203. SrcBlend = SrcAlpha;
  204. DestBlend = InvSrcAlpha;
  205. }
  206. }
  207. #if ENABLE_LOD
  208. // ----------------------------------------------------------------------------
  209. // Techniques: Medium LOD
  210. // ----------------------------------------------------------------------------
  211. DEFINE_ARRAY_MULTIPLIER( PS_M_Multiplier_FogMode = 1 );
  212. #define PS_M_FogMode \
  213. compile PS_2_0 PS(FogMode_Disabled, false), \
  214. compile PS_2_0 PS(FogMode_Opaque, false), \
  215. compile PS_2_0 PS(FogMode_Additive, false)
  216. DEFINE_ARRAY_MULTIPLIER( PS_M_Multiplier_Final = PS_M_Multiplier_FogMode * 3 );
  217. #if SUPPORTS_SHADER_ARRAYS
  218. pixelshader PS_M_Array[PS_M_Multiplier_Final] =
  219. {
  220. PS_M_FogMode
  221. };
  222. #endif
  223. technique _Additive_M
  224. {
  225. pass P0
  226. {
  227. VertexShader = compile VS_2_0 VS();
  228. PixelShader = ARRAY_EXPRESSION_PS( PS_M_Array,
  229. (Fog.IsEnabled ? FogMode_Additive : FogMode_Disabled) * PS_M_Multiplier_FogMode,
  230. compile PS_VERSION PS_XenonAdditive()
  231. );
  232. AlphaBlendEnable = true;
  233. AlphaTestEnable = false;
  234. CullMode = None;
  235. ZEnable = true;
  236. ZFunc = ZFUNC_INFRONT;
  237. ZWriteEnable = false;
  238. SrcBlend = One;
  239. DestBlend = One;
  240. }
  241. }
  242. technique _Alpha_M
  243. {
  244. pass P0
  245. {
  246. VertexShader = compile VS_2_0 VS();
  247. PixelShader = ARRAY_EXPRESSION_PS( PS_M_Array,
  248. (Fog.IsEnabled ? FogMode_Opaque : FogMode_Disabled) * PS_M_Multiplier_FogMode,
  249. compile PS_VERSION PS_XenonAlpha()
  250. );
  251. AlphaBlendEnable = true;
  252. AlphaTestEnable = false;
  253. CullMode = None;
  254. ZEnable = true;
  255. ZFunc = ZFUNC_INFRONT;
  256. ZWriteEnable = false;
  257. SrcBlend = SrcAlpha;
  258. DestBlend = InvSrcAlpha;
  259. }
  260. }
  261. // ----------------------------------------------------------------------------
  262. // Techniques: Low LOD
  263. // ----------------------------------------------------------------------------
  264. technique _Additive_L
  265. {
  266. pass P0
  267. {
  268. VertexShader = compile VS_2_0 VS();
  269. PixelShader = compile PS_2_0 PS(FogMode_Disabled, false);
  270. AlphaBlendEnable = true;
  271. AlphaTestEnable = false;
  272. CullMode = None;
  273. ZEnable = true;
  274. ZFunc = ZFUNC_INFRONT;
  275. ZWriteEnable = false;
  276. SrcBlend = One;
  277. DestBlend = One;
  278. }
  279. }
  280. technique _Alpha_L
  281. {
  282. pass P0
  283. {
  284. VertexShader = compile VS_2_0 VS();
  285. PixelShader = compile PS_2_0 PS(FogMode_Disabled, false);
  286. AlphaBlendEnable = true;
  287. AlphaTestEnable = false;
  288. CullMode = None;
  289. ZEnable = true;
  290. ZFunc = ZFUNC_INFRONT;
  291. ZWriteEnable = false;
  292. SrcBlend = SrcAlpha;
  293. DestBlend = InvSrcAlpha;
  294. }
  295. }
  296. #endif // if ENABLE_LOD