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.

358 lines
13 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. // This should already exist
  14. //SHADER_PARAM( BUMPMAP, SHADER_PARAM_TYPE_TEXTURE, "models/shadertest/shader1_normal", "bump map" )
  15. //SHADER_PARAM( BUMPFRAME, SHADER_PARAM_TYPE_INTEGER, "0", "frame number for $bumpmap" )
  16. //SHADER_PARAM( BUMPTRANSFORM, SHADER_PARAM_TYPE_MATRIX, "center .5 .5 scale 1 1 rotate 0 translate 0 0", "$bumpmap texcoord transform" )
  17. Add this above SHADER_INIT_PARAMS()
  18. // Cloak Pass
  19. void SetupVarsCloakBlendedPass( CloakBlendedPassVars_t &info )
  20. {
  21. info.m_nCloakFactor = CLOAKFACTOR;
  22. info.m_nCloakColorTint = CLOAKCOLORTINT;
  23. info.m_nRefractAmount = REFRACTAMOUNT;
  24. // Delete these lines if not bump mapping!
  25. info.m_nBumpmap = BUMPMAP;
  26. info.m_nBumpFrame = BUMPFRAME;
  27. info.m_nBumpTransform = BUMPTRANSFORM;
  28. }
  29. bool NeedsPowerOfTwoFrameBufferTexture( IMaterialVar **params, bool bCheckSpecificToThisFrame ) const
  30. {
  31. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  32. {
  33. if ( bCheckSpecificToThisFrame == false ) // For setting model flag at load time
  34. return true;
  35. else if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  36. return true;
  37. // else, not cloaking this frame, so check flag2 in case the base material still needs it
  38. }
  39. // Check flag2 if not drawing cloak pass
  40. return IS_FLAG2_SET( MATERIAL_VAR2_NEEDS_POWER_OF_TWO_FRAME_BUFFER_TEXTURE );
  41. }
  42. bool IsTranslucent( IMaterialVar **params ) const
  43. {
  44. if ( params[CLOAKPASSENABLED]->GetIntValue() ) // If material supports cloaking
  45. {
  46. if ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) // Per-frame check
  47. return true;
  48. // else, not cloaking this frame, so check flag in case the base material still needs it
  49. }
  50. // Check flag if not drawing cloak pass
  51. return IS_FLAG_SET( MATERIAL_VAR_TRANSLUCENT );
  52. }
  53. In SHADER_INIT_PARAMS()
  54. // Cloak Pass
  55. if ( !params[CLOAKPASSENABLED]->IsDefined() )
  56. {
  57. params[CLOAKPASSENABLED]->SetIntValue( 0 );
  58. }
  59. else if ( params[CLOAKPASSENABLED]->GetIntValue() )
  60. {
  61. CloakBlendedPassVars_t info;
  62. SetupVarsCloakBlendedPass( info );
  63. InitParamsCloakBlendedPass( this, params, pMaterialName, info );
  64. }
  65. In SHADER_INIT
  66. // Cloak Pass
  67. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  68. {
  69. CloakBlendedPassVars_t info;
  70. SetupVarsCloakBlendedPass( info );
  71. InitCloakBlendedPass( this, params, info );
  72. }
  73. Modify SHADER_DRAW to look something like this:
  74. // Skip the standard rendering if cloak pass is fully opaque
  75. bool bDrawStandardPass = true;
  76. if ( params[CLOAKPASSENABLED]->GetIntValue() && ( pShaderShadow == NULL ) ) // && not snapshotting
  77. {
  78. CloakBlendedPassVars_t info;
  79. SetupVarsCloakBlendedPass( info );
  80. if ( CloakBlendedPassIsFullyOpaque( params, info ) )
  81. {
  82. bDrawStandardPass = false;
  83. }
  84. }
  85. // Standard rendering pass
  86. if ( bDrawStandardPass )
  87. {
  88. Eye_Refract_Vars_t info;
  89. SetupVarsEyeRefract( info );
  90. Draw_Eyes_Refract( this, params, pShaderAPI, pShaderShadow, info );
  91. }
  92. else
  93. {
  94. // Skip this pass!
  95. Draw( false );
  96. }
  97. // Cloak Pass
  98. if ( params[CLOAKPASSENABLED]->GetIntValue() )
  99. {
  100. // If ( snapshotting ) or ( we need to draw this frame )
  101. if ( ( pShaderShadow != NULL ) || ( ( params[CLOAKFACTOR]->GetFloatValue() > 0.0f ) && ( params[CLOAKFACTOR]->GetFloatValue() < 1.0f ) ) )
  102. {
  103. CloakBlendedPassVars_t info;
  104. SetupVarsCloakBlendedPass( info );
  105. DrawCloakBlendedPass( this, params, pShaderAPI, pShaderShadow, info );
  106. }
  107. else // We're not snapshotting and we don't need to draw this frame
  108. {
  109. // Skip this pass!
  110. Draw( false );
  111. }
  112. }
  113. ==================================================================================================== */
  114. #include "BaseVSShader.h"
  115. #include "mathlib/vmatrix.h"
  116. #include "cloak_blended_pass_helper.h"
  117. #include "convar.h"
  118. // Auto generated inc files
  119. #include "cloak_blended_pass_vs20.inc"
  120. #include "cloak_blended_pass_ps20.inc"
  121. #include "cloak_blended_pass_ps20b.inc"
  122. #ifndef _X360
  123. #include "cloak_blended_pass_vs30.inc"
  124. #include "cloak_blended_pass_ps30.inc"
  125. #endif
  126. void InitParamsCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, CloakBlendedPassVars_t &info )
  127. {
  128. // Set material flags
  129. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  130. SET_FLAGS( MATERIAL_VAR_MODEL );
  131. SET_FLAGS2( MATERIAL_VAR2_NEEDS_TANGENT_SPACES );
  132. // Set material parameter default values
  133. if ( ( info.m_nCloakFactor != -1 ) && ( !params[info.m_nCloakFactor]->IsDefined() ) )
  134. {
  135. params[info.m_nCloakFactor]->SetFloatValue( kDefaultCloakFactor );
  136. }
  137. if ( ( info.m_nRefractAmount != -1 ) && ( !params[info.m_nRefractAmount]->IsDefined() ) )
  138. {
  139. params[info.m_nRefractAmount]->SetFloatValue( kDefaultRefractAmount );
  140. }
  141. if ( ( info.m_nCloakColorTint != -1 ) && ( !params[info.m_nCloakColorTint]->IsDefined() ) )
  142. {
  143. params[info.m_nCloakColorTint]->SetVecValue( kDefaultCloakColorTint[0], kDefaultCloakColorTint[1], kDefaultCloakColorTint[2], kDefaultCloakColorTint[3] );
  144. }
  145. if( (info.m_nBumpFrame != -1 ) && !params[info.m_nBumpFrame]->IsDefined() )
  146. {
  147. params[info.m_nBumpFrame]->SetIntValue( 0 );
  148. }
  149. }
  150. void InitCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, CloakBlendedPassVars_t &info )
  151. {
  152. // Load textures
  153. if ( g_pConfig->UseBumpmapping() )
  154. {
  155. if ( (info.m_nBumpmap != -1) && params[info.m_nBumpmap]->IsDefined() )
  156. {
  157. pShader->LoadTexture( info.m_nBumpmap );
  158. }
  159. }
  160. }
  161. void DrawCloakBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  162. IShaderShadow* pShaderShadow, CloakBlendedPassVars_t &info, VertexCompressionType_t vertexCompression )
  163. {
  164. bool bBumpMapping = ( !g_pConfig->UseBumpmapping() ) || ( info.m_nBumpmap == -1 ) || !params[info.m_nBumpmap]->IsTexture() ? 0 : 1;
  165. SHADOW_STATE
  166. {
  167. // Reset shadow state manually since we're drawing from two materials
  168. pShader->SetInitialShadowState( );
  169. // Set stream format (note that this shader supports compression)
  170. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  171. int nTexCoordCount = 1;
  172. int userDataSize = 0;
  173. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  174. #ifndef _X360
  175. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  176. #endif
  177. {
  178. // Vertex Shader
  179. DECLARE_STATIC_VERTEX_SHADER( cloak_blended_pass_vs20 );
  180. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  181. SET_STATIC_VERTEX_SHADER( cloak_blended_pass_vs20 );
  182. // Pixel Shader
  183. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  184. {
  185. DECLARE_STATIC_PIXEL_SHADER( cloak_blended_pass_ps20b );
  186. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  187. SET_STATIC_PIXEL_SHADER( cloak_blended_pass_ps20b );
  188. }
  189. else
  190. {
  191. DECLARE_STATIC_PIXEL_SHADER( cloak_blended_pass_ps20 );
  192. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  193. SET_STATIC_PIXEL_SHADER( cloak_blended_pass_ps20 );
  194. }
  195. }
  196. #ifndef _X360
  197. else
  198. {
  199. // The vertex shader uses the vertex id stream
  200. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  201. // Vertex Shader
  202. DECLARE_STATIC_VERTEX_SHADER( cloak_blended_pass_vs30 );
  203. SET_STATIC_VERTEX_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  204. SET_STATIC_VERTEX_SHADER( cloak_blended_pass_vs30 );
  205. // Pixel Shader
  206. DECLARE_STATIC_PIXEL_SHADER( cloak_blended_pass_ps30 );
  207. SET_STATIC_PIXEL_SHADER_COMBO( BUMPMAP, bBumpMapping ? 1 : 0 );
  208. SET_STATIC_PIXEL_SHADER( cloak_blended_pass_ps30 );
  209. }
  210. #endif
  211. // Textures
  212. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // Refraction texture
  213. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  214. if ( bBumpMapping )
  215. {
  216. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Bump
  217. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Not sRGB
  218. }
  219. pShaderShadow->EnableSRGBWrite( true );
  220. // Blending
  221. pShader->EnableAlphaBlending( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  222. pShaderShadow->EnableAlphaWrites( false );
  223. // !!! We need to turn this back on because EnableAlphaBlending() above disables it!
  224. pShaderShadow->EnableDepthWrites( true );
  225. }
  226. DYNAMIC_STATE
  227. {
  228. // Reset render state manually since we're drawing from two materials
  229. pShaderAPI->SetDefaultState();
  230. // Set Vertex Shader Constants
  231. if ( ( bBumpMapping ) && ( info.m_nBumpTransform != -1 ) )
  232. {
  233. pShader->SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBumpTransform );
  234. }
  235. #ifndef _X360
  236. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  237. #endif
  238. {
  239. // Set Vertex Shader Combos
  240. DECLARE_DYNAMIC_VERTEX_SHADER( cloak_blended_pass_vs20 );
  241. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  242. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  243. SET_DYNAMIC_VERTEX_SHADER( cloak_blended_pass_vs20 );
  244. // Set Pixel Shader Combos
  245. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  246. {
  247. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps20b );
  248. SET_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps20b );
  249. }
  250. else
  251. {
  252. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps20 );
  253. SET_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps20 );
  254. }
  255. }
  256. #ifndef _X360
  257. else
  258. {
  259. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  260. // Set Vertex Shader Combos
  261. DECLARE_DYNAMIC_VERTEX_SHADER( cloak_blended_pass_vs30 );
  262. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  263. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  264. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  265. SET_DYNAMIC_VERTEX_SHADER( cloak_blended_pass_vs30 );
  266. // Set Pixel Shader Combos
  267. DECLARE_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps30 );
  268. SET_DYNAMIC_PIXEL_SHADER( cloak_blended_pass_ps30 );
  269. }
  270. #endif
  271. // Bind textures
  272. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_FRAME_BUFFER_FULL_TEXTURE_0 ); // Refraction Map
  273. if ( bBumpMapping )
  274. {
  275. pShader->BindTexture( SHADER_SAMPLER1, info.m_nBumpmap, info.m_nBumpFrame );
  276. }
  277. // Set Pixel Shader Constants
  278. float vEyePos[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  279. pShaderAPI->GetWorldSpaceCameraPosition( vEyePos );
  280. pShaderAPI->SetPixelShaderConstant( 5, vEyePos, 1 );
  281. float vPackedConst1[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  282. vPackedConst1[0] = IS_PARAM_DEFINED( info.m_nCloakFactor ) ? params[info.m_nCloakFactor]->GetFloatValue() : kDefaultCloakFactor;
  283. vPackedConst1[1] = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  284. pShaderAPI->SetPixelShaderConstant( 6, vPackedConst1, 1 );
  285. // Refract color tint
  286. pShaderAPI->SetPixelShaderConstant( 7, IS_PARAM_DEFINED( info.m_nCloakColorTint ) ? params[info.m_nCloakColorTint]->GetVecValue() : kDefaultCloakColorTint, 1 );
  287. // Set c0 and c1 to contain first two rows of ViewProj matrix
  288. VMatrix mView, mProj;
  289. pShaderAPI->GetMatrix( MATERIAL_VIEW, mView.m[0] );
  290. pShaderAPI->GetMatrix( MATERIAL_PROJECTION, mProj.m[0] );
  291. VMatrix mViewProj = mView * mProj;
  292. mViewProj = mViewProj.Transpose3x3();
  293. pShaderAPI->SetPixelShaderConstant( 0, mViewProj.m[0], 2 );
  294. }
  295. pShader->Draw();
  296. }
  297. bool CloakBlendedPassIsFullyOpaque ( IMaterialVar** params, CloakBlendedPassVars_t &info )
  298. {
  299. float flCloakFactor = IS_PARAM_DEFINED( info.m_nCloakFactor ) ? params[info.m_nCloakFactor]->GetFloatValue() : kDefaultCloakFactor;
  300. //float flRefractAmount = IS_PARAM_DEFINED( info.m_nRefractAmount ) ? params[info.m_nRefractAmount]->GetFloatValue() : kDefaultRefractAmount;
  301. // NOTE: If this math changes, you need to update the pixel shader code!
  302. float flFresnel = 1.0f - ( 0.0f ); // Assume V.N = 0.0f;
  303. float flCloakLerpFactor = clamp( Lerp( clamp( flCloakFactor, 0.0f, 1.0f ), 1.0f, flFresnel - 1.35f ), 0.0f, 1.0f );
  304. //flCloakLerpFactor = 1.0f - smoothstep( 0.4f, 0.425f, flCloakLerpFactor );
  305. if ( flCloakLerpFactor <= 0.4f )
  306. return true;
  307. return false;
  308. }