LineRenderers.fx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2005 Electronic Arts Inc
  3. //
  4. // FX Shader for rendering the shoreline
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include "Common.fxh"
  7. #include "Gamma.fxh"
  8. // Transformations
  9. float4x4 Projection : Projection;
  10. float Time : Time;
  11. SAMPLER_2D_BEGIN(Texture0,
  12. string UIWidget = "None";
  13. string SasBindAddress = "WW3D.MiscTexture";
  14. )
  15. MinFilter = Linear;
  16. MagFilter = Linear;
  17. MipFilter = Linear;
  18. AddressU = Wrap;
  19. AddressV = Wrap;
  20. SAMPLER_2D_END
  21. SAMPLER_2D_BEGIN(Texture1,
  22. string UIWidget = "None";
  23. string SasBindAddress = "WW3D.FXDistortionFractal";
  24. )
  25. MinFilter = Linear;
  26. MagFilter = Linear;
  27. MipFilter = Linear;
  28. AddressU = Wrap;
  29. AddressV = Wrap;
  30. SAMPLER_2D_END
  31. static const int BLEND_MODE_ALPHA = 0;
  32. static const int BLEND_MODE_ADDITIVE = 1;
  33. static const int BLEND_MODE_ADDITIVE_ALPHA_TEST = 2;
  34. static const int BLEND_MODE_ALPHA_TEST = 3;
  35. static const int BLEND_MODE_MULTIPLICATIVE = 4;
  36. static const int BLEND_MODE_ADDITIVE_NO_DEPTH_TEST = 5;
  37. static const int BLEND_MODE_ALPHA_NO_DEPTH_TEST = 6;
  38. static const int NUM_BLEND_MODES = 7;
  39. int BlendMode
  40. <
  41. string UIName = "Blend mode";
  42. int UIMin = 0;
  43. int UIMax = NUM_BLEND_MODES - 1;
  44. > = BLEND_MODE_ALPHA;
  45. struct VSOutput
  46. {
  47. float4 Position : POSITION;
  48. float2 BaseTexCoord : TEXCOORD0;
  49. float4 DiffuseColor : TEXCOORD1;
  50. };
  51. VSOutput VS(float3 Position : POSITION, float2 TexCoord0 : TEXCOORD0, float4 VertexColor : COLOR0)
  52. {
  53. VSOutput Out;
  54. // Note: The incoming geometry is already transformed to view space, so only apply Projection, not WorldViewProjection
  55. Out.Position = mul(float4(Position, 1), Projection);
  56. Out.DiffuseColor = VertexColor;
  57. Out.BaseTexCoord = TexCoord0;
  58. return Out;
  59. }
  60. float4 PS(VSOutput In, uniform bool applyGammaCorrection) : COLOR
  61. {
  62. // Get vertex color
  63. float4 color = In.DiffuseColor;
  64. // Apply texture
  65. float4 baseTexture = tex2D(SAMPLER(Texture0), In.BaseTexCoord);
  66. if (applyGammaCorrection)
  67. {
  68. baseTexture.xyz = GammaToLinear(baseTexture);
  69. }
  70. color *= baseTexture;
  71. return color;
  72. }
  73. //-------------------------------------------------------------------------------------------
  74. //-- Lightning Draw -------------------------------------------------------------------------
  75. //-------------------------------------------------------------------------------------------
  76. struct VSLightningOutput
  77. {
  78. float4 Position : POSITION;
  79. float2 BaseTexCoord : TEXCOORD0;
  80. float4 DistortionTexCoord : TEXCOORD1;
  81. float4 DiffuseColor : TEXCOORD2;
  82. };
  83. VSLightningOutput VSLightning(float3 Position : POSITION,
  84. float2 TexCoord0 : TEXCOORD0,
  85. float4 VertexColor : COLOR0)
  86. {
  87. VSLightningOutput Out;
  88. float DispScale = .1; //Displacement Coord Scale
  89. float DisplaceDivergenceAngle = 45 * .017453; //Rotation difference in degrees, convert to radians
  90. float DisplaceSpeed = 6; //Speed of scolling displacement texture
  91. // Build Displace Texture Rotation Matrix -----
  92. float cosAngle, sinAngle;
  93. cosAngle = 0;
  94. sinAngle = 0;
  95. float2x2 uvCoordRotate = { 1.0f, 0.0f, 1.0f, 0.0f };
  96. sincos (DisplaceDivergenceAngle, sinAngle, cosAngle);
  97. uvCoordRotate[0][0] = cosAngle;
  98. uvCoordRotate[0][1] = -sinAngle;
  99. uvCoordRotate[1][1] = uvCoordRotate[0][0];
  100. uvCoordRotate[1][0] = -uvCoordRotate[0][1];
  101. // Texture coordinates for lighting have the y channel be a "world space distance" from the origin of the lighting,
  102. // so at the beginning y is 0, at the end it is eg 120 for one of our largest lightning weapons.
  103. // This can be used to use only a partial lightning texture for short lightning bolts.
  104. // For now we assume our lightning texture has been scaled to look "good" for the longest bolt of 120 length.
  105. static const float maxLightningLength = 160;
  106. TexCoord0.y /= maxLightningLength;
  107. float2 DisplaceTexCoords = TexCoord0 * DispScale;
  108. DisplaceTexCoords.y *= 3;
  109. // Rotate and Animate Displace Divergence Texture Coords --------------------
  110. float2 DisplaceTexCoordsDiverge = mul(DisplaceTexCoords, uvCoordRotate);
  111. DisplaceTexCoordsDiverge.x += float(DisplaceSpeed * Time * .005);
  112. float2 DisplaceTexCoordsDivergeInv = mul(DisplaceTexCoords * .5, transpose(uvCoordRotate));
  113. DisplaceTexCoordsDivergeInv.x += float(DisplaceSpeed * Time * .01);
  114. // Note: The incoming geometry is already transformed to view space, so only apply Projection, not WorldViewProjection
  115. Out.Position = mul(float4(Position, 1), Projection);
  116. Out.DiffuseColor = VertexColor;
  117. Out.BaseTexCoord = TexCoord0;
  118. Out.DistortionTexCoord = float4(DisplaceTexCoordsDiverge, DisplaceTexCoordsDivergeInv);
  119. return Out;
  120. }
  121. float4 PSLightning(VSLightningOutput In, uniform bool applyGammaCorrection) : COLOR
  122. {
  123. // Get vertex color
  124. float4 color = In.DiffuseColor;
  125. //Setup for Lightning
  126. float DispAmp = .01; //Displacement amplitude
  127. // Get distortion coords, normalize and apply
  128. float2 textureDisplace = tex2D(SAMPLER(Texture1), In.DistortionTexCoord.xy);
  129. textureDisplace += tex2D(SAMPLER(Texture1), In.DistortionTexCoord.zw);
  130. textureDisplace = textureDisplace - 1;
  131. float2 displacedTextureCoords = In.BaseTexCoord.xy + (textureDisplace.xy * DispAmp);
  132. float4 baseTexture = tex2D(SAMPLER(Texture0), displacedTextureCoords);
  133. if (applyGammaCorrection)
  134. {
  135. baseTexture.xyz = GammaToLinear(baseTexture);
  136. color *= 2; // This multiply without HDR looks bad. MJ
  137. }
  138. color *= baseTexture;
  139. return color;
  140. }
  141. //
  142. // High - UltraHigh techniques
  143. //
  144. technique Alpha
  145. {
  146. pass P0
  147. {
  148. VertexShader = compile VS_2_0 VS();
  149. PixelShader = compile PS_2_0 PS(true);
  150. ZEnable = true;
  151. ZFunc = ZFUNC_INFRONT;
  152. ZWriteEnable = false;
  153. CullMode = None;
  154. AlphaBlendEnable = true;
  155. SrcBlend = SrcAlpha;
  156. DestBlend = InvSrcAlpha;
  157. AlphaTestEnable = false;
  158. }
  159. }
  160. technique Additive
  161. {
  162. pass P0
  163. {
  164. VertexShader = compile VS_2_0 VS();
  165. PixelShader = compile PS_2_0 PS(true);
  166. ZEnable = true;
  167. ZFunc = ZFUNC_INFRONT;
  168. ZWriteEnable = false;
  169. CullMode = None;
  170. AlphaBlendEnable = true;
  171. SrcBlend = One;
  172. DestBlend = One;
  173. AlphaTestEnable = false;
  174. }
  175. }
  176. // Hi-jacked for Lightning Renderer
  177. technique AdditiveAlphaTest
  178. {
  179. pass P0
  180. {
  181. VertexShader = compile VS_2_0 VSLightning();
  182. PixelShader = compile PS_2_0 PSLightning(true);
  183. ZEnable = true;
  184. ZFunc = ZFUNC_INFRONT;
  185. ZWriteEnable = false;
  186. CullMode = None;
  187. AlphaBlendEnable = true;
  188. SrcBlend = One;
  189. DestBlend = One;
  190. AlphaTestEnable = false;
  191. }
  192. }
  193. technique AlphaTest
  194. {
  195. pass P0
  196. {
  197. VertexShader = compile VS_2_0 VS();
  198. PixelShader = compile PS_2_0 PS(true);
  199. ZEnable = true;
  200. ZFunc = ZFUNC_INFRONT;
  201. ZWriteEnable = false;
  202. CullMode = None;
  203. AlphaBlendEnable = false;
  204. AlphaTestEnable = true;
  205. AlphaFunc = GreaterEqual;
  206. AlphaRef = DEFAULT_ALPHATEST_THRESHOLD;
  207. }
  208. }
  209. technique Multiplicative
  210. {
  211. pass P0
  212. {
  213. VertexShader = compile VS_2_0 VS();
  214. PixelShader = compile PS_2_0 PS(true);
  215. ZEnable = true;
  216. ZFunc = ZFUNC_INFRONT;
  217. ZWriteEnable = false;
  218. CullMode = None;
  219. AlphaBlendEnable = true;
  220. SrcBlend = DestColor;
  221. DestBlend = Zero;
  222. AlphaTestEnable = false;
  223. }
  224. }
  225. technique AdditiveNoDepthTest
  226. {
  227. pass P0
  228. {
  229. VertexShader = compile VS_2_0 VS();
  230. PixelShader = compile PS_2_0 PS(true);
  231. ZEnable = false;
  232. ZWriteEnable = false;
  233. CullMode = None;
  234. AlphaBlendEnable = true;
  235. SrcBlend = One;
  236. DestBlend = One;
  237. AlphaTestEnable = false;
  238. }
  239. }
  240. technique AlphaNoDepthTest
  241. {
  242. pass P0
  243. {
  244. VertexShader = compile VS_2_0 VS();
  245. PixelShader = compile PS_2_0 PS(true);
  246. ZEnable = false;
  247. ZWriteEnable = false;
  248. CullMode = None;
  249. AlphaBlendEnable = true;
  250. SrcBlend = SrcAlpha;
  251. DestBlend = InvSrcAlpha;
  252. AlphaTestEnable = false;
  253. }
  254. }
  255. //
  256. // Medium techniques
  257. //
  258. technique Alpha_M
  259. {
  260. pass P0
  261. {
  262. VertexShader = compile VS_2_0 VS();
  263. PixelShader = compile PS_2_0 PS(false);
  264. ZEnable = true;
  265. ZFunc = ZFUNC_INFRONT;
  266. ZWriteEnable = false;
  267. CullMode = None;
  268. AlphaBlendEnable = true;
  269. SrcBlend = SrcAlpha;
  270. DestBlend = InvSrcAlpha;
  271. AlphaTestEnable = false;
  272. }
  273. }
  274. technique Additive_M
  275. {
  276. pass P0
  277. {
  278. VertexShader = compile VS_2_0 VS();
  279. PixelShader = compile PS_2_0 PS(false);
  280. ZEnable = true;
  281. ZFunc = ZFUNC_INFRONT;
  282. ZWriteEnable = false;
  283. CullMode = None;
  284. AlphaBlendEnable = true;
  285. SrcBlend = One;
  286. DestBlend = One;
  287. AlphaTestEnable = false;
  288. }
  289. }
  290. // Hi-jacked for Lightning Renderer
  291. technique AdditiveAlphaTest_M
  292. {
  293. pass P0
  294. {
  295. VertexShader = compile VS_2_0 VSLightning();
  296. PixelShader = compile PS_2_0 PSLightning(false);
  297. ZEnable = true;
  298. ZFunc = ZFUNC_INFRONT;
  299. ZWriteEnable = false;
  300. CullMode = None;
  301. AlphaBlendEnable = true;
  302. SrcBlend = One;
  303. DestBlend = One;
  304. AlphaTestEnable = false;
  305. }
  306. }
  307. technique AlphaTest_M
  308. {
  309. pass P0
  310. {
  311. VertexShader = compile VS_2_0 VS();
  312. PixelShader = compile PS_2_0 PS(false);
  313. ZEnable = true;
  314. ZFunc = ZFUNC_INFRONT;
  315. ZWriteEnable = false;
  316. CullMode = None;
  317. AlphaBlendEnable = false;
  318. AlphaTestEnable = true;
  319. AlphaFunc = GreaterEqual;
  320. AlphaRef = DEFAULT_ALPHATEST_THRESHOLD;
  321. }
  322. }
  323. technique Multiplicative_M
  324. {
  325. pass P0
  326. {
  327. VertexShader = compile VS_2_0 VS();
  328. PixelShader = compile PS_2_0 PS(false);
  329. ZEnable = true;
  330. ZFunc = ZFUNC_INFRONT;
  331. ZWriteEnable = false;
  332. CullMode = None;
  333. AlphaBlendEnable = true;
  334. SrcBlend = DestColor;
  335. DestBlend = Zero;
  336. AlphaTestEnable = false;
  337. }
  338. }
  339. technique AdditiveNoDepthTest_M
  340. {
  341. pass P0
  342. {
  343. VertexShader = compile VS_2_0 VS();
  344. PixelShader = compile PS_2_0 PS(false);
  345. ZEnable = false;
  346. ZWriteEnable = false;
  347. CullMode = None;
  348. AlphaBlendEnable = true;
  349. SrcBlend = One;
  350. DestBlend = One;
  351. AlphaTestEnable = false;
  352. }
  353. }
  354. technique AlphaNoDepthTest_M
  355. {
  356. pass P0
  357. {
  358. VertexShader = compile VS_2_0 VS();
  359. PixelShader = compile PS_2_0 PS(false);
  360. ZEnable = false;
  361. ZWriteEnable = false;
  362. CullMode = None;
  363. AlphaBlendEnable = true;
  364. SrcBlend = SrcAlpha;
  365. DestBlend = InvSrcAlpha;
  366. AlphaTestEnable = false;
  367. }
  368. }
  369. // ----------------------------------------------------------------------------
  370. // SHADER: CreateShadowMapStreakVS
  371. // ----------------------------------------------------------------------------
  372. struct VSOutput_CreateShadowMap
  373. {
  374. float4 Position : POSITION;
  375. float4 Color : COLOR0;
  376. float2 BaseTexCoord : TEXCOORD0;
  377. float Depth : TEXCOORD1;
  378. };
  379. // ----------------------------------------------------------------------------
  380. VSOutput_CreateShadowMap CreateShadowMapVS(float3 Position : POSITION,
  381. float3 Normal : NORMAL, float2 TexCoord0 : TEXCOORD0, float4 VertexColor: COLOR0)
  382. {
  383. VSOutput_CreateShadowMap Out;
  384. // Note: The incoming geometry is already transformed to view space, so only apply Projection, not WorldViewProjection
  385. Out.Position = mul(float4(Position, 1), Projection);
  386. Out.Color = VertexColor;
  387. Out.BaseTexCoord = TexCoord0;
  388. Out.Depth = Out.Position.z / Out.Position.w;
  389. return Out;
  390. }
  391. // ----------------------------------------------------------------------------
  392. float4 CreateShadowMapPS(VSOutput_CreateShadowMap In, uniform int blendMode) COLORTARGET
  393. {
  394. float4 color = In.Color;
  395. color *= tex2D(SAMPLER(Texture0), In.BaseTexCoord);
  396. float clipValue;
  397. if (blendMode == BLEND_MODE_ALPHA || blendMode == BLEND_MODE_ALPHA_TEST || blendMode == BLEND_MODE_ADDITIVE_ALPHA_TEST || blendMode == BLEND_MODE_ALPHA_NO_DEPTH_TEST)
  398. {
  399. // Simulate alpha testing for floating point render target
  400. clipValue = color.w - ((float)90 / 255);
  401. }
  402. else // if (blendMode == BLEND_MODE_ADDITIVE || blendMode == BLEND_MODE_ADDITIVE_NO_DEPTH_TEST || blendMode == BLEND_MODE_MULTIPLICATIVE)
  403. {
  404. // Simulate additive "alpha testing" for floating point render target
  405. clipValue = dot(color.xyz, float3(1, 1, 1)) - 3 * ((float)200 / 255);
  406. }
  407. clip(clipValue);
  408. return In.Depth;
  409. }
  410. float4 CreateShadowMapPS_Xenon(VSOutput_CreateShadowMap In) : COLOR
  411. {
  412. return CreateShadowMapPS(In, BlendMode);
  413. }
  414. // ----------------------------------------------------------------------------
  415. // TECHNIQUE: _CreateShadowMap
  416. // ----------------------------------------------------------------------------
  417. DEFINE_ARRAY_MULTIPLIER(PSCreateShadowMap_Multiplier_BlendMode = 1);
  418. #define PSCreateShadowMap_BlendMode \
  419. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ALPHA), \
  420. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ADDITIVE), \
  421. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ADDITIVE_ALPHA_TEST), \
  422. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ALPHA_TEST), \
  423. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_MULTIPLICATIVE), \
  424. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ADDITIVE_NO_DEPTH_TEST), \
  425. compile PS_2_0 CreateShadowMapPS(BLEND_MODE_ALPHA_NO_DEPTH_TEST)
  426. DEFINE_ARRAY_MULTIPLIER(PSCreateShadowMap_Multiplier_Final = 7 * PSCreateShadowMap_Multiplier_BlendMode);
  427. #if SUPPORTS_SHADER_ARRAYS
  428. pixelshader PSCreateShadowMap_Array[PSCreateShadowMap_Multiplier_Final] =
  429. {
  430. PSCreateShadowMap_BlendMode
  431. };
  432. #endif
  433. technique _CreateShadowMap
  434. {
  435. pass P0
  436. {
  437. VertexShader = compile VS_2_0 CreateShadowMapVS();
  438. PixelShader = ARRAY_EXPRESSION_DIRECT_PS(PSCreateShadowMap_Array,
  439. BlendMode * PSCreateShadowMap_Multiplier_BlendMode,
  440. // Non-array alternative:
  441. compile PS_VERSION CreateShadowMapPS_Xenon()
  442. );
  443. ZEnable = true;
  444. ZFunc = ZFUNC_INFRONT;
  445. ZWriteEnable = true;
  446. CullMode = None;
  447. AlphaBlendEnable = false;
  448. AlphaTestEnable = false; // Handled in pixel shader
  449. }
  450. }