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.

122 lines
5.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Cloaking shader for Spy in TF2 (and probably many other things to come)
  4. //
  5. // $NoKeywords: $
  6. //=====================================================================================//
  7. #include "BaseVSShader.h"
  8. #include "convar.h"
  9. #include "cloak_dx9_helper.h"
  10. DEFINE_FALLBACK_SHADER( Cloak, Cloak_DX90 )
  11. BEGIN_VS_SHADER( Cloak_DX90, "Help for Cloak" )
  12. BEGIN_SHADER_PARAMS
  13. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  14. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  15. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  16. SHADER_PARAM( REFRACTTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "refraction tint" )
  17. SHADER_PARAM( NORMALMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "normal map" )
  18. SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  19. SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  20. SHADER_PARAM( TIME, SHADER_PARAM_TYPE_FLOAT, "0.0f", "" )
  21. SHADER_PARAM( REFRACTTINTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shield", "" )
  22. SHADER_PARAM( REFRACTTINTTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  23. SHADER_PARAM( FRESNELREFLECTION, SHADER_PARAM_TYPE_FLOAT, "1.0", "1.0 == mirror, 0.0 == water" )
  24. SHADER_PARAM( NOWRITEZ, SHADER_PARAM_TYPE_INTEGER, "0", "0 == write z, 1 = no write z" )
  25. SHADER_PARAM( MASKED, SHADER_PARAM_TYPE_BOOL, "0", "mask using dest alpha" )
  26. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0", "How cloaked? Zero is not cloaked, 1 is fully cloaked." )
  27. SHADER_PARAM( LIGHTWARPTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "1D ramp texture for tinting scalar diffuse term" )
  28. SHADER_PARAM( PHONGEXPONENT, SHADER_PARAM_TYPE_FLOAT, "5.0", "Phong exponent for local specular lights" )
  29. SHADER_PARAM( PHONGTINT, SHADER_PARAM_TYPE_VEC3, "5.0", "Phong tint for local specular lights" )
  30. SHADER_PARAM( PHONGALBEDOTINT, SHADER_PARAM_TYPE_BOOL, "1.0", "Apply tint by albedo (controlled by spec exponent texture" )
  31. SHADER_PARAM( PHONGFRESNELRANGES, SHADER_PARAM_TYPE_VEC3, "[0 0.5 1]", "Parameters for remapping fresnel output" )
  32. SHADER_PARAM( PHONGBOOST, SHADER_PARAM_TYPE_FLOAT, "1.0", "Phong overbrightening factor (specular mask channel should be authored to account for this)" )
  33. SHADER_PARAM( PHONGEXPONENTTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "shadertest/BaseTexture", "Phong Exponent map" )
  34. // Rim lighting terms
  35. SHADER_PARAM( RIMLIGHT, SHADER_PARAM_TYPE_BOOL, "0", "enables rim lighting" )
  36. SHADER_PARAM( RIMLIGHTEXPONENT, SHADER_PARAM_TYPE_FLOAT, "4.0", "Exponent for rim lights" )
  37. SHADER_PARAM( RIMLIGHTBOOST, SHADER_PARAM_TYPE_FLOAT, "1.0", "Boost for rim lights" )
  38. SHADER_PARAM( RIMMASK, SHADER_PARAM_TYPE_BOOL, "0", "Indicates whether or not to use alpha channel of exponent texture to mask the rim term" )
  39. END_SHADER_PARAMS
  40. // FIXME: doesn't support Fresnel!
  41. void SetupVars( Cloak_DX9_Vars_t& info )
  42. {
  43. info.m_nBaseTexture = BASETEXTURE;
  44. info.m_nRefractAmount = REFRACTAMOUNT;
  45. info.m_nRefractTint = REFRACTTINT;
  46. info.m_nNormalMap = NORMALMAP;
  47. info.m_nBumpFrame = BUMPFRAME;
  48. info.m_nBumpTransform = BUMPTRANSFORM;
  49. info.m_nRefractTintTexture = REFRACTTINTTEXTURE;
  50. info.m_nRefractTintTextureFrame = REFRACTTINTTEXTUREFRAME;
  51. info.m_nFresnelReflection = FRESNELREFLECTION;
  52. info.m_nMasked = MASKED;
  53. info.m_nCloakFactor = CLOAKFACTOR;
  54. info.m_nDiffuseWarpTexture = LIGHTWARPTEXTURE;
  55. info.m_nPhongExponent = PHONGEXPONENT;
  56. info.m_nPhongTint = PHONGTINT;
  57. info.m_nPhongAlbedoTint = PHONGALBEDOTINT;
  58. info.m_nPhongExponentTexture = PHONGEXPONENTTEXTURE;
  59. info.m_nPhongBoost = PHONGBOOST;
  60. info.m_nPhongFresnelRanges = PHONGFRESNELRANGES;
  61. // Rim lighting parameters
  62. info.m_nRimLight = RIMLIGHT;
  63. info.m_nRimLightPower = RIMLIGHTEXPONENT;
  64. info.m_nRimLightBoost = RIMLIGHTBOOST;
  65. info.m_nRimMask = RIMMASK;
  66. }
  67. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  68. {
  69. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  70. return true;
  71. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  72. return true;
  73. else
  74. return false;
  75. }
  76. bool IsTranslucent( IMaterialVar **params ) const
  77. {
  78. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  79. return true;
  80. else
  81. return false;
  82. }
  83. SHADER_INIT_PARAMS()
  84. {
  85. Cloak_DX9_Vars_t info;
  86. SetupVars( info );
  87. InitParamsCloak_DX9( this, params, pMaterialName, info );
  88. }
  89. SHADER_FALLBACK
  90. {
  91. if( g_pHardwareConfig->GetDXSupportLevel() < 82 )
  92. return "Refract_DX80";
  93. return 0;
  94. }
  95. SHADER_INIT
  96. {
  97. Cloak_DX9_Vars_t info;
  98. SetupVars( info );
  99. InitCloak_DX9( this, params, info );
  100. }
  101. SHADER_DRAW
  102. {
  103. Cloak_DX9_Vars_t info;
  104. SetupVars( info );
  105. DrawCloak_DX9( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  106. }
  107. END_SHADER