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.

106 lines
5.0 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. // Shaders Constants and Globals ==================================================================
  14. const float4 g_mViewProj0 : register( c0 ); // 1st row of matrix
  15. const float4 g_mViewProj1 : register( c1 ); // 2nd row of matrix
  16. const float4 g_vCameraPosition : register( c5 );
  17. const float4 g_vPackedConst6 : register( c6 );
  18. #define g_flCloakFactor g_vPackedConst6.x // Default = 1.0f
  19. #define g_flRefractAmount g_vPackedConst6.y // Default = 1.0f
  20. const float4 g_cCloakColorTint : register( c7 );
  21. // 8 2D Poisson offsets (designed to use .xy and .wz swizzles (not .zw)
  22. static const float4 g_vPoissonOffset[4] = { float4 (-0.0876f, 0.9703f, 0.5651f, 0.4802f ),
  23. float4 ( 0.1851f, 0.1580f, -0.0617f, -0.2616f ),
  24. float4 (-0.5477f, -0.6603f, 0.0711f, -0.5325f ),
  25. float4 (-0.0751f, -0.8954f, 0.4054f, 0.6384f ) };
  26. // Interpolated values ============================================================================
  27. struct PS_INPUT
  28. {
  29. float3 vWorldNormal : TEXCOORD0; // World-space normal
  30. float3 vProjPosForRefract : TEXCOORD1;
  31. float3 vWorldViewVector : TEXCOORD2;
  32. #if BUMPMAP
  33. float3x3 mTangentSpaceTranspose : TEXCOORD3;
  34. // second row : TEXCOORD4;
  35. // third row : TEXCOORD5;
  36. float2 vTexCoord0 : TEXCOORD6;
  37. #endif
  38. };
  39. // Main ===========================================================================================
  40. float4 main( PS_INPUT i ) : COLOR
  41. {
  42. float3 vWorldNormal = normalize( i.vWorldNormal.xyz );
  43. #if BUMPMAP
  44. float4 vBumpTexel = tex2D( g_tBumpSampler, i.vTexCoord0.xy );
  45. float3 vTangentNormal = ( 2.0f * vBumpTexel ) - 1.0f;
  46. vWorldNormal.xyz = mul( i.mTangentSpaceTranspose, vTangentNormal.xyz );
  47. #endif
  48. // Transform world space normal into clip space and project
  49. float3 vProjNormal;
  50. vProjNormal.x = dot( vWorldNormal.xyz, g_mViewProj0.xyz ); // 1st row
  51. vProjNormal.y = dot( vWorldNormal.xyz, g_mViewProj1.xyz ); // 2nd row
  52. // Compute coordinates for sampling refraction
  53. float2 vRefractTexCoordNoWarp = i.vProjPosForRefract.xy / i.vProjPosForRefract.z;
  54. float2 vRefractTexCoord = vProjNormal.xy;
  55. float scale = lerp( g_flRefractAmount, 0.0f, saturate( g_flCloakFactor ) );
  56. vRefractTexCoord.xy *= scale;
  57. vRefractTexCoord.xy += vRefractTexCoordNoWarp.xy;
  58. // Blur by scalable Poisson filter
  59. float flBlurAmount = lerp( 0.05f, 0.0f, saturate( g_flCloakFactor ) );
  60. float3 cRefract = tex2D( g_tRefractionSampler, vRefractTexCoord.xy );
  61. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[0].xy * flBlurAmount ) );
  62. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[0].wz * flBlurAmount ) );
  63. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[1].xy * flBlurAmount ) );
  64. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[1].wz * flBlurAmount ) );
  65. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[2].xy * flBlurAmount ) );
  66. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[2].wz * flBlurAmount ) );
  67. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[3].xy * flBlurAmount ) );
  68. cRefract += tex2D( g_tRefractionSampler, vRefractTexCoord.xy + ( g_vPoissonOffset[3].wz * flBlurAmount ) );
  69. cRefract /= 9.0f;
  70. // 1-(N.V) for Fresnel term (NOTE: If this math changes, you need to update the C code that mimics this on the CPU)
  71. float flFresnel = 1.0f - saturate( dot( i.vWorldNormal.xyz, normalize( -i.vWorldViewVector.xyz ) ) );
  72. float flCloakLerpFactor = saturate( lerp( 1.0f, flFresnel - 1.35f, saturate( g_flCloakFactor ) ) );
  73. flCloakLerpFactor = 1.0f - smoothstep( 0.4f, 0.425f, flCloakLerpFactor );
  74. // Slightly dim the facing pixels and brighten the silhouette pixels
  75. cRefract.rgb *= lerp( flFresnel * 0.4 + 0.8, 1.0f, saturate( g_flCloakFactor ) * saturate( g_flCloakFactor ) ); // This gives a scalar in the range [0.8 1.2]
  76. // Refract color tint
  77. float fColorTintStrength = saturate( ( saturate( g_flCloakFactor ) - 0.75f ) * 4.0f );
  78. cRefract.rgb *= lerp( g_cCloakColorTint, 1.0f, fColorTintStrength );
  79. //===============//
  80. // Combine terms //
  81. //===============//
  82. float4 result;
  83. result.rgb = cRefract.rgb;
  84. // Set alpha to cloak mask
  85. result.a = flCloakLerpFactor;
  86. return FinalOutput( result, 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  87. }