Outlines.fx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2006 Electronic Arts Inc
  3. //
  4. // FX Shader for object outlines (selection and hovering)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include "Common.fxh"
  7. #include "CommonPostFX.fxh"
  8. // we use the first three bits in the stencil buffer for this effect
  9. // THESE MUST BE KEPT IN SYNC WITH CODE!
  10. #define STENCIL_BITS_SELECTED 0x1
  11. #define STENCIL_BITS_HOVER_SELECT 0x2
  12. #define STENCIL_BITS_HOVER_ATTACK 0x3
  13. // ----------------------------------------------------------------------------
  14. // Constants
  15. // ----------------------------------------------------------------------------
  16. // ngavalas
  17. // This controls the distance to sample from the center pixel being blurred.
  18. // It needs to be on a border of 4 pixels so we can get more sampling for free.
  19. // 0.5 and 1.5 work well. Please do not change this without consulting me.
  20. static const float TEXEL_DISTANCE_FROM_CENTER = 1.5f;
  21. static const float4 OutlineSelectedColor = float4(1.0f, 0.713f, 0.0f, 1.0f);
  22. static const float4 OutlineHoverSelectColor = float4(1.0f, 1.0f, 1.0f, 1.0f);
  23. static const float4 OutlineHoverAttackColor = float4(1.0f, 0.0f, 0.0f, 1.0f);
  24. static const float OutlineIntensity = 5.0f;
  25. // ----------------------------------------------------------------------------
  26. SAMPLER_2D_BEGIN( PostEffectOutlineTexture,
  27. string SasBindAddress = "WW3D.Outline.Texture";
  28. )
  29. MinFilter = Linear;
  30. MagFilter = Linear;
  31. MipFilter = Point;
  32. AddressU = Clamp;
  33. AddressV = Clamp;
  34. SAMPLER_2D_END
  35. float2 FrameBufferSize
  36. <
  37. string UIWidget = "None";
  38. string SasBindAddress = "WW3D.FrameBufferSize";
  39. > = float2(1.0f, 1.0f);
  40. // ----------------------------------------------------------------------------
  41. // SHADERS
  42. // ----------------------------------------------------------------------------
  43. float4 VS(float3 Position : POSITION) : POSITION
  44. {
  45. return float4(Position, 1);
  46. }
  47. // ----------------------------------------------------------------------------
  48. float4 PS_Selected(void) : COLOR
  49. {
  50. return OutlineSelectedColor;
  51. }
  52. // ----------------------------------------------------------------------------
  53. float4 PS_HoverSelect(void) : COLOR
  54. {
  55. return OutlineHoverSelectColor;
  56. }
  57. // ----------------------------------------------------------------------------
  58. float4 PS_HoverAttack(void) : COLOR
  59. {
  60. return OutlineHoverAttackColor;
  61. }
  62. // ----------------------------------------------------------------------------
  63. // TECHNIQUE: FillSelected
  64. // ----------------------------------------------------------------------------
  65. technique FillSelected
  66. {
  67. pass P0
  68. {
  69. VertexShader = compile VS_2_0 VS();
  70. PixelShader = compile PS_2_0 PS_Selected();
  71. ZEnable = false;
  72. ZWriteEnable = false;
  73. CullMode = None;
  74. StencilEnable = true;
  75. StencilFunc = Equal;
  76. StencilPass = Keep;
  77. StencilZFail = Keep;
  78. StencilFail = Keep;
  79. StencilMask = STENCIL_BITS_SELECTED;
  80. StencilRef = STENCIL_BITS_SELECTED;
  81. AlphaBlendEnable = false;
  82. AlphaTestEnable = false;
  83. }
  84. }
  85. // ----------------------------------------------------------------------------
  86. // TECHNIQUE: FillHoverSelect
  87. // ----------------------------------------------------------------------------
  88. technique FillHoverSelect
  89. {
  90. pass P0
  91. {
  92. VertexShader = compile VS_2_0 VS();
  93. PixelShader = compile PS_2_0 PS_HoverSelect();
  94. ZEnable = false;
  95. ZWriteEnable = false;
  96. CullMode = None;
  97. AlphaBlendEnable = false;
  98. AlphaTestEnable = false;
  99. StencilEnable = true;
  100. StencilFunc = Equal;
  101. StencilPass = Keep;
  102. StencilZFail = Keep;
  103. StencilFail = Keep;
  104. StencilMask = STENCIL_BITS_HOVER_SELECT;
  105. StencilRef = STENCIL_BITS_HOVER_SELECT;
  106. }
  107. }
  108. // ----------------------------------------------------------------------------
  109. // TECHNIQUE: FillHoverAttack
  110. // ----------------------------------------------------------------------------
  111. technique FillHoverAttack
  112. {
  113. pass P0
  114. {
  115. VertexShader = compile VS_2_0 VS();
  116. PixelShader = compile PS_2_0 PS_HoverAttack();
  117. ZEnable = false;
  118. ZWriteEnable = false;
  119. CullMode = None;
  120. AlphaBlendEnable = false;
  121. AlphaTestEnable = false;
  122. StencilEnable = true;
  123. StencilFunc = Equal;
  124. StencilPass = Keep;
  125. StencilZFail = Keep;
  126. StencilFail = Keep;
  127. StencilMask = STENCIL_BITS_HOVER_ATTACK;
  128. StencilRef = STENCIL_BITS_HOVER_ATTACK;
  129. }
  130. }
  131. struct VS_OUTPUT_BLUR_BOX
  132. {
  133. float4 Position : POSITION;
  134. float2 Coord : TEXCOORD0;
  135. float4 NegX_PosX_NegY_PosY : TEXCOORD1;
  136. };
  137. // ----------------------------------------------------------------------------
  138. VS_OUTPUT_BLUR_BOX VS_Blur_Box(float3 Position : POSITION, float3 TexCoord : TEXCOORD0)
  139. {
  140. VS_OUTPUT_BLUR_BOX OUT = (VS_OUTPUT_BLUR_BOX)0;
  141. OUT.Position = float4(Position, 1);
  142. // Based on the outline size we know how far we should sample by.
  143. float2 TexelIncrement;
  144. TexelIncrement.x = 1.0/FrameBufferSize.x;
  145. TexelIncrement.y = 1.0/FrameBufferSize.y;
  146. // Store all the possible amounts to move from the source pixel so we don't
  147. // have to do this in the pixel shader.
  148. OUT.NegX_PosX_NegY_PosY.x = TexelIncrement.x * -TEXEL_DISTANCE_FROM_CENTER;
  149. OUT.NegX_PosX_NegY_PosY.y = TexelIncrement.x * TEXEL_DISTANCE_FROM_CENTER;
  150. OUT.NegX_PosX_NegY_PosY.z = TexelIncrement.y * -TEXEL_DISTANCE_FROM_CENTER;
  151. OUT.NegX_PosX_NegY_PosY.w = TexelIncrement.y * TEXEL_DISTANCE_FROM_CENTER;
  152. OUT.Coord = TexCoord.xy;
  153. return OUT;
  154. }
  155. float4 PS_Blur_Box(VS_OUTPUT_BLUR_BOX IN) : COLOR
  156. {
  157. float4 color = tex2D(SAMPLER(PostEffectOutlineTexture), IN.Coord + IN.NegX_PosX_NegY_PosY.xz);
  158. color += tex2D(SAMPLER(PostEffectOutlineTexture), IN.Coord + IN.NegX_PosX_NegY_PosY.xw);
  159. color += tex2D(SAMPLER(PostEffectOutlineTexture), IN.Coord + IN.NegX_PosX_NegY_PosY.yz);
  160. color += tex2D(SAMPLER(PostEffectOutlineTexture), IN.Coord + IN.NegX_PosX_NegY_PosY.yw);
  161. return OutlineIntensity * (color/4);
  162. }
  163. // ----------------------------------------------------------------------------
  164. // TECHNIQUE: BlurBox
  165. // ----------------------------------------------------------------------------
  166. technique BlurBox
  167. {
  168. pass P0
  169. {
  170. VertexShader = compile VS_2_0 VS_Blur_Box();
  171. PixelShader = compile PS_2_0 PS_Blur_Box();
  172. ZEnable = false;
  173. ZWriteEnable = false;
  174. CullMode = None;
  175. StencilEnable = false;
  176. AlphaBlendEnable = false;
  177. AlphaTestEnable = false;
  178. }
  179. }
  180. // ----------------------------------------------------------------------------
  181. struct VS_DrawWithStencilOutput
  182. {
  183. float4 Position : POSITION;
  184. float2 TexCoord : TEXCOORD0;
  185. };
  186. // ----------------------------------------------------------------------------
  187. VS_DrawWithStencilOutput VS_DrawWithStencil(float3 Position : POSITION, float2 TexCoord : TEXCOORD0)
  188. {
  189. VS_DrawWithStencilOutput Out;
  190. Out.Position = float4(Position, 1);
  191. Out.TexCoord = TexCoord;
  192. return Out;
  193. }
  194. // ----------------------------------------------------------------------------
  195. float4 PS_DrawWithStencil(float2 TexCoord : TEXCOORD0) : COLOR
  196. {
  197. return tex2D( SAMPLER(PostEffectOutlineTexture), TexCoord );
  198. }
  199. // ----------------------------------------------------------------------------
  200. // TECHNIQUE: DrawWithStencil
  201. // ----------------------------------------------------------------------------
  202. technique DrawWithStencil
  203. {
  204. pass P0
  205. {
  206. VertexShader = compile VS_2_0 VS_DrawWithStencil();
  207. PixelShader = compile PS_2_0 PS_DrawWithStencil();
  208. ZEnable = false;
  209. ZWriteEnable = false;
  210. CullMode = None;
  211. StencilEnable = true;
  212. StencilFunc = Equal;
  213. StencilPass = Keep;
  214. StencilZFail = Keep;
  215. StencilFail = Keep;
  216. StencilMask = 0x3;
  217. StencilRef = 0;
  218. AlphaBlendEnable = true;
  219. SrcBlend = SrcAlpha;
  220. DestBlend = InvSrcAlpha;
  221. }
  222. }
  223. // ----------------------------------------------------------------------------
  224. float4 PS_Blit(float2 TexCoord : TEXCOORD0) : COLOR
  225. {
  226. float4 color = tex2D( SAMPLER(PostEffectOutlineTexture), TexCoord );
  227. color.xyz = UncompressRenderTargetColor(color.xyz);
  228. return color;
  229. }
  230. // ----------------------------------------------------------------------------
  231. // TECHNIQUE: Blit
  232. // ----------------------------------------------------------------------------
  233. technique Blit
  234. {
  235. pass P0
  236. {
  237. VertexShader = compile VS_2_0 VS_DrawWithStencil();
  238. PixelShader = compile PS_2_0 PS_Blit();
  239. ZEnable = false;
  240. ZWriteEnable = false;
  241. CullMode = None;
  242. StencilEnable = false;
  243. AlphaTestEnable = false;
  244. AlphaBlendEnable = false;
  245. }
  246. }