DistortingObject.fx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. float DistortionStrength
  68. <
  69. string UIName = "Distortion Strength";
  70. string UIWidget = "Slider";
  71. float UIMin = 0.0f;
  72. float UIMax = 1.0f;
  73. float UIStep = .01f;
  74. > = 1.0f;
  75. float EdgeFadeOut
  76. <
  77. string UIName = "Edge Fade: Linear";
  78. string UIWidget = "Slider";
  79. float UIMax = 1.0f;
  80. float UIMin = -1.0f;
  81. float UIStep = .01f;
  82. > = 0.0f;
  83. bool AlphaTestEnable
  84. <
  85. string UIName = "Alpha Test Enable";
  86. > = false;
  87. bool CullingEnable
  88. <
  89. string UIName = "Backface Cull Enable";
  90. > = false;
  91. bool ZWriteEnable
  92. <
  93. string UIName = "Write to Z Buffer Enable";
  94. > = false;
  95. // ----------------------------------------------------------------------------
  96. // Shroud
  97. // ----------------------------------------------------------------------------
  98. ShroudSetup Shroud
  99. <
  100. string UIWidget = "None";
  101. #if !defined(_W3DVIEW_)
  102. string SasBindAddress = "Terrain.Shroud";
  103. #endif
  104. > = DEFAULT_SHROUD;
  105. SAMPLER_2D_BEGIN( ShroudTexture,
  106. string UIWidget = "None";
  107. string SasBindAddress = "Terrain.Shroud.Texture";
  108. )
  109. MinFilter = Linear;
  110. MagFilter = Linear;
  111. MipFilter = Linear;
  112. AddressU = Clamp;
  113. AddressV = Clamp;
  114. SAMPLER_2D_END
  115. #if !defined(USE_INDIRECT_CONSTANT)
  116. float OpacityOverride
  117. <
  118. string UIWidget = "None";
  119. string SasBindAddress = "WW3D.OpacityOverride";
  120. > = 1.0;
  121. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  122. // ----------------------------------------------------------------------------
  123. // Transformations (world transformations are in skinning header)
  124. // ----------------------------------------------------------------------------
  125. float4x4 View : View;
  126. float4x3 ViewI : ViewInverse;
  127. #if defined(_WW3D_)
  128. #if !defined(USE_INDIRECT_CONSTANT)
  129. float4x4 ViewProjection
  130. <
  131. string UIWidget = "None";
  132. string SasBindAddress = "Sas.Camera.WorldToProjection";
  133. >;
  134. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  135. float4x4 GetViewProjection()
  136. {
  137. return ViewProjection;
  138. }
  139. float3 GetEyePosition()
  140. {
  141. return EyePosition;
  142. }
  143. #else
  144. float4x4 Projection : Projection;
  145. float4x4 GetViewProjection()
  146. {
  147. return mul(View, Projection);
  148. }
  149. float3 GetEyePosition()
  150. {
  151. return ViewI[3];
  152. }
  153. #endif
  154. float Time : Time;
  155. // ----------------------------------------------------------------------------
  156. // SHADER: DEFAULT
  157. // ----------------------------------------------------------------------------
  158. struct VSOutput {
  159. float4 Position : POSITION;
  160. float Alpha : COLOR0;
  161. float2 TexCoord0 : TEXCOORD0;
  162. float2 ShroudTexCoord : TEXCOORD1;
  163. float3x3 TangentToViewSpace : TEXCOORD2;
  164. };
  165. // ----------------------------------------------------------------------------
  166. VSOutput VS(VSInputSkinningOneBoneTangentFrame InSkin,
  167. float2 TexCoord : TEXCOORD0,
  168. float4 VertexColor : COLOR0,
  169. uniform int numJointsPerVertex)
  170. {
  171. USE_DIRECTIONAL_LIGHT_INTERACTIVE(DirectionalLight, 0);
  172. VSOutput Out;
  173. float3 worldPosition = 0;
  174. float3 worldNormal = 0;
  175. float3 worldTangent = 0;
  176. float3 worldBinormal = 0;
  177. #if defined(_3DSMAX_)
  178. Time = Time * .2;
  179. VertexColor = float4(1,1,1,1);
  180. OpacityOverride = 1;
  181. #endif
  182. CalculatePositionAndTangentFrame(InSkin, numJointsPerVertex,
  183. worldPosition, worldNormal, worldTangent, worldBinormal);
  184. // transform position to projection space
  185. Out.Position = mul(float4(worldPosition, 1), GetViewProjection());
  186. float3 viewPosition = mul(float4(worldPosition, 1), View);
  187. // Compute view direction in world space
  188. float3 worldEyeDir = normalize(GetEyePosition() - worldPosition);
  189. float viewingAngle = abs(dot(worldEyeDir,normalize(worldNormal)));
  190. float fadeOut = 1;
  191. if (EdgeFadeOut >= 0)
  192. {
  193. fadeOut = smoothstep(0, EdgeFadeOut, viewingAngle); // Soften Edges
  194. }
  195. else
  196. {
  197. fadeOut = smoothstep(abs(EdgeFadeOut), 0, viewingAngle); // Soften Center
  198. }
  199. VertexColor.w = fadeOut;
  200. // Build 3x3 tranform from tangent to world space
  201. float3x3 tangentToWorldSpace = float3x3(-worldBinormal, -worldTangent, worldNormal);
  202. Out.TangentToViewSpace = mul(tangentToWorldSpace, (float3x3)View);
  203. Out.Alpha = VertexColor.w * OpacityOverride * DistortionStrength;
  204. // pass texture coordinates for fetching the diffuse and normal maps
  205. Out.TexCoord0.xy = TexCoord.xy * TexCoordTransform_0.xy + Time * TexCoordTransform_0.zw;
  206. // Calculate shroud texture coordinates
  207. Out.ShroudTexCoord = CalculateShroudTexCoord(Shroud, worldPosition);
  208. return Out;
  209. }
  210. // ----------------------------------------------------------------------------
  211. float4 PS(VSOutput In) : COLOR
  212. {
  213. float2 texCoord0 = In.TexCoord0;
  214. float4 normalMapSample = tex2D( SAMPLER(NormalMap), texCoord0);
  215. float3 bumpNormal = normalMapSample.xyz * 2.0 - 1.0; // Normalized
  216. bumpNormal.xy *= BumpScale; // Scale normal to increase/decrease bump effect
  217. bumpNormal = normalize(bumpNormal); // Normalized
  218. float3 normal = (mul(bumpNormal, In.TangentToViewSpace)) * .5 + .5; // Get geometery normal and normalize
  219. float4 color = float4(normal, In.Alpha * normalMapSample.w);
  220. #if !defined(_3DSMAX_)
  221. color.w *= tex2D( SAMPLER(ShroudTexture), In.ShroudTexCoord).w;
  222. #endif
  223. // return float4(In.Color.xyz * normal, In.Color.w * normalMapSample.w);
  224. return color;
  225. }
  226. // ----------------------------------------------------------------------------
  227. VSOutput VS_Xenon(
  228. VSInputSkinningOneBoneTangentFrame InSkin,
  229. float2 TexCoord : TEXCOORD0,
  230. float4 VertexColor : COLOR0 )
  231. {
  232. return VS( InSkin, TexCoord, VertexColor, min(NumJointsPerVertex, 1) );
  233. }
  234. // ----------------------------------------------------------------------------
  235. // TECHNIQUE: Default
  236. // ----------------------------------------------------------------------------
  237. #define VS_NumJointsPerVertex \
  238. compile VS_2_0 VS(0), \
  239. compile VS_2_0 VS(1)
  240. DEFINE_ARRAY_MULTIPLIER( VS_Multiplier_Final = 2 );
  241. #if SUPPORTS_SHADER_ARRAYS
  242. vertexshader VS_Array[VS_Multiplier_Final] =
  243. {
  244. VS_NumJointsPerVertex
  245. };
  246. #endif
  247. // ----------------------------------------------------------------------------
  248. // Technique: Default (Medium and up)
  249. // ----------------------------------------------------------------------------
  250. technique Default_M
  251. {
  252. pass p0
  253. <
  254. USE_EXPRESSION_EVALUATOR("DistortingObject")
  255. >
  256. {
  257. VertexShader = ARRAY_EXPRESSION_VS( VS_Array,
  258. min(NumJointsPerVertex, 1),
  259. compile VS_VERSION VS_Xenon()
  260. );
  261. PixelShader = compile PS_2_0 PS();
  262. ZEnable = true;
  263. ZFunc = ZFUNC_INFRONT;
  264. ZWriteEnable = ( ZWriteEnable );
  265. CullMode = ( CullingEnable ? D3DCULL_CW : D3DCULL_NONE );
  266. AlphaBlendEnable = true;
  267. SrcBlend = SrcAlpha;
  268. DestBlend = InvSrcAlpha;
  269. // As an optimization some objects specify the alpha test enable flag to throw away pixels.
  270. // This doesn't work on Xenon unless we implement a ShaderExpressionEvaluator for it,
  271. // and since it's only an optimization, leave it out for now.
  272. #if defined(EA_PLATFORM_XENON) || defined(EA_PLATFORM_PS3)
  273. AlphaTestEnable = false;
  274. #else
  275. AlphaTestEnable = ( AlphaTestEnable );
  276. AlphaFunc = GreaterEqual;
  277. AlphaRef = DEFAULT_ALPHATEST_THRESHOLD;
  278. #endif
  279. }
  280. }
  281. // ----------------------------------------------------------------------------
  282. // Technique: Default (Low)
  283. // ----------------------------------------------------------------------------
  284. #if !defined(_3DSMAX_) // 3DS Max crashes when a technique is empty
  285. technique Default_L
  286. {
  287. // No passes. Indicates technique disabled.
  288. }
  289. #endif // !defined(_3DSMAX_)