Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.4 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps20b][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  3. // STATIC: "CONVERT_TO_SRGB" "0..1" [ps30][= g_pHardwareConfig->NeedsShaderSRGBConversion()] [PC]
  4. // STATIC: "CONVERT_TO_SRGB" "0..0" [= 0] [XBOX]
  5. // STATIC: "BUMPMAP" "0..1"
  6. // Includes =======================================================================================
  7. #include "common_vertexlitgeneric_dx9.h"
  8. // Texture Samplers ===============================================================================
  9. sampler g_tRefractionSampler : register( s0 );
  10. #if BUMPMAP
  11. sampler g_tBumpSampler : register( s1 );
  12. #endif
  13. sampler EnvmapSampler : register( s2 );
  14. sampler EnvmapMaskSampler : register( s3 );
  15. // Shaders Constants and Globals ==================================================================
  16. const float4 g_mViewProj0 : register( c0 ); // 1st row of matrix
  17. const float4 g_mViewProj1 : register( c1 ); // 2nd row of matrix
  18. const float4 g_vCameraPosition : register( c5 );
  19. const float4 g_vPackedConst6 : register( c6 );
  20. const float4 g_vPackedConst7 : register( c7 );
  21. const float4 g_cCloakColorTint : register( c8 );
  22. #define g_flSheenMapMaskScaleX g_vPackedConst6.x // Default = 1.0f
  23. #define g_flSheenMapMaskScaleY g_vPackedConst6.y // Default = 1.0f
  24. #define g_flSheenMapMaskOffsetX g_vPackedConst6.z // Default = 0.0f
  25. #define g_flSheenMapMaskOffsetY g_vPackedConst6.w // Default = 0.0f
  26. #define g_flSheenDirection g_vPackedConst7.x // 0,1,2 -> XYZ
  27. #define g_flEffectIndex g_vPackedConst7.y // W
  28. // 8 2D Poisson offsets (designed to use .xy and .wz swizzles (not .zw)
  29. static const float4 g_vPoissonOffset[4] = { float4 (-0.0876f, 0.9703f, 0.5651f, 0.4802f ),
  30. float4 ( 0.1851f, 0.1580f, -0.0617f, -0.2616f ),
  31. float4 (-0.5477f, -0.6603f, 0.0711f, -0.5325f ),
  32. float4 (-0.0751f, -0.8954f, 0.4054f, 0.6384f ) };
  33. // Interpolated values ============================================================================
  34. struct PS_INPUT
  35. {
  36. float3 vWorldNormal : TEXCOORD0; // World-space normal
  37. float3 vProjPosForRefract : TEXCOORD1;
  38. float3 vWorldViewVector : TEXCOORD2;
  39. float3x3 mTangentSpaceTranspose : TEXCOORD3;
  40. // second row : TEXCOORD4;
  41. // third row : TEXCOORD5;
  42. float2 vTexCoord0 : TEXCOORD6;
  43. float4 vModelSpacePos : TEXCOORD7;
  44. };
  45. // Main ===========================================================================================
  46. float4 main( PS_INPUT i ) : COLOR
  47. {
  48. float3 vWorldNormal = normalize( i.vWorldNormal.xyz );
  49. #if BUMPMAP
  50. float4 vBumpTexel = tex2D( g_tBumpSampler, i.vTexCoord0.xy );
  51. float3 vTangentNormal = ( 2.0f * vBumpTexel ) - 1.0f;
  52. vWorldNormal.xyz = mul( i.mTangentSpaceTranspose, vTangentNormal.xyz );
  53. #endif
  54. float4 result;
  55. // Staging test for weapon patterns
  56. if ( g_flEffectIndex == 2 )
  57. {
  58. float3 ppos = i.vModelSpacePos;
  59. float2 temp = 0;
  60. // 'Scaling' of texture to get it to map to the weapons
  61. // No Skewing, just move left to right
  62. if ( g_flSheenDirection == 0 )
  63. {
  64. temp.x = ppos.z;
  65. temp.y = ppos.y;
  66. }
  67. else if ( g_flSheenDirection == 1 )
  68. {
  69. temp.x = ppos.z;
  70. temp.y = ppos.x;
  71. }
  72. else
  73. {
  74. temp.x = ppos.y;
  75. temp.y = ppos.x;
  76. }
  77. temp.x -= ( g_flSheenMapMaskOffsetX ); // offset
  78. temp.y -= ( g_flSheenMapMaskOffsetY ); // offset
  79. temp.x /= g_flSheenMapMaskScaleX; // scale
  80. temp.y /= g_flSheenMapMaskScaleY;
  81. temp.y = 1.0 - temp.y;
  82. // Sample Texture
  83. // Sample Mask
  84. //float4 patternTexel = tex2D( EnvmapSampler, temp );
  85. float4 patternTexel = tex2D( EnvmapSampler, i.vTexCoord0.xy );
  86. float4 maskTexel = tex2D( EnvmapMaskSampler, i.vTexCoord0.xy );
  87. //result.rgba = float4( patternTexel.xyz * 1.0, maskTexel.x * 1.0); // 0.3 is a hack to preserve rimlight
  88. //result.rgba = float4( patternTexel.xyz, 1.0); // 0.3 is a hack to preserve rimlight
  89. result.rgba = float4( 0.0,0.0,0.0,0.0); // 0.3 is a hack to preserve rimlight
  90. }
  91. else
  92. {
  93. // generate a hard reflection in to the cube map
  94. float3 vEyeDir = -normalize(i.vWorldViewVector.xyz);
  95. float3 worldSpaceNormal, tangentSpaceNormal;
  96. tangentSpaceNormal = float3(0, 0, 1);
  97. worldSpaceNormal = normalize( mul( i.mTangentSpaceTranspose, tangentSpaceNormal ) );
  98. float3 vReflect = 2 * worldSpaceNormal * dot(worldSpaceNormal, vEyeDir) - vEyeDir;
  99. float3 envMapColor = float3( 0.0f, 0.0f, 0.0f );
  100. envMapColor = ENV_MAP_SCALE * texCUBE( EnvmapSampler, vReflect ) * g_cCloakColorTint.xyz;
  101. envMapColor *= 10.0f;
  102. // Sample the Mask
  103. float4 envmapMaskTexel;
  104. float2 temp = 0;
  105. float3 ppos = i.vModelSpacePos;
  106. //
  107. // skew the sampling based on sheen direction
  108. //if ( g_flSheenDirection == 0 )
  109. //{
  110. // temp.x = ppos.z - ppos.y;
  111. // temp.y = (ppos.x + ppos.y);
  112. //}
  113. //else if ( g_flSheenDirection == 1 )
  114. //{
  115. // temp.x = ppos.x - ppos.z;
  116. // temp.y = (ppos.y + ppos.z);
  117. //}
  118. //else
  119. //{
  120. // temp.x = ppos.y - ppos.x;
  121. // temp.y = (ppos.z + ppos.x);
  122. //}
  123. // No Skewing, just move left to right
  124. if ( g_flSheenDirection == 0 )
  125. {
  126. temp.x = ppos.z;
  127. temp.y = ppos.y;
  128. }
  129. else if ( g_flSheenDirection == 1 )
  130. {
  131. temp.x = ppos.z;
  132. temp.y = ppos.x;
  133. }
  134. else
  135. {
  136. temp.x = ppos.y;
  137. temp.y = ppos.x;
  138. }
  139. temp.x -= ( g_flSheenMapMaskOffsetX ); // offset
  140. temp.y -= ( g_flSheenMapMaskOffsetY ); // offset
  141. temp.x /= g_flSheenMapMaskScaleX; // scale
  142. temp.y /= g_flSheenMapMaskScaleY;
  143. temp.y = 1.0 - temp.y;
  144. envmapMaskTexel = tex2D( EnvmapMaskSampler, temp );
  145. // Build result, only have alpha if there was value in the mask.
  146. // High alpha (white) will override the underlying texture while low to none will show model underneath
  147. //float4 result;
  148. //result.rgba = float4( envMapColor, envmapMaskTexel.x * g_cCloakColorTint.w );
  149. if ( g_flEffectIndex == 1 )
  150. {
  151. //float alpha = max( max( envmapMaskTexel.x, envmapMaskTexel.y), envmapMaskTexel.z );
  152. //result.rgba = float4( envMapColor.xyz * envmapMaskTexel.xyz, alpha );
  153. float alpha = max( max( envMapColor.x, envMapColor.y), envMapColor.z );
  154. result.rgba = float4( envMapColor.xyz * envmapMaskTexel.xyz, alpha * envmapMaskTexel.x );
  155. result.rgba = result.rgba * 1.8f;
  156. }
  157. else
  158. {
  159. float alpha = max( max( envMapColor.x, envMapColor.y), envMapColor.z );
  160. result.rgba = float4( envMapColor.xyz * envmapMaskTexel.xyz, alpha * envmapMaskTexel.x );
  161. }
  162. }
  163. return FinalOutput( result, 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  164. }