DistortingObject.fx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2006 Electronic Arts Inc
  3. //
  4. // FX Shader for rendering objects into the distortion buffer
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include "Common.fxh"
  7. int _SasGlobal : SasGlobal
  8. <
  9. string UIWidget = "None";
  10. int3 SasVersion = int3(1, 0, 0);
  11. string RenderBin = "Distorter";
  12. > = 0;
  13. #if defined(EA_PLATFORM_WINDOWS) && defined(_3DSMAX_)
  14. // ----------------------------------------------------------------------------
  15. // SAMPLER : nhendricks : had to pull these in here for MAX to compile
  16. // ----------------------------------------------------------------------------
  17. #define SAMPLER_2D_BEGIN(samplerName, annotations) \
  18. texture samplerName \
  19. < \
  20. annotations \
  21. >; \
  22. sampler2D samplerName##Sampler = sampler_state \
  23. { \
  24. Texture = < samplerName >;
  25. #define SAMPLER_2D_END };
  26. #define SAMPLER( samplerName ) samplerName##Sampler
  27. #define SAMPLER_CUBE_BEGIN(samplerName, annotations) \
  28. texture samplerName \
  29. < \
  30. annotations \
  31. >; \
  32. samplerCUBE samplerName##Sampler = sampler_state \
  33. { \
  34. Texture = < samplerName >;
  35. #define SAMPLER_CUBE_END };
  36. #endif
  37. // ----------------------------------------------------------------------------
  38. // Skinning
  39. // ----------------------------------------------------------------------------
  40. #define MaxSkinningBonesPerVertex 1
  41. #include "Skinning.fxh"
  42. SAMPLER_2D_BEGIN( NormalMap,
  43. string UIName = "Normal Texture";
  44. )
  45. MinFilter = MinFilterBest;
  46. MagFilter = MagFilterBest;
  47. MipFilter = MipFilterBest;
  48. MaxAnisotropy = 8;
  49. AddressU = Wrap;
  50. AddressV = Wrap;
  51. SAMPLER_2D_END
  52. float4 TexCoordTransform_0
  53. <
  54. string UIName = "UV0 Scl/Move";
  55. string UIWidget = "Spinner";
  56. int UIMin = -1000;
  57. int UIMax = 1000;
  58. > = float4(1.0, 1.0, 0.0, 0.0);
  59. float BumpScale
  60. <
  61. string UIName = "Bump Height";
  62. string UIWidget = "Slider";
  63. float UIMin = 0.0;
  64. float UIMax = 10.0;
  65. float UIStep = 0.1;
  66. > = 1.0;
  67. bool AlphaTestEnable
  68. <
  69. string UIName = "Alpha Test Enable";
  70. > = false;
  71. // ----------------------------------------------------------------------------
  72. // Shroud
  73. // ----------------------------------------------------------------------------
  74. ShroudSetup Shroud
  75. <
  76. string UIWidget = "None";
  77. #if !defined(_W3DVIEW_)
  78. string SasBindAddress = "Terrain.Shroud";
  79. #endif
  80. > = DEFAULT_SHROUD;
  81. SAMPLER_2D_BEGIN( ShroudTexture,
  82. string UIWidget = "None";
  83. string SasBindAddress = "Terrain.Shroud.Texture";
  84. )
  85. MinFilter = Linear;
  86. MagFilter = Linear;
  87. MipFilter = Linear;
  88. AddressU = Clamp;
  89. AddressV = Clamp;
  90. SAMPLER_2D_END
  91. #if !defined(USE_INDIRECT_CONSTANT)
  92. float OpacityOverride
  93. <
  94. string UIWidget = "None";
  95. string SasBindAddress = "WW3D.OpacityOverride";
  96. > = 1.0;
  97. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  98. // ----------------------------------------------------------------------------
  99. // Transformations (world transformations are in skinning header)
  100. // ----------------------------------------------------------------------------
  101. float4x4 View : View;
  102. #if defined(_WW3D_)
  103. #if !defined(USE_INDIRECT_CONSTANT)
  104. float4x4 ViewProjection
  105. <
  106. string UIWidget = "None";
  107. string SasBindAddress = "Sas.Camera.WorldToProjection";
  108. >;
  109. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  110. float4x4 GetViewProjection()
  111. {
  112. return ViewProjection;
  113. }
  114. #else
  115. float4x4 Projection : Projection;
  116. float4x4 GetViewProjection()
  117. {
  118. return mul(View, Projection);
  119. }
  120. #endif
  121. float Time : Time;
  122. // ----------------------------------------------------------------------------
  123. // SHADER: DEFAULT
  124. // ----------------------------------------------------------------------------
  125. struct VSOutput {
  126. float4 Position : POSITION;
  127. float4 Color : COLOR0;
  128. float2 TexCoord0 : TEXCOORD0;
  129. float2 ShroudTexCoord : TEXCOORD1;
  130. float3x3 TangentToViewSpace : TEXCOORD2;
  131. };
  132. // ----------------------------------------------------------------------------
  133. VSOutput VS(VSInputSkinningOneBoneTangentFrame InSkin,
  134. float2 TexCoord : TEXCOORD0,
  135. float4 VertexColor : COLOR0,
  136. uniform int numJointsPerVertex)
  137. {
  138. USE_DIRECTIONAL_LIGHT_INTERACTIVE(DirectionalLight, 0);
  139. VSOutput Out;
  140. float3 worldPosition = 0;
  141. float3 worldNormal = 0;
  142. float3 worldTangent = 0;
  143. float3 worldBinormal = 0;
  144. #if defined(_3DSMAX_)
  145. Time = Time * .2;
  146. VertexColor = float4(1,1,1,1);
  147. #endif
  148. CalculatePositionAndTangentFrame(InSkin, numJointsPerVertex,
  149. worldPosition, worldNormal, worldTangent, worldBinormal);
  150. // transform position to projection space
  151. Out.Position = mul(float4(worldPosition, 1), GetViewProjection());
  152. float3 viewPosition = mul(float4(worldPosition, 1), View);
  153. // Build 3x3 tranform from tangent to world space
  154. float3x3 tangentToWorldSpace = float3x3(-worldBinormal, -worldTangent, worldNormal);
  155. Out.TangentToViewSpace = mul(tangentToWorldSpace, (float3x3)View);
  156. Out.Color = float4(mul(worldNormal, (float3x3)View) * 0.5 + 0.5, OpacityOverride);
  157. Out.Color = float4(float(500 / (200 -viewPosition.z)).xxx, OpacityOverride) * VertexColor;
  158. // Out.Color = float4(1..xxx, OpacityOverride) * VertexColor;
  159. // pass texture coordinates for fetching the diffuse and normal maps
  160. Out.TexCoord0.xy = TexCoord.xy * TexCoordTransform_0.xy + Time * TexCoordTransform_0.zw;
  161. // Calculate shroud texture coordinates
  162. Out.ShroudTexCoord = CalculateShroudTexCoord(Shroud, worldPosition);
  163. return Out;
  164. }
  165. // ----------------------------------------------------------------------------
  166. float4 PS(VSOutput In) : COLOR
  167. {
  168. float2 texCoord0 = In.TexCoord0;
  169. float4 normalMapSample = tex2D( SAMPLER(NormalMap), texCoord0);
  170. float3 bumpNormal = normalMapSample.xyz * 2.0 - 1.0;
  171. // Scale normal to increase/decrease bump effect
  172. bumpNormal.xy *= BumpScale;
  173. bumpNormal = normalize(bumpNormal);
  174. float3 normal = mul(bumpNormal, In.TangentToViewSpace);
  175. float4 color = float4(In.Color.xyz * normal * 0.5 + 0.5, In.Color.w * normalMapSample.w);
  176. color.w *= tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord).w;
  177. return color;
  178. }
  179. // ----------------------------------------------------------------------------
  180. VSOutput VS_Xenon(
  181. VSInputSkinningOneBoneTangentFrame InSkin,
  182. float2 TexCoord : TEXCOORD0,
  183. float4 VertexColor : COLOR0 )
  184. {
  185. return VS( InSkin, TexCoord, VertexColor, min(NumJointsPerVertex, 1) );
  186. }
  187. // ----------------------------------------------------------------------------
  188. // TECHNIQUE: Default
  189. // ----------------------------------------------------------------------------
  190. #define VS_NumJointsPerVertex \
  191. compile VS_2_0 VS(0), \
  192. compile VS_2_0 VS(1)
  193. DEFINE_ARRAY_MULTIPLIER( VS_Multiplier_Final = 2 );
  194. #if SUPPORTS_SHADER_ARRAYS
  195. vertexshader VS_Array[VS_Multiplier_Final] =
  196. {
  197. VS_NumJointsPerVertex
  198. };
  199. #endif
  200. // ----------------------------------------------------------------------------
  201. // Technique: Default (Medium and up)
  202. // ----------------------------------------------------------------------------
  203. technique Default_M
  204. {
  205. pass p0
  206. <
  207. USE_EXPRESSION_EVALUATOR("DistortingObject")
  208. >
  209. {
  210. VertexShader = ARRAY_EXPRESSION_VS( VS_Array,
  211. min(NumJointsPerVertex, 1),
  212. compile VS_VERSION VS_Xenon()
  213. );
  214. PixelShader = compile PS_2_0 PS();
  215. ZEnable = true;
  216. ZFunc = ZFUNC_INFRONT;
  217. ZWriteEnable = false;//true;
  218. CullMode = CW;
  219. CullMode = None;
  220. AlphaBlendEnable = true;
  221. SrcBlend = SrcAlpha;
  222. DestBlend = InvSrcAlpha;
  223. // As an optimization some objects specify the alpha test enable flag to throw away pixels.
  224. // This doesn't work on Xenon unless we implement a ShaderExpressionEvaluator for it,
  225. // and since it's only an optimization, leave it out for now.
  226. #if defined(EA_PLATFORM_XENON) || defined(EA_PLATFORM_PS3)
  227. AlphaTestEnable = false;
  228. #else
  229. AlphaTestEnable = ( AlphaTestEnable );
  230. AlphaFunc = GreaterEqual;
  231. AlphaRef = DEFAULT_ALPHATEST_THRESHOLD;
  232. #endif
  233. }
  234. }
  235. // ----------------------------------------------------------------------------
  236. // Technique: Default (Low)
  237. // ----------------------------------------------------------------------------
  238. #if !defined(_3DSMAX_) // 3DS Max crashes when a technique is empty
  239. technique Default_L
  240. {
  241. // No passes. Indicates technique disabled.
  242. }
  243. #endif // !defined(_3DSMAX_)