RiverReflection.fx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // ----------------------------------------------------------------------------
  9. // MATERIAL PARAMATERS
  10. // ----------------------------------------------------------------------------
  11. SAMPLER_2D_BEGIN( RiverTexture,
  12. string UIName = "River Texture";
  13. )
  14. MipFilter = LINEAR;
  15. MinFilter = LINEAR;
  16. MagFilter = LINEAR;
  17. AddressU = WRAP;
  18. AddressV = WRAP;
  19. SAMPLER_2D_END
  20. float4 Opacity
  21. <
  22. string UIName = "Opacity";
  23. > = float4(1, 1, 1, 1);
  24. // ----------------------------------------------------------------------------
  25. // Reflection
  26. // ----------------------------------------------------------------------------
  27. SAMPLER_2D_BEGIN( WaterReflectionTexture,
  28. string UIWidget = "None";
  29. string SasBindAddress = "Water.ReflectionTexture";
  30. )
  31. MipFilter = Point;
  32. MinFilter = Linear;
  33. MagFilter = Linear;
  34. AddressU = CLAMP;
  35. AddressV = CLAMP;
  36. SAMPLER_2D_END
  37. // ----------------------------------------------------------------------------
  38. // Environment map
  39. // ----------------------------------------------------------------------------
  40. SAMPLER_CUBE_BEGIN( EnvironmentTexture,
  41. string UIWidget = "None";
  42. string SasBindAddress = "Terrain.EnvironmentTexture";
  43. string ResourceType = "Cube";
  44. )
  45. MinFilter = Linear;
  46. MagFilter = Linear;
  47. MipFilter = Linear;
  48. AddressU = Clamp;
  49. AddressV = Clamp;
  50. AddressW = Clamp;
  51. SAMPLER_CUBE_END
  52. // ----------------------------------------------------------------------------
  53. // Shroud
  54. // ----------------------------------------------------------------------------
  55. ShroudSetup Shroud
  56. <
  57. string UIWidget = "None";
  58. string SasBindAddress = "Terrain.Shroud";
  59. > = DEFAULT_SHROUD;
  60. SAMPLER_2D_BEGIN( ShroudTexture,
  61. string UIWidget = "None";
  62. string SasBindAddress = "Terrain.Shroud.Texture";
  63. string ResourceName = "ShaderPreviewShroud.dds";
  64. )
  65. MinFilter = Linear;
  66. MagFilter = Linear;
  67. MipFilter = Linear;
  68. AddressU = Clamp;
  69. AddressV = Clamp;
  70. SAMPLER_2D_END
  71. // ----------------------------------------------------------------------------
  72. // Transformations
  73. // ----------------------------------------------------------------------------
  74. float4x4 WorldViewProjection : WorldViewProjection;
  75. float4x4 World : World;
  76. #if defined(_WW3D_)
  77. #if !defined(USE_INDIRECT_CONSTANT)
  78. float3 EyePosition
  79. <
  80. string UIWidget = "None";
  81. string SasBindAddress = "Sas.Camera.Position";
  82. >;
  83. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  84. float3 GetEyePosition()
  85. {
  86. return EyePosition;
  87. }
  88. #else // #if defined(_WW3D_)
  89. float4x3 ViewI : ViewInverse;
  90. float3 GetEyePosition()
  91. {
  92. return ViewI[3];
  93. }
  94. #endif // #if defined(_WW3D_)
  95. float Time : Time;
  96. // ----------------------------------------------------------------------------
  97. // SHADER: Default (UltraHigh)
  98. // ----------------------------------------------------------------------------
  99. struct VSOutput
  100. {
  101. float4 Position : POSITION;
  102. float4 Color : COLOR0;
  103. float Fog : COLOR1;
  104. float2 RiverTexCoord : TEXCOORD0;
  105. float2 ShroudTexCoord : TEXCOORD1;
  106. float3 ReflectionTexCoord : TEXCOORD2;
  107. };
  108. // ----------------------------------------------------------------------------
  109. VSOutput VS( float3 Position : POSITION,
  110. float4 Color : COLOR0,
  111. float2 RiverTexCoord : TEXCOORD0,
  112. float2 NormalTexCoord : TEXCOORD1 )
  113. {
  114. VSOutput Out;
  115. // Position
  116. Out.Position = mul( float4(Position, 1), WorldViewProjection );
  117. // Color
  118. float4 color = Color;
  119. color.w *= Opacity.w;
  120. Out.Color = color;
  121. // Fog
  122. float3 worldPosition = mul( float4(Position, 1), World );
  123. Out.Fog = CalculateFog( Fog, worldPosition, GetEyePosition() );
  124. // RiverTexCoord
  125. Out.RiverTexCoord = NormalTexCoord;
  126. // ShroudTexCoord
  127. Out.ShroudTexCoord = CalculateShroudTexCoord( Shroud, worldPosition );
  128. // ReflectionTexCoord
  129. Out.ReflectionTexCoord.xy = 0.5 * ( Out.Position.xy + Out.Position.w * float2(1.0, 1.0) );
  130. Out.ReflectionTexCoord.z = Out.Position.w;
  131. return Out;
  132. }
  133. // ----------------------------------------------------------------------------
  134. float4 PS( VSOutput In ) : COLOR
  135. {
  136. // Sample the base texture
  137. float4 color = tex2D( SAMPLER(RiverTexture), In.RiverTexCoord );
  138. // Apply vertex color
  139. color *= In.Color;
  140. // Apply the reflection map
  141. float2 reflectionCoord = In.ReflectionTexCoord.xy / In.ReflectionTexCoord.z;
  142. float3 reflectionColor = tex2D( SAMPLER(WaterReflectionTexture), reflectionCoord );
  143. color.xyz *= reflectionColor;
  144. // Apply fog
  145. color.xyz *= In.Fog;
  146. // Apply shroud
  147. float3 shroud = tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord ).xyz;
  148. color.xyz *= shroud;
  149. return color;
  150. }
  151. // ----------------------------------------------------------------------------
  152. technique Default_U
  153. {
  154. pass P0
  155. {
  156. VertexShader = compile VS_2_0 VS();
  157. PixelShader = compile PS_2_0 PS();
  158. ZEnable = true;
  159. ZFunc = ZFUNC_INFRONT;
  160. ZWriteEnable = false;
  161. CullMode = None;
  162. AlphaBlendEnable = true;
  163. AlphaTestEnable = false;
  164. ColorWriteEnable = 7;
  165. SrcBlend = SrcAlpha;
  166. DestBlend = One;
  167. }
  168. }
  169. #if ENABLE_LOD
  170. // ----------------------------------------------------------------------------
  171. // SHADER: Default Medium Quality
  172. // ----------------------------------------------------------------------------
  173. struct VSOutput_M
  174. {
  175. float4 Position : POSITION;
  176. float4 Color : COLOR0;
  177. float Fog : COLOR1;
  178. float2 RiverTexCoord : TEXCOORD0;
  179. float2 ShroudTexCoord : TEXCOORD1;
  180. float3 ReflectionTexCoord : TEXCOORD2;
  181. };
  182. // ----------------------------------------------------------------------------
  183. VSOutput_M VS_M( float3 Position : POSITION,
  184. float4 Color : COLOR0,
  185. float2 RiverTexCoord : TEXCOORD0,
  186. float2 NormalTexCoord : TEXCOORD1 )
  187. {
  188. VSOutput_M Out;
  189. // Position
  190. Out.Position = mul( float4(Position, 1), WorldViewProjection );
  191. // Color
  192. float4 color = Color;
  193. color.w *= Opacity.w;
  194. Out.Color = color;
  195. // Fog
  196. float3 worldPosition = mul( float4(Position, 1), World );
  197. Out.Fog = CalculateFog( Fog, worldPosition, GetEyePosition() );
  198. // RiverTexCoord
  199. Out.RiverTexCoord = NormalTexCoord;
  200. // ShroudTexCoord
  201. Out.ShroudTexCoord = CalculateShroudTexCoord( Shroud, worldPosition );
  202. // Compute view direction in world space
  203. float3 worldEyeDir = normalize( GetEyePosition() - worldPosition );
  204. // Compute env map reflection direction
  205. float3 worldNormal = float3( 0, 0, 1 ); // Rivers always face up
  206. Out.ReflectionTexCoord = -reflect( worldEyeDir, worldNormal );
  207. return Out;
  208. }
  209. // ----------------------------------------------------------------------------
  210. float4 PS_M( VSOutput_M In ) : COLOR
  211. {
  212. // Sample the base texture
  213. float4 color = tex2D( SAMPLER(RiverTexture), In.RiverTexCoord );
  214. // Apply vertex color
  215. color *= In.Color;
  216. // Apply the reflection map
  217. float3 reflectionColor = texCUBE( SAMPLER(EnvironmentTexture), In.ReflectionTexCoord );
  218. color.xyz *= reflectionColor;
  219. // Apply fog
  220. color.xyz *= In.Fog;
  221. // Apply shroud
  222. float3 shroud = tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord ).xyz;
  223. color.xyz *= shroud;
  224. return color;
  225. }
  226. // ----------------------------------------------------------------------------
  227. // TECHNIQUE: High Quality
  228. // ----------------------------------------------------------------------------
  229. technique _Default
  230. {
  231. pass P0
  232. {
  233. VertexShader = compile VS_2_0 VS_M();
  234. PixelShader = compile PS_2_0 PS_M();
  235. ZEnable = true;
  236. ZFunc = ZFUNC_INFRONT;
  237. ZWriteEnable = false;
  238. CullMode = None;
  239. AlphaBlendEnable = true;
  240. AlphaTestEnable = false;
  241. ColorWriteEnable = 7;
  242. SrcBlend = SrcAlpha;
  243. DestBlend = One;
  244. }
  245. }
  246. // ----------------------------------------------------------------------------
  247. // TECHNIQUE: Low Quality
  248. // ----------------------------------------------------------------------------
  249. technique _Default_L
  250. {
  251. // Do nothing!
  252. }
  253. #endif