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.

132 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A wet version of base * lightmap
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "BaseVSShader.h"
  9. #include "cable.inc"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. DEFINE_FALLBACK_SHADER( Cable, Cable_DX8 )
  13. BEGIN_VS_SHADER( Cable_DX8,
  14. "Help for Cable shader" )
  15. BEGIN_SHADER_PARAMS
  16. SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "cable/cablenormalmap", "bumpmap texture" )
  17. SHADER_PARAM( MINLIGHT, SHADER_PARAM_TYPE_FLOAT, "0.1", "Minimum amount of light (0-1 value)" )
  18. SHADER_PARAM( MAXLIGHT, SHADER_PARAM_TYPE_FLOAT, "0.3", "Maximum amount of light" )
  19. END_SHADER_PARAMS
  20. SHADER_FALLBACK
  21. {
  22. if ( IsPC() && !g_pHardwareConfig->SupportsVertexAndPixelShaders())
  23. {
  24. return "UnlitGeneric_DX6";
  25. }
  26. return 0;
  27. }
  28. SHADER_INIT
  29. {
  30. LoadBumpMap( BUMPMAP );
  31. LoadTexture( BASETEXTURE );
  32. }
  33. SHADER_DRAW
  34. {
  35. SHADOW_STATE
  36. {
  37. // Enable blending?
  38. if ( IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT ) )
  39. {
  40. pShaderShadow->EnableDepthWrites( false );
  41. pShaderShadow->EnableBlending( true );
  42. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  43. }
  44. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  45. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  46. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  47. if ( g_pHardwareConfig->GetDXSupportLevel() >= 90)
  48. {
  49. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, true );
  50. }
  51. int tCoordDimensions[] = {2,2};
  52. pShaderShadow->VertexShaderVertexFormat(
  53. VERTEX_POSITION | VERTEX_COLOR | VERTEX_TANGENT_S | VERTEX_TANGENT_T,
  54. 2, tCoordDimensions, 0 );
  55. cable_Static_Index vshIndex;
  56. pShaderShadow->SetVertexShader( "Cable", vshIndex.GetIndex() );
  57. pShaderShadow->SetPixelShader( "Cable" );
  58. // we are writing linear values from this shader.
  59. // This is kinda wrong. We are writing linear or gamma depending on "IsHDREnabled" below.
  60. // The COLOR really decides if we are gamma or linear.
  61. if ( g_pHardwareConfig->GetDXSupportLevel() >= 90)
  62. {
  63. pShaderShadow->EnableSRGBWrite( true );
  64. }
  65. FogToFogColor();
  66. }
  67. DYNAMIC_STATE
  68. {
  69. BindTexture( SHADER_SAMPLER0, BUMPMAP );
  70. BindTexture( SHADER_SAMPLER1, BASETEXTURE );
  71. // The dot product with the light is remapped from the range
  72. // [-1,1] to [MinLight, MaxLight].
  73. // Given:
  74. // -A + B = MinLight
  75. // A + B = MaxLight
  76. // then A = (MaxLight - MinLight) / 2
  77. // and B = (MaxLight + MinLight) / 2
  78. // So here, we multiply the light direction by A to scale the dot product.
  79. // Then in the pixel shader we add by B.
  80. float flMinLight = params[MINLIGHT]->GetFloatValue();
  81. float flMaxLight = params[MAXLIGHT]->GetFloatValue();
  82. float A = (flMaxLight - flMinLight) * 0.5f;
  83. float B = (flMaxLight + flMinLight) * 0.5f;
  84. float b4[4] = {B,B,B,B};
  85. if( g_pHardwareConfig->GetDXSupportLevel() >= 90)
  86. {
  87. SetPixelShaderConstantGammaToLinear( 0, b4 );
  88. }
  89. else
  90. {
  91. pShaderAPI->SetPixelShaderConstant( 0, b4 );
  92. }
  93. // This is the light direction [0,1,0,0] * A * 0.5
  94. float lightDir[4] = {0, A*0.5, 0, 0};
  95. if( g_pHardwareConfig->GetDXSupportLevel() >= 90)
  96. {
  97. SetVertexShaderConstantGammaToLinear( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, lightDir );
  98. }
  99. else
  100. {
  101. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, lightDir );
  102. }
  103. cable_Dynamic_Index vshIndex;
  104. vshIndex.SetDOWATERFOG( pShaderAPI->GetSceneFogMode() == MATERIAL_FOG_LINEAR_BELOW_FOG_Z );
  105. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  106. }
  107. Draw();
  108. }
  109. END_SHADER