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.

252 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. /* Example how to plug this into an existing shader:
  3. In the VMT:
  4. // Cloak Pass
  5. "$cloakPassEnabled" "1"
  6. #include "cloak_blended_pass_helper.h"
  7. In BEGIN_SHADER_PARAMS:
  8. // Cloak Pass
  9. SHADER_PARAM( CLOAKPASSENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enables cloak render in a second pass" )
  10. SHADER_PARAM( CLOAKFACTOR, SHADER_PARAM_TYPE_FLOAT, "0.0", "" )
  11. SHADER_PARAM( CLOAKCOLORTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Cloak color tint" )
  12. SHADER_PARAM( REFRACTAMOUNT, SHADER_PARAM_TYPE_FLOAT, "2", "" )
  13. Add this above SHADER_INIT_PARAMS()
  14. // Cloak Pass
  15. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  16. {
  17. info.m_nCloakFactor = CLOAKFACTOR;
  18. info.m_nCloakColorTint = CLOAKCOLORTINT;
  19. info.m_nRefractAmount = REFRACTAMOUNT;
  20. }
  21. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  22. {
  23. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  24. {
  25. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  26. return true;
  27. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  28. return true;
  29. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  30. }
  31. // Check flag2 if not drawing cloak pass
  32. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  33. }
  34. bool IsTranslucent( IMaterialVar **params ) const
  35. {
  36. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  37. {
  38. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  39. return true;
  40. // else, not cloaking this frame, so check flag in case the base material still needs it
  41. }
  42. // Check flag if not drawing cloak pass
  43. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  44. }
  45. In SHADER_INIT_PARAMS()
  46. // Cloak Pass
  47. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  48. {
  49. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  50. }
  51. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  52. {
  53. CloakBlendedPassVars_t info;
  54. SetupVarsCloakBlendedPass( info );
  55. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  56. }
  57. In SHADER_INIT
  58. // Cloak Pass
  59. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  60. {
  61. CloakBlendedPassVars_t info;
  62. SetupVarsCloakBlendedPass( info );
  63. InitCloakBlendedPass( this, params, info );
  64. }
  65. Modify SHADER_DRAW to look something like this:
  66. // Skip the standard rendering if cloak pass is fully opaque
  67. bool bDrawStandardPass = true;
  68. if ( params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  69. {
  70. CloakBlendedPassVars_t info;
  71. SetupVarsCloakBlendedPass( info );
  72. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  73. {
  74. bDrawStandardPass = false;
  75. }
  76. }
  77. // Standard rendering pass
  78. if ( bDrawStandardPass )
  79. {
  80. Eye_Refract_Vars_t info;
  81. SetupVarsEyeRefract( info );
  82. Draw_Eyes_Refract( this, params, pShaderAPI, pShaderShadow, info );
  83. }
  84. else
  85. {
  86. // Skip this pass!
  87. Draw( false );
  88. }
  89. // Cloak Pass
  90. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  91. {
  92. // If ( snapshotting ) or ( we need to draw this frame )
  93. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  94. {
  95. CloakBlendedPassVars_t info;
  96. SetupVarsCloakBlendedPass( info );
  97. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info, vertexCompression );
  98. }
  99. else // We're not snapshotting and we don't need to draw this frame
  100. {
  101. // Skip this pass!
  102. Draw( false );
  103. }
  104. }
  105. ==================================================================================================== */
  106. #include "BaseVSShader.h"
  107. #include "mathlib/vmatrix.h"
  108. #include "cloak_blended_pass_helper.h"
  109. #include "convar.h"
  110. // Auto generated inc files
  111. #include "cloak_blended_pass_dx8_vs11.inc"
  112. void InitParamsCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, CloakBlendedPassVars_t &info )
  113. {
  114. // Set material flags
  115. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  116. SET_FLAGS( MATERIAL_VAR_MODEL );
  117. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  118. // Set material parameter default values
  119. if ( ( info.m_nCloakFactor != -1 ) && ( !params[info.m_nCloakFactor]->IsDefined() ) )
  120. {
  121. params[info.m_nCloakFactor]->SetFloatValue( kDefaultCloakFactor );
  122. }
  123. if ( ( info.m_nRefractAmount != -1 ) && ( !params[info.m_nRefractAmount]->IsDefined() ) )
  124. {
  125. params[info.m_nRefractAmount]->SetFloatValue( kDefaultRefractAmount );
  126. }
  127. if ( ( info.m_nCloakColorTint != -1 ) && ( !params[info.m_nCloakColorTint]->IsDefined() ) )
  128. {
  129. params[info.m_nCloakColorTint]->SetVecValue( kDefaultCloakColorTint[0], kDefaultCloakColorTint[1], kDefaultCloakColorTint[2], kDefaultCloakColorTint[3] );
  130. }
  131. }
  132. void InitCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, CloakBlendedPassVars_t &info )
  133. {
  134. // No textures
  135. }
  136. void DrawCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  137. IShaderShadow* pShaderShadow, CloakBlendedPassVars_t &info, VertexCompressionType_t vertexCompression )
  138. {
  139. SHADOW_STATE
  140. {
  141. // Reset shadow state manually since we're drawing from two materials
  142. pShader->SetInitialShadowState( );
  143. // Set stream format (note that this shader supports compression)
  144. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  145. int nTexCoordCount = 1;
  146. int userDataSize = 0;
  147. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  148. // Vertex Shader
  149. cloak_blended_pass_dx8_vs11_Static_Index vshIndex;
  150. pShaderShadow->SetVertexShader( "cloak_blended_pass_dx8_vs11", vshIndex.GetIndex() );
  151. // Pixel Shader
  152. pShaderShadow->SetPixelShader( "cloak_blended_pass_dx8_ps11", 0 );
  153. // Textures
  154. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Renderable texture for refraction
  155. // Blending
  156. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  157. pShaderShadow->EnableAlphaWrites( false );
  158. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  159. pShaderShadow->EnableDepthWrites( true );
  160. }
  161. DYNAMIC_STATE
  162. {
  163. // Reset render state manually since we're drawing from two materials
  164. pShaderAPI->SetDefaultState();
  165. // Set Vertex Shader Combos
  166. cloak_blended_pass_dx8_vs11_Dynamic_Index vshIndex;
  167. vshIndex.SetSKINNING( pShaderAPI->GetCurrentNumBones() > 0 );
  168. pShaderAPI->SetVertexShaderIndex( vshIndex.GetIndex() );
  169. // Set Vertex Shader Constants
  170. float vVsConst3[4] = { 1.35f, 0.0f, 0.4f, ( 1.0f / ( 0.425f - 0.4f ) ) };
  171. vVsConst3[1] = clamp( IS_PARAM_DEFINED( info.m_nCloakFactor ) ? params[info.m_nCloakFactor]->GetFloatValue() : kDefaultCloakFactor, 0.0f, 1.0f );
  172. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_3, vVsConst3 );
  173. float vVsConst4[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  174. vVsConst4[0] = params[info.m_nRefractAmount]->GetFloatValue() * ( 1.0f - clamp( params[info.m_nCloakFactor]->GetFloatValue(), 0.0f, 1.0f ) );
  175. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, vVsConst4 );
  176. float vVsConst5[4] = { 1.0f, 1.0f, 0.0f, 0.0f };
  177. pShaderAPI->SetVertexShaderConstant( VERTEX_SHADER_SHADER_SPECIFIC_CONST_5, vVsConst5, 1 );
  178. // Bind textures
  179. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  180. // Set Pixel Shader Constants
  181. // Refract color tint
  182. float vPsConst0[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  183. float fColorTintStrength = clamp( ( clamp( IS_PARAM_DEFINED( info.m_nCloakFactor ) ? params[info.m_nCloakFactor]->GetFloatValue() : kDefaultCloakFactor, 0.0f, 1.0f ) - 0.75f ) * 4.0f, 0.0f, 1.0f );
  184. vPsConst0[0] = IS_PARAM_DEFINED( info.m_nCloakColorTint ) ? params[info.m_nCloakColorTint]->GetVecValue()[0] : kDefaultCloakColorTint[0];
  185. vPsConst0[1] = IS_PARAM_DEFINED( info.m_nCloakColorTint ) ? params[info.m_nCloakColorTint]->GetVecValue()[1] : kDefaultCloakColorTint[1];
  186. vPsConst0[2] = IS_PARAM_DEFINED( info.m_nCloakColorTint ) ? params[info.m_nCloakColorTint]->GetVecValue()[2] : kDefaultCloakColorTint[2];
  187. vPsConst0[0] = SrgbLinearToGamma( vPsConst0[0] );
  188. vPsConst0[1] = SrgbLinearToGamma( vPsConst0[1] );
  189. vPsConst0[2] = SrgbLinearToGamma( vPsConst0[2] );
  190. vPsConst0[0] = ( vPsConst0[0] * ( 1.0f - fColorTintStrength ) ) + ( fColorTintStrength );
  191. vPsConst0[1] = ( vPsConst0[1] * ( 1.0f - fColorTintStrength ) ) + ( fColorTintStrength );
  192. vPsConst0[2] = ( vPsConst0[2] * ( 1.0f - fColorTintStrength ) ) + ( fColorTintStrength );
  193. pShaderAPI->SetPixelShaderConstant( 0, vPsConst0, 1 );
  194. }
  195. pShader->Draw();
  196. }
  197. bool CloakBlendedPassIsFullyOpaque ( IMaterialVar** params, CloakBlendedPassVars_t &info )
  198. {
  199. float flCloakFactor = IS_PARAM_DEFINED( info.m_nCloakFactor ) ? params[info.m_nCloakFactor]->GetFloatValue() : kDefaultCloakFactor;
  200. //float flRefractAmount = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  201. // NOTE: If this math changes, you need to update the pixel shader code!
  202. float flFresnel = 1.0f - ( 0.0f ); // Assume V.N = 0.0f;
  203. float flCloakLerpFactor = clamp( Lerp( clamp( flCloakFactor, 0.0f, 1.0f ), 1.0f, flFresnel - 1.35f ), 0.0f, 1.0f );
  204. //flCloakLerpFactor = 1.0f - smoothstep( 0.4f, 0.425f, flCloakLerpFactor );
  205. if ( flCloakLerpFactor <= 0.4f )
  206. return true;
  207. return false;
  208. }