FXLightning.fx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2008 Electronic Arts Inc
  3. //
  4. // FX Shader for Lightning FX (unlit, 2 texture solution, assumed additive)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #define SUPPORT_RECOLORING 1
  7. #define SUPPORT_FOG 1
  8. //#define SUPPORT_PROTONCOLLIDER 1
  9. #include "Common.fxh"
  10. #include "Gamma.fxh"
  11. #include "Random.fxh"
  12. #if defined(EA_PLATFORM_WINDOWS) && defined(_3DSMAX_)
  13. // ----------------------------------------------------------------------------
  14. // SAMPLER : nhendricks : had to pull these in here for MAX to compile
  15. // ----------------------------------------------------------------------------
  16. #define SAMPLER_2D_BEGIN(samplerName, annotations) \
  17. texture samplerName \
  18. < \
  19. annotations \
  20. >; \
  21. sampler2D samplerName##Sampler = sampler_state \
  22. { \
  23. Texture = < samplerName >;
  24. #define SAMPLER_2D_END };
  25. #define SAMPLER( samplerName ) samplerName##Sampler
  26. #define SAMPLER_CUBE_BEGIN(samplerName, annotations) \
  27. texture samplerName \
  28. < \
  29. annotations \
  30. >; \
  31. samplerCUBE samplerName##Sampler = sampler_state \
  32. { \
  33. Texture = < samplerName >;
  34. #define SAMPLER_CUBE_END };
  35. #endif
  36. // ----------------------------------------------------------------------------
  37. // Skinning
  38. // ----------------------------------------------------------------------------
  39. static const int MaxSkinningBonesPerVertex = 2;
  40. #include "Skinning.fxh"
  41. // ----------------------------------------------------------------------------
  42. // Transformations
  43. // ----------------------------------------------------------------------------
  44. #if defined(_WW3D_)
  45. #if !defined(USE_INDIRECT_CONSTANT)
  46. float4x4 ViewProjection
  47. <
  48. string UIWidget = "None";
  49. string SasBindAddress = "Sas.Camera.WorldToProjection";
  50. >;
  51. float3 EyePosition
  52. <
  53. string UIWidget = "None";
  54. string SasBindAddress = "Sas.Camera.Position";
  55. >;
  56. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  57. float4x4 GetViewProjection()
  58. {
  59. return ViewProjection;
  60. }
  61. float3 GetEyePosition()
  62. {
  63. return EyePosition;
  64. }
  65. #else // #if defined(_WW3D_)
  66. float4x4 View : View;
  67. float4x3 ViewI : ViewInverse;
  68. float4x4 Projection : Projection;
  69. float4x4 GetViewProjection()
  70. {
  71. return mul(View, Projection);
  72. }
  73. float3 GetEyePosition()
  74. {
  75. return ViewI[3];
  76. }
  77. #endif // #if defined(_WW3D_)
  78. float Time : Time;
  79. // ----------------------------------------------------------------------------
  80. // Material parameters
  81. // ----------------------------------------------------------------------------
  82. SAMPLER_2D_BEGIN( Texture_0,
  83. string UIName = "Diffuse Texture";
  84. )
  85. MinFilter = MinFilterBest;
  86. MagFilter = MagFilterBest;
  87. MipFilter = MipFilterBest;
  88. AddressU = Wrap;
  89. AddressV = Wrap;
  90. SAMPLER_2D_END
  91. float3 ColorDiffuse
  92. <
  93. string UIName = "Diffuse Material Color";
  94. string UIWidget = "Color";
  95. > = float3(1.0, 1.0, 1.0);
  96. float EmissiveHDRMultipler
  97. <
  98. string UIName = "Emissive HDR Multiplier";
  99. string UIWidget = "Slider";
  100. float UIMax = 200;
  101. > = 1.0;
  102. bool MultiTextureEnable
  103. <
  104. string UIName = "Multi-Texture Enable";
  105. > = false;
  106. float4 DiffuseCoordOffset
  107. <
  108. string UIName = "Diffuse Coord Offset/Scale";
  109. string UIWidget = "Slider";
  110. float UIMax = 1.0f;
  111. float UIMin = 0;
  112. float UIStep = 0.001f;
  113. > = float4(0.00, 0.00, 1.0, 1.0);
  114. #if !defined(SUPPORT_PROTONCOLLIDER)
  115. bool MultiplyBlendEnable
  116. <
  117. string UIName = "Multiply Blend Enable";
  118. > = false;
  119. float EdgeFadeOut
  120. <
  121. string UIName = "Edge fade out";
  122. string UIWidget = "Slider";
  123. > = 0.0f;
  124. #endif //SUPPORT_PROTONCOLLIDER
  125. // ----------------------------------------------------------------------------
  126. // Displace Mapping
  127. // ----------------------------------------------------------------------------
  128. SAMPLER_2D_BEGIN( Texture_1,
  129. string UIName = "Displace Texture";
  130. )
  131. MinFilter = MinFilterBest;
  132. MagFilter = MagFilterBest;
  133. MipFilter = MipFilterBest;
  134. AddressU = Wrap;
  135. AddressV = Wrap;
  136. SAMPLER_2D_END
  137. bool UniqueWorldCoordEnable
  138. <
  139. string UIName = "Unique World Coords Enable";
  140. > = false;
  141. float UniqueWorldCoordScalar
  142. <
  143. string UIName = "Unique World Coords Scale";
  144. string UIWidget = "Slider";
  145. float UIMax = 1.0f;
  146. float UIMin = 0;
  147. float UIStep = 0.001f;
  148. > = 0.01;
  149. float UniqueWorldCoordStrength
  150. <
  151. string UIName = "Unique World Coords Strength";
  152. string UIWidget = "Slider";
  153. float UIMax = 1.0f;
  154. float UIMin = 0;
  155. float UIStep = 0.01f;
  156. > = 1.0;
  157. float DisplaceScalar
  158. <
  159. string UIName = "Displace Scale";
  160. string UIWidget = "Slider";
  161. float UIMax = 50.0f;
  162. float UIMin = 0;
  163. float UIStep = 0.01f;
  164. > = 1.0;
  165. float DisplaceAmp
  166. <
  167. string UIName = "Displace Amplitude";
  168. string UIWidget = "Slider";
  169. float UIMax = 50.0f;
  170. float UIMin = 0;
  171. float UIStep = 0.01f;
  172. > = 1.0;
  173. float DisplaceDivergenceAngle
  174. <
  175. string UIName = "Displace Divergence Angle";
  176. string UIWidget = "Slider";
  177. float UIMax = 180.0f;
  178. float UIMin = 0;
  179. float UIStep = 0.5f;
  180. > = 0.0;
  181. float DisplaceSpeed
  182. <
  183. string UIName = "Displace Speed";
  184. string UIWidget = "Slider";
  185. float UIMax = 50.0f;
  186. float UIMin = 0;
  187. float UIStep = 0.01f;
  188. > = 1.0;
  189. #if defined(SUPPORT_PROTONCOLLIDER)
  190. // ----------------------------------------------------------------------------
  191. // Mask Mapping for Proton Collider FX
  192. // ----------------------------------------------------------------------------
  193. SAMPLER_2D_BEGIN( Texture_2,
  194. string UIName = "Mask Texture";
  195. )
  196. MinFilter = MinFilterBest;
  197. MagFilter = MagFilterBest;
  198. MipFilter = MipFilterBest;
  199. AddressU = Clamp;
  200. AddressV = Clamp;
  201. SAMPLER_2D_END
  202. bool UV2Enable
  203. <
  204. string UIName = "Use UV Channel 2 for Mask";
  205. > = false;
  206. #endif
  207. // ----------------------------------------------------------------------------
  208. // House coloring
  209. // ----------------------------------------------------------------------------
  210. bool UseRecolorColors
  211. <
  212. string UIName = "Allow House Color";
  213. > = false;
  214. bool CullingEnable
  215. <
  216. string UIName = "Culling Enable";
  217. > = true;
  218. #if !defined(USE_INDIRECT_CONSTANT)
  219. float OpacityOverride
  220. <
  221. string UIWidget = "None";
  222. string SasBindAddress = "WW3D.OpacityOverride";
  223. > = 1.0;
  224. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  225. // ----------------------------------------------------------------------------
  226. // SHADER : DEFAULT
  227. // ----------------------------------------------------------------------------
  228. struct VSOutput
  229. {
  230. float4 Position : POSITION;
  231. float4 DiffuseColor : TEXCOORD0;
  232. float4 DiffuseTexCoord : TEXCOORD1;
  233. float4 DisplaceTexCoord : TEXCOORD2;
  234. #if defined(SUPPORT_PROTONCOLLIDER)
  235. float3 MaskTexCoord : TEXCOORD3;
  236. #endif
  237. float Fog : TEXCOORD4;
  238. };
  239. // ----------------------------------------------------------------------------
  240. VSOutput VS(VSInputSkinningMultipleBones InSkin,
  241. float2 TexCoord0 : TEXCOORD0,
  242. float2 TexCoord1 : TEXCOORD1,
  243. float4 VertexColor: COLOR0,
  244. uniform int numJointsPerVertex)
  245. {
  246. VSOutput Out;
  247. float3 worldPosition = 0;
  248. float3 worldNormal = 0;
  249. CalculatePositionAndNormal(InSkin, numJointsPerVertex, worldPosition, worldNormal);
  250. #if defined(_3DSMAX_)
  251. // Default vertex color is 0 in Max, that's bad.
  252. VertexColor = 1.0;
  253. // A little bit better motion previewing in MAX
  254. Time *= .25;
  255. #endif
  256. //Calculate the displace texture coordinates
  257. DisplaceSpeed *= Time * .01; // animate Displace as a multiplier of Time with a a happy MAX modifier
  258. //This is the wrong way, but there have been too many FX based on this to fix it now
  259. float2 DisplaceTexCoords = TexCoord0 * DisplaceScalar;
  260. if (UniqueWorldCoordEnable == true )
  261. {
  262. #if defined(SUPPORT_PROTONCOLLIDER)
  263. // This is the right way
  264. DisplaceTexCoords.xy = (worldPosition.xy * UniqueWorldCoordScalar * .01) * UniqueWorldCoordStrength; //the ".01" helps max with a small spinner
  265. #else
  266. //This is the wrong way, but there have been too many FX based on this to fix it now
  267. DisplaceTexCoords.y += (worldPosition.y + worldPosition.x) * UniqueWorldCoordScalar * .1; //the ".1" helps max with a small spinner
  268. #endif
  269. }
  270. // Build Displace Texture Rotation Matrix And Convert Degrees to Radians -----
  271. float cosAngle, sinAngle;
  272. cosAngle = 0;
  273. sinAngle = 0;
  274. float2x2 uvCoordRotate = { 1.0f, 0.0f, 1.0f, 0.0f };
  275. sincos (DisplaceDivergenceAngle * .017453, sinAngle, cosAngle);
  276. uvCoordRotate[0][0] = cosAngle;
  277. uvCoordRotate[0][1] = -sinAngle;
  278. uvCoordRotate[1][1] = uvCoordRotate[0][0];
  279. uvCoordRotate[1][0] = -uvCoordRotate[0][1];
  280. // Rotate and Animate Displace Divergence Texture Coords --------------------
  281. float2 DisplaceTexCoordsDiverge = mul(DisplaceTexCoords, uvCoordRotate);
  282. DisplaceTexCoordsDiverge.x += DisplaceSpeed;
  283. float2 DisplaceTexCoordsDivergeInv = mul(DisplaceTexCoords, transpose(uvCoordRotate));
  284. DisplaceTexCoordsDivergeInv.x += DisplaceSpeed;
  285. // Compute view direction in world space
  286. float3 worldEyeDir = normalize(GetEyePosition() - worldPosition);
  287. float viewingAngle = abs(dot(worldEyeDir,worldNormal));
  288. #if !defined(SUPPORT_PROTONCOLLIDER)
  289. float fadeOut = smoothstep(0, EdgeFadeOut, viewingAngle);
  290. VertexColor.w *= fadeOut;
  291. VertexColor.xyz *= OpacityOverride; // This is a hack, the particle system can only adjust OpacityOverride so we use that for now. MJ
  292. #endif //SUPPORT_PROTONCOLLIDER
  293. // Compute all registers
  294. Out.Position = mul(float4(worldPosition, 1), GetViewProjection());
  295. Out.DiffuseColor = VertexColor * float4(ColorDiffuse,1);
  296. #if !defined(SUPPORT_PROTONCOLLIDER)
  297. if (MultiplyBlendEnable == true )
  298. {
  299. Out.DiffuseColor = VertexColor * float4(1 - ColorDiffuse,1);
  300. }
  301. #endif //SUPPORT_PROTONCOLLIDER
  302. TexCoord0.xy *= DiffuseCoordOffset.zw;
  303. // Restrict the numerical range to 0..1 to hide precision issues in the texture sampling when Time goes really large
  304. float2 offset = frac(DiffuseCoordOffset.xy * Time);
  305. Out.DiffuseTexCoord.xyzw = TexCoord0.xyxy + offset.xyxy;
  306. if (MultiTextureEnable == true )
  307. {
  308. Out.DiffuseTexCoord.zw = TexCoord0.xy * DiffuseCoordOffset.zw - offset;
  309. }
  310. Out.DisplaceTexCoord = float4(DisplaceTexCoordsDiverge, DisplaceTexCoordsDivergeInv);
  311. Out.Fog = CalculateFog(Fog, worldPosition, GetEyePosition());
  312. #if defined(SUPPORT_PROTONCOLLIDER)
  313. Out.MaskTexCoord.xy = TexCoord1.xy;
  314. Out.MaskTexCoord.z = (OpacityOverride * 3)- 1;
  315. #endif
  316. return Out;
  317. }
  318. // ----------------------------------------------------------------------------
  319. float4 PS(VSOutput In) : COLOR
  320. {
  321. float4 color = In.DiffuseColor * EmissiveHDRMultipler;
  322. color *= In.DiffuseColor.w;
  323. float fogStrength = In.Fog;
  324. float3 textureDisplace = tex2D( SAMPLER(Texture_1), In.DisplaceTexCoord.xy);
  325. textureDisplace += tex2D( SAMPLER(Texture_1), In.DisplaceTexCoord.zw);
  326. textureDisplace = textureDisplace - 1;
  327. float4 displacedTextureCoords = In.DiffuseTexCoord + (textureDisplace.xyxy * DisplaceAmp);
  328. float4 textures = tex2D( SAMPLER(Texture_0), displacedTextureCoords.xy);
  329. if (MultiTextureEnable)
  330. {
  331. textures *= tex2D( SAMPLER(Texture_0), displacedTextureCoords.zw);
  332. }
  333. textures.xyz = GammaToLinear(textures.xyz);
  334. color *= textures;
  335. #if !defined(SUPPORT_PROTONCOLLIDER)
  336. if (MultiplyBlendEnable)
  337. {
  338. color = .5 - color;
  339. }
  340. #endif //SUPPORT_PROTONCOLLIDER
  341. // Apply house color
  342. if (UseRecolorColors)
  343. {
  344. color.xyz *= lerp(RecolorColor, float3(1,1,1), .35);
  345. }
  346. #if defined(SUPPORT_PROTONCOLLIDER)
  347. color.w = 1;
  348. float3 maskDiffuse = tex2D( SAMPLER(Texture_2), (In.DiffuseTexCoord.xy - In.MaskTexCoord.z));
  349. if (UV2Enable)
  350. {
  351. maskDiffuse = tex2D( SAMPLER(Texture_2), (In.MaskTexCoord.xy - In.MaskTexCoord.z));
  352. }
  353. #if !defined(_3DSMAX_)
  354. color.xyz *= maskDiffuse; // MAX does not play well with multiple textures
  355. #endif
  356. #endif //SUPPORT_PROTONCOLLIDER
  357. // Overbrighten
  358. color.xyz += color.xyz;
  359. // Fog used with additive blending just needs to reduce the additive influence, not blend towards the fog color
  360. color.xyz *= fogStrength;
  361. return color;
  362. }
  363. // ----------------------------------------------------------------------------
  364. // Technique: Default
  365. // ----------------------------------------------------------------------------
  366. technique Default
  367. {
  368. pass P0
  369. {
  370. VertexShader = compile VS_2_0 VS(0);
  371. PixelShader = compile PS_2_0 PS();
  372. ZEnable = true;
  373. ZFunc = ZFUNC_INFRONT;
  374. ZWriteEnable = false;
  375. AlphaTestEnable = false;
  376. AlphaBlendEnable = true;
  377. #if defined(EA_PLATFORM_WINDOWS)
  378. CullMode = ( CullingEnable ? D3DCULL_CW : D3DCULL_NONE );
  379. #if !defined(SUPPORT_PROTONCOLLIDER)
  380. // Additive or Multiply Blend Mode
  381. SrcBlend = ( MultiplyBlendEnable == true ? D3DBLEND_DESTCOLOR : D3DBLEND_ONE );
  382. DestBlend = ( MultiplyBlendEnable == true ? D3DBLEND_ZERO : D3DBLEND_ONE );
  383. #else
  384. // Force Additive for SUPPORT_PROTONCOLLIDER
  385. SrcBlend = One;
  386. DestBlend = One;
  387. #endif //SUPPORT_PROTONCOLLIDER
  388. #else
  389. CullMode = None;
  390. // Additive or Multiply Blend Mode
  391. SrcBlend = One;
  392. DestBlend = One;
  393. #endif
  394. }
  395. }