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.

205 lines
6.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "BaseVSShader.h"
  8. #include "convar.h"
  9. #include "d3dx.h"
  10. DEFINE_FALLBACK_SHADER( Portal, Portal_DX60 )
  11. BEGIN_VS_SHADER( Portal_DX60,
  12. "Help for Portal_DX60 shader" )
  13. BEGIN_SHADER_PARAMS
  14. SHADER_PARAM_OVERRIDE( COLOR, SHADER_PARAM_TYPE_COLOR, "{255 255 255}", "unused", SHADER_PARAM_NOT_EDITABLE )
  15. SHADER_PARAM_OVERRIDE( ALPHA, SHADER_PARAM_TYPE_FLOAT, "1.0", "unused", SHADER_PARAM_NOT_EDITABLE )
  16. SHADER_PARAM( STATICAMOUNT, SHADER_PARAM_TYPE_FLOAT, "0.0", "Amount of the static blend texture to blend into the base texture" )
  17. SHADER_PARAM( STATICBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "When adding static, this is the texture that gets blended in" )
  18. SHADER_PARAM( STATICBLENDTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  19. SHADER_PARAM( ALPHAMASKTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "An alpha mask for odd shaped portals" )
  20. SHADER_PARAM( ALPHAMASKTEXTUREFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "" )
  21. SHADER_PARAM( RENDERFIXZ, SHADER_PARAM_TYPE_INTEGER, "0", "Special depth handling, intended for rendering bug workarounds for extremely close polygons" )
  22. END_SHADER_PARAMS
  23. SHADER_INIT_PARAMS()
  24. {
  25. SET_FLAGS( MATERIAL_VAR_TRANSLUCENT );
  26. }
  27. SHADER_FALLBACK
  28. {
  29. return 0; //it doesn't get any simpler than this
  30. }
  31. SHADER_INIT
  32. {
  33. if( params[STATICBLENDTEXTURE]->IsDefined() )
  34. LoadTexture( STATICBLENDTEXTURE );
  35. if( params[ALPHAMASKTEXTURE]->IsDefined() )
  36. LoadTexture( ALPHAMASKTEXTURE );
  37. if( !params[STATICAMOUNT]->IsDefined() )
  38. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  39. if( !params[STATICAMOUNT]->IsDefined() )
  40. params[STATICAMOUNT]->SetFloatValue( 0.0f );
  41. if( !params[STATICBLENDTEXTURE]->IsDefined() )
  42. params[STATICBLENDTEXTURE]->SetIntValue( 0 );
  43. if( !params[STATICBLENDTEXTUREFRAME]->IsDefined() )
  44. params[STATICBLENDTEXTUREFRAME]->SetIntValue( 0 );
  45. if( !params[ALPHAMASKTEXTURE]->IsDefined() )
  46. params[ALPHAMASKTEXTURE]->SetIntValue( 0 );
  47. if( !params[ALPHAMASKTEXTUREFRAME]->IsDefined() )
  48. params[ALPHAMASKTEXTUREFRAME]->SetIntValue( 0 );
  49. if( !params[RENDERFIXZ]->IsDefined() )
  50. params[RENDERFIXZ]->SetIntValue( 0 );
  51. }
  52. SHADER_DRAW
  53. {
  54. bool bAlphaMaskTexture = params[ALPHAMASKTEXTURE]->IsTexture();
  55. SHADOW_STATE
  56. {
  57. SetInitialShadowState();
  58. bool bIsModel = IS_FLAG_SET( MATERIAL_VAR_MODEL );
  59. FogToFogColor();
  60. if( params[RENDERFIXZ]->GetIntValue() == 0 )
  61. {
  62. //pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DECAL ); //a portal is effectively a decal on top of a wall
  63. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_NEAREROREQUAL );
  64. }
  65. else
  66. {
  67. pShaderShadow->EnablePolyOffset( SHADER_POLYOFFSET_DISABLE );
  68. pShaderShadow->DepthFunc( SHADER_DEPTHFUNC_ALWAYS );
  69. pShaderShadow->EnableDepthTest( false );
  70. pShaderShadow->EnableDepthWrites( false );
  71. }
  72. pShaderShadow->EnableDepthWrites( true );
  73. if( bAlphaMaskTexture )
  74. {
  75. pShaderShadow->EnableAlphaTest( true );
  76. if( bIsModel )
  77. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_TEXCOORD1 );
  78. else
  79. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_LIGHTMAP_TEXCOORD0 | SHADER_DRAW_LIGHTMAP_TEXCOORD1 );
  80. }
  81. else
  82. {
  83. if( bIsModel )
  84. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  85. else
  86. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_LIGHTMAP_TEXCOORD0 );
  87. }
  88. }
  89. DYNAMIC_STATE
  90. {
  91. pShaderAPI->SetDefaultState();
  92. }
  93. if( bAlphaMaskTexture )
  94. StaticPass_WithAlphaMask( pShaderShadow, pShaderAPI, params ); //portal static texture blending
  95. else
  96. StaticPass_NoAlphaMask( pShaderShadow, pShaderAPI, params ); //portal static texture blending
  97. }
  98. void StaticPass_NoAlphaMask( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, IMaterialVar **params )
  99. {
  100. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  101. SHADOW_STATE
  102. {
  103. if( bStaticBlendTexture )
  104. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  105. }
  106. DYNAMIC_STATE
  107. {
  108. if( bStaticBlendTexture )
  109. BindTexture( SHADER_SAMPLER0, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  110. else
  111. pShaderAPI->Color4f( 0.5f, 0.5f, 0.5f, 0.0f );
  112. }
  113. Draw();
  114. }
  115. void StaticPass_WithAlphaMask( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, IMaterialVar **params )
  116. {
  117. bool bStaticBlendTexture = params[STATICBLENDTEXTURE]->IsTexture();
  118. SHADOW_STATE
  119. {
  120. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  121. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  122. pShaderShadow->EnableBlending( true );
  123. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  124. pShaderShadow->EnableCustomPixelPipe( true );
  125. pShaderShadow->CustomTextureStages( 2 );
  126. //portal static
  127. if( bStaticBlendTexture )
  128. {
  129. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  130. SHADER_TEXCHANNEL_COLOR,
  131. SHADER_TEXOP_SELECTARG1,
  132. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_TEXTURE );
  133. }
  134. else
  135. {
  136. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  137. SHADER_TEXCHANNEL_COLOR,
  138. SHADER_TEXOP_SELECTARG1,
  139. SHADER_TEXARG_CONSTANTCOLOR, SHADER_TEXARG_CONSTANTCOLOR );
  140. }
  141. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  142. SHADER_TEXCHANNEL_ALPHA,
  143. SHADER_TEXOP_SELECTARG1,
  144. SHADER_TEXARG_ZERO, SHADER_TEXARG_ZERO );
  145. //alpha mask
  146. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  147. SHADER_TEXCHANNEL_COLOR,
  148. SHADER_TEXOP_SELECTARG1,
  149. SHADER_TEXARG_PREVIOUSSTAGE, SHADER_TEXARG_PREVIOUSSTAGE );
  150. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  151. SHADER_TEXCHANNEL_ALPHA,
  152. SHADER_TEXOP_SELECTARG1,
  153. SHADER_TEXARG_TEXTUREALPHA, SHADER_TEXARG_TEXTUREALPHA );
  154. }
  155. DYNAMIC_STATE
  156. {
  157. if( bStaticBlendTexture )
  158. BindTexture( SHADER_SAMPLER0, STATICBLENDTEXTURE, STATICBLENDTEXTUREFRAME );
  159. else
  160. pShaderAPI->Color4f( 0.5f, 0.5f, 0.5f, 0.0f );
  161. BindTexture( SHADER_SAMPLER1, ALPHAMASKTEXTURE, ALPHAMASKTEXTUREFRAME );
  162. }
  163. Draw();
  164. SHADOW_STATE
  165. {
  166. pShaderShadow->EnableCustomPixelPipe( false );
  167. }
  168. }
  169. END_SHADER