DistortingObject.fx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2006 Electronic Arts Inc
  3. //
  4. // FX Shader for rendering objects into the distortion buffer
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include "Common.fxh"
  7. int _GlobalInfo : SasGlobal
  8. <
  9. string UIWidget = "None";
  10. int3 SasVersion = int3(1, 0, 0);
  11. int SortLevel = SortLevel_Distorter;
  12. > = 0;
  13. #if defined(EA_PLATFORM_WINDOWS)
  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. static const int 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. int ObjectShroudStatus
  92. <
  93. string UIWidget = "None";
  94. #if !defined(_W3DVIEW_)
  95. string SasBindAddress = "Terrain.Shroud.ObjectShroudStatus";
  96. #endif
  97. > = OBJECTSHROUD_INVALID;
  98. float OpacityOverride
  99. <
  100. string UIWidget = "None";
  101. string SasBindAddress = "WW3D.OpacityOverride";
  102. > = 1.0;
  103. // ----------------------------------------------------------------------------
  104. // Transformations (world transformations are in skinning header)
  105. // ----------------------------------------------------------------------------
  106. float4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
  107. float4x4 View : View;
  108. float4x3 ViewI : ViewInverse;
  109. float4x4 Projection : Projection;
  110. float Time : Time;
  111. // ----------------------------------------------------------------------------
  112. // SHADER: DEFAULT
  113. // ----------------------------------------------------------------------------
  114. struct VSOutput {
  115. float4 Position : POSITION;
  116. float4 Color : COLOR0;
  117. float2 TexCoord0 : TEXCOORD0;
  118. float2 ShroudTexCoord : TEXCOORD1;
  119. float3x3 TangentToViewSpace : TEXCOORD2;
  120. };
  121. // ----------------------------------------------------------------------------
  122. VSOutput VS(VSInputSkinningOneBoneTangentFrame InSkin, float2 TexCoord : TEXCOORD0,
  123. uniform int numJointsPerVertex)
  124. {
  125. USE_DIRECTIONAL_LIGHT_INTERACTIVE(DirectionalLight, 0);
  126. VSOutput Out;
  127. float3 worldPosition = 0;
  128. float3 worldNormal = 0;
  129. float3 worldTangent = 0;
  130. float3 worldBinormal = 0;
  131. CalculatePositionAndTangentFrame(InSkin, numJointsPerVertex,
  132. worldPosition, worldNormal, worldTangent, worldBinormal);
  133. // transform position to projection space
  134. Out.Position = mul(float4(worldPosition, 1), mul(View, Projection));
  135. float3 viewPosition = mul(float4(worldPosition, 1), View);
  136. // Build 3x3 tranform from tangent to world space
  137. float3x3 tangentToWorldSpace = float3x3(-worldBinormal, -worldTangent, worldNormal);
  138. Out.TangentToViewSpace = mul(tangentToWorldSpace, (float3x3)View);
  139. //Out.Color = float4(mul(worldNormal, (float3x3)View) * 0.5 + 0.5, OpacityOverride);
  140. //Out.Color = float4(float(500 / (300 -viewPosition.z)).xxx, OpacityOverride);
  141. Out.Color = float4(1..xxx, OpacityOverride);
  142. // pass texture coordinates for fetching the diffuse and normal maps
  143. Out.TexCoord0.xy = TexCoord.xy * TexCoordTransform_0.xy + Time * TexCoordTransform_0.zw;
  144. // Calculate shroud texture coordinates
  145. Out.ShroudTexCoord = CalculateShroudTexCoord(Shroud, worldPosition);
  146. return Out;
  147. }
  148. // ----------------------------------------------------------------------------
  149. float4 PS(VSOutput In, uniform bool applyShroud) : COLOR
  150. {
  151. float2 texCoord0 = In.TexCoord0;
  152. float4 normalMapSample = tex2D( SAMPLER(NormalMap), texCoord0);
  153. float3 bumpNormal = normalMapSample.xyz * 2.0 - 1.0;
  154. // Scale normal to increase/decrease bump effect
  155. bumpNormal.xy *= BumpScale;
  156. bumpNormal = normalize(bumpNormal);
  157. float3 normal = mul(bumpNormal, In.TangentToViewSpace);
  158. float4 color = float4(In.Color.xyz * normal * 0.5 + 0.5, In.Color.w * normalMapSample.w);
  159. if (applyShroud)
  160. {
  161. color.w *= tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord).w;
  162. }
  163. return color;
  164. }
  165. // ----------------------------------------------------------------------------
  166. VSOutput VS_Xenon( VSInputSkinningOneBoneTangentFrame InSkin, float2 TexCoord : TEXCOORD0 )
  167. {
  168. return VS( InSkin, TexCoord, min(NumJointsPerVertex, 1) );
  169. }
  170. // ----------------------------------------------------------------------------
  171. float4 PS_Xenon( VSOutput In ) : COLOR
  172. {
  173. return PS( In, (ObjectShroudStatus == OBJECTSHROUD_PARTIAL_CLEAR) );
  174. }
  175. // ----------------------------------------------------------------------------
  176. // TECHNIQUE: Default
  177. // ----------------------------------------------------------------------------
  178. #define VS_NumJointsPerVertex \
  179. compile vs_2_0 VS(0), \
  180. compile vs_2_0 VS(1)
  181. DEFINE_ARRAY_MULTIPLIER( VS_Multiplier_Final = 2 );
  182. #if SUPPORTS_SHADER_ARRAYS
  183. vertexshader VS_Array[VS_Multiplier_Final] =
  184. {
  185. VS_NumJointsPerVertex
  186. };
  187. #endif
  188. DEFINE_ARRAY_MULTIPLIER( PS_Multiplier_ApplyShroud = 1 );
  189. #define PS_ApplyShroud \
  190. compile ps_2_0 PS(false), \
  191. compile ps_2_0 PS(true)
  192. DEFINE_ARRAY_MULTIPLIER( PS_Multiplier_Final = PS_Multiplier_ApplyShroud * 2 );
  193. #if SUPPORTS_SHADER_ARRAYS
  194. pixelshader PS_Array[PS_Multiplier_Final] =
  195. {
  196. PS_ApplyShroud
  197. };
  198. #endif
  199. // ----------------------------------------------------------------------------
  200. // Technique: Default (Medium and up)
  201. // ----------------------------------------------------------------------------
  202. technique Default_M
  203. <
  204. int MaxSkinningBones = MaxSkinningBones;
  205. >
  206. {
  207. pass p0
  208. <
  209. USE_EXPRESSION_EVALUATOR("DistortingObject")
  210. >
  211. {
  212. VertexShader = ARRAY_EXPRESSION_VS( VS_Array,
  213. min(NumJointsPerVertex, 1),
  214. compile VS_VERSION VS_Xenon()
  215. );
  216. PixelShader = ARRAY_EXPRESSION_PS( PS_Array,
  217. (ObjectShroudStatus == OBJECTSHROUD_PARTIAL_CLEAR) * PS_Multiplier_ApplyShroud,
  218. compile PS_VERSION PS_Xenon()
  219. );
  220. ZEnable = true;
  221. ZFunc = ZFUNC_INFRONT;
  222. ZWriteEnable = true;
  223. CullMode = CW;
  224. AlphaBlendEnable = true;
  225. SrcBlend = SrcAlpha;
  226. DestBlend = InvSrcAlpha;
  227. AlphaTestEnable = false;
  228. AlphaFunc = GreaterEqual;
  229. AlphaRef = 0x60; // WW3D magic number
  230. #if !defined( _NO_FIXED_FUNCTION_ )
  231. FogEnable = false;
  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. int MaxSkinningBones = MaxSkinningBones;
  242. >
  243. {
  244. // No passes. Indicates technique disabled.
  245. }
  246. #endif // !defined(_3DSMAX_)