LaserDistortion.fx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //-----------------------------------------------------------------------------
  2. // ©2006 Electronic Arts Inc
  3. //-----------------------------------------------------------------------------
  4. #include "Common.fxh"
  5. //-----------------------------------------------------------------------------
  6. float4x4 World : World < string UIWidget="None"; >;
  7. float4x4 View : View < string UIWidget="None"; >;
  8. #if defined(_WW3D_)
  9. #if !defined(USE_INDIRECT_CONSTANT)
  10. float4x4 ViewProjection
  11. <
  12. string UIWidget = "None";
  13. string SasBindAddress = "Sas.Camera.WorldToProjection";
  14. >;
  15. #endif // #if !defined(USE_INDIRECT_CONSTANT)
  16. float4x4 GetViewProjection()
  17. {
  18. return ViewProjection;
  19. }
  20. #else
  21. float4x4 Projection : Projection;
  22. float4x4 GetViewProjection()
  23. {
  24. return mul(View, Projection);
  25. }
  26. #endif
  27. // ----------------------------------------------------------------------------
  28. // Texture1
  29. // ----------------------------------------------------------------------------
  30. SAMPLER_2D_BEGIN(Texture1,
  31. string UIWidget = "None";
  32. )
  33. MinFilter = MinFilterBest;
  34. MagFilter = MagFilterBest;
  35. MipFilter = MipFilterBest;
  36. MaxAnisotropy = 8;
  37. AddressU = Wrap;
  38. AddressV = Wrap;
  39. SAMPLER_2D_END
  40. // ----------------------------------------------------------------------------
  41. // Texture2
  42. // ----------------------------------------------------------------------------
  43. SAMPLER_2D_BEGIN(Texture2,
  44. string UIWidget = "None";
  45. )
  46. MinFilter = Linear;
  47. MagFilter = Linear;
  48. MipFilter = Linear;
  49. AddressU = Wrap;
  50. AddressV = Wrap;
  51. SAMPLER_2D_END
  52. // ----------------------------------------------------------------------------
  53. // Shroud
  54. // ----------------------------------------------------------------------------
  55. ShroudSetup Shroud
  56. <
  57. string UIWidget = "None";
  58. string SasBindAddress = "Terrain.Shroud";
  59. > = DEFAULT_SHROUD;
  60. SAMPLER_2D_BEGIN( ShroudTexture,
  61. string UIWidget = "None";
  62. string SasBindAddress = "Terrain.Shroud.Texture";
  63. string ResourceName = "ShaderPreviewShroud.dds";
  64. )
  65. MinFilter = Linear;
  66. MagFilter = Linear;
  67. MipFilter = Linear;
  68. AddressU = Clamp;
  69. AddressV = Clamp;
  70. SAMPLER_2D_END
  71. //-----------------------------------------------------------------------------
  72. // Vertex Shader structure
  73. //-----------------------------------------------------------------------------
  74. struct VSInput
  75. {
  76. float3 Position : POSITION;
  77. float3 Normal : NORMAL;
  78. float3 Tangent : TANGENT;
  79. float3 Binormal : BINORMAL;
  80. float2 UV0 : TEXCOORD0;
  81. float2 UV1 : TEXCOORD1;
  82. };
  83. struct VSOutput
  84. {
  85. float4 Position : POSITION;
  86. float2 UV0 : TEXCOORD0;
  87. float2 UV1 : TEXCOORD1;
  88. float2 ShroudTexCoord : TEXCOORD2;
  89. float3x3 TangentToViewSpace : TEXCOORD3;
  90. };
  91. //-----------------------------------------------------------------------------
  92. // Pixel Shader structures
  93. //-----------------------------------------------------------------------------
  94. struct PSOutput
  95. {
  96. float4 Color : COLOR0;
  97. };
  98. //-----------------------------------------------------------------------------
  99. void CalculatePositionAndTangentFrame( VSInput vertex,
  100. out float3 WorldPosition,
  101. out float3 WorldNormal,
  102. out float3 WorldTangent,
  103. out float3 WorldBinormal )
  104. {
  105. WorldPosition = mul( float4( vertex.Position, 1 ), World );
  106. WorldNormal = normalize( mul( vertex.Normal, (float3x3)World ) );
  107. WorldTangent = normalize( mul( vertex.Tangent, (float3x3)World ) );
  108. WorldBinormal = normalize( mul( vertex.Binormal,(float3x3)World ) );
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Vertex Shader
  112. //-----------------------------------------------------------------------------
  113. VSOutput VS( VSInput Input )
  114. {
  115. VSOutput Output;
  116. float3 world_position = 0;
  117. float3 world_normal = 0;
  118. float3 world_tangent = 0;
  119. float3 world_binormal = 0;
  120. CalculatePositionAndTangentFrame( Input, world_position, world_normal, world_tangent, world_binormal );
  121. // Transform position to projection space
  122. Output.Position = mul( float4( world_position, 1 ), GetViewProjection() );
  123. // Build 3x3 tranform from tangent to world space
  124. float3x3 tangent_to_world_space = float3x3( -world_binormal, -world_tangent, world_normal );
  125. Output.TangentToViewSpace = mul( tangent_to_world_space, (float3x3)View );
  126. // Transfer UVs
  127. Output.UV0 = Input.UV0;
  128. Output.UV1 = Input.UV1;
  129. // Calculate Shroud UVs
  130. Output.ShroudTexCoord = CalculateShroudTexCoord( Shroud, world_position );
  131. return Output;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Pixel Shader
  135. //-----------------------------------------------------------------------------
  136. PSOutput PS( VSOutput Input )
  137. {
  138. PSOutput Output;
  139. float4 normal1 = tex2D( SAMPLER(Texture1), Input.UV0 );
  140. float4 normal2 = tex2D( SAMPLER(Texture2), Input.UV1 );
  141. float3 bump_normal1 = ( normal1 * 2.0 ) - 1.0;
  142. float3 bump_normal2 = ( normal2 * 2.0 ) - 1.0;
  143. float3 bump_normal = normalize( bump_normal1 + bump_normal2 );
  144. float3 normal = mul( bump_normal, Input.TangentToViewSpace );
  145. Output.Color = float4( ( normal * 0.5 ) + 0.5, normal1.w * normal2.w );
  146. // Apply shroud
  147. float shroud = tex2D( SAMPLER(ShroudTexture), Input.ShroudTexCoord ).x;
  148. shroud = BiasShroudValueForEffects( shroud );
  149. Output.Color.w *= shroud;
  150. return Output;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Technique: Default (Medium and up)
  154. //-----------------------------------------------------------------------------
  155. technique Default_M
  156. {
  157. pass pass0
  158. {
  159. VertexShader = compile VS_2_0 VS();
  160. PixelShader = compile PS_2_0 PS();
  161. ZEnable = true;
  162. ZWriteEnable = true;
  163. ZFunc = ZFUNC_INFRONT;
  164. AlphaBlendEnable = true;
  165. AlphaTestEnable = false;
  166. CullMode = none;
  167. SrcBlend = SrcAlpha;
  168. DestBlend = InvSrcAlpha;
  169. }
  170. }
  171. // ----------------------------------------------------------------------------
  172. // Technique: Default (Low)
  173. // ----------------------------------------------------------------------------
  174. technique Default_L
  175. {
  176. // No passes. Indicates technique disabled.
  177. }