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.

278 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. /* Example how to plug this into an existing shader:
  3. In the VMT:
  4. // Emissive Scroll Pass
  5. "$emissiveBlendEnabled" "1" // Enables effect
  6. "$emissiveBlendTexture" "models/vortigaunt/vortigaunt_illum"
  7. "$emissiveBlendBaseTexture" "Models/Vortigaunt/vortigaunt_blue"
  8. "$emissiveBlendFlowTexture" "models/vortigaunt/vortigaunt_flow"
  9. "$emissiveBlendTint" "[10 10 10]"
  10. "$emissiveBlendStrength" "1.0" // Set by game code
  11. "$emissiveBlendScrollVector" "[0.11 0.124]"
  12. "Proxies"
  13. {
  14. "VortEmissive" // For setting $selfillumstrength
  15. {
  16. }
  17. }
  18. #include "emissive_scroll_blended_pass_helper.h"
  19. In BEGIN_SHADER_PARAMS:
  20. // Emissive Scroll Pass
  21. SHADER_PARAM( EMISSIVEBLENDENABLED, SHADER_PARAM_TYPE_BOOL, "0", "Enable emissive blend pass" )
  22. SHADER_PARAM( EMISSIVEBLENDBASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "self-illumination map" )
  23. SHADER_PARAM( EMISSIVEBLENDSCROLLVECTOR, SHADER_PARAM_TYPE_VEC2, "[0.11 0.124]", "Emissive scroll vec" )
  24. SHADER_PARAM( EMISSIVEBLENDSTRENGTH, SHADER_PARAM_TYPE_FLOAT, "1.0", "Emissive blend strength" )
  25. SHADER_PARAM( EMISSIVEBLENDTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "self-illumination map" )
  26. SHADER_PARAM( EMISSIVEBLENDTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  27. SHADER_PARAM( EMISSIVEBLENDFLOWTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "flow map" )
  28. Add this above SHADER_INIT_PARAMS()
  29. // Emissive Scroll Pass
  30. void SetupVarsEmissiveScrollBlendedPass( EmissiveScrollBlendedPassVars_t &info )
  31. {
  32. info.m_nBlendStrength = EMISSIVEBLENDSTRENGTH;
  33. info.m_nBaseTexture = EMISSIVEBLENDBASETEXTURE;
  34. info.m_nFlowTexture = EMISSIVEBLENDFLOWTEXTURE;
  35. info.m_nEmissiveTexture = EMISSIVEBLENDTEXTURE;
  36. info.m_nEmissiveTint = EMISSIVEBLENDTINT;
  37. info.m_nEmissiveScrollVector = EMISSIVEBLENDSCROLLVECTOR;
  38. }
  39. In SHADER_INIT_PARAMS()
  40. // Emissive Scroll Pass
  41. if ( !params[EMISSIVEBLENDENABLED]->IsDefined() )
  42. {
  43. params[EMISSIVEBLENDENABLED]->SetIntValue( 0 );
  44. }
  45. else if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  46. {
  47. EmissiveScrollBlendedPassVars_t info;
  48. SetupVarsEmissiveScrollBlendedPass( info );
  49. InitParamsEmissiveScrollBlendedPass( this, params, pMaterialName, info );
  50. }
  51. In SHADER_INIT
  52. // Emissive Scroll Pass
  53. if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  54. {
  55. EmissiveScrollBlendedPassVars_t info;
  56. SetupVarsEmissiveScrollBlendedPass( info );
  57. InitEmissiveScrollBlendedPass( this, params, info );
  58. }
  59. At the very end of SHADER_DRAW
  60. // Emissive Scroll Pass
  61. if ( params[EMISSIVEBLENDENABLED]->GetIntValue() )
  62. {
  63. // If ( snapshotting ) or ( we need to draw this frame )
  64. if ( ( pShaderShadow != NULL ) || ( params[EMISSIVEBLENDSTRENGTH]->GetFloatValue() > 0.0f ) )
  65. {
  66. EmissiveScrollBlendedPassVars_t info;
  67. SetupVarsEmissiveScrollBlendedPass( info );
  68. DrawEmissiveScrollBlendedPass( this, params, pShaderAPI, pShaderShadow, info );
  69. }
  70. else // We're not snapshotting and we don't need to draw this frame
  71. {
  72. // Skip this pass!
  73. Draw( false );
  74. }
  75. }
  76. ==================================================================================================== */
  77. #include "BaseVSShader.h"
  78. #include "mathlib/vmatrix.h"
  79. #include "emissive_scroll_blended_pass_helper.h"
  80. #include "convar.h"
  81. // Auto generated inc files
  82. #include "emissive_scroll_blended_pass_vs20.inc"
  83. #include "emissive_scroll_blended_pass_ps20.inc"
  84. #include "emissive_scroll_blended_pass_ps20b.inc"
  85. #ifndef _X360
  86. #include "emissive_scroll_blended_pass_vs30.inc"
  87. #include "emissive_scroll_blended_pass_ps30.inc"
  88. #endif
  89. void InitParamsEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, EmissiveScrollBlendedPassVars_t &info )
  90. {
  91. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  92. if ( ( info.m_nEmissiveScrollVector != -1 ) && ( !params[info.m_nEmissiveScrollVector]->IsDefined() ) )
  93. {
  94. params[info.m_nEmissiveScrollVector]->SetVecValue( kDefaultEmissiveScrollVector, 4 );
  95. }
  96. if ( ( info.m_nBlendStrength != -1 ) && ( !params[info.m_nBlendStrength]->IsDefined() ) )
  97. {
  98. params[info.m_nBlendStrength]->SetFloatValue( kDefaultEmissiveBlendStrength );
  99. }
  100. if ( ( info.m_nEmissiveTint != -1 ) && ( !params[info.m_nEmissiveTint]->IsDefined() ) )
  101. {
  102. params[info.m_nEmissiveTint]->SetVecValue( kDefaultEmissiveTint, 4 );
  103. }
  104. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  105. }
  106. void InitEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, EmissiveScrollBlendedPassVars_t &info )
  107. {
  108. // Load textures
  109. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  110. pShader->LoadTexture( info.m_nFlowTexture );
  111. pShader->LoadTexture( info.m_nEmissiveTexture, TEXTUREFLAGS_SRGB );
  112. }
  113. void DrawEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  114. IShaderShadow* pShaderShadow, EmissiveScrollBlendedPassVars_t &info, VertexCompressionType_t vertexCompression )
  115. {
  116. SHADOW_STATE
  117. {
  118. // Reset shadow state manually since we're drawing from two materials
  119. pShader->SetInitialShadowState();
  120. // Set stream format (note that this shader supports compression)
  121. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  122. int nTexCoordCount = 1;
  123. int userDataSize = 0;
  124. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  125. #ifndef _X360
  126. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  127. #endif
  128. {
  129. // Vertex Shader
  130. DECLARE_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  131. SET_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  132. // Pixel Shader
  133. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  134. {
  135. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  136. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  137. }
  138. else
  139. {
  140. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  141. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  142. }
  143. }
  144. #ifndef _X360
  145. else
  146. {
  147. // The vertex shader uses the vertex id stream
  148. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  149. DECLARE_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  150. SET_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  151. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  152. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  153. }
  154. #endif
  155. // Textures
  156. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  157. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  158. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  159. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Flow texture not sRGB
  160. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  161. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  162. pShaderShadow->EnableSRGBWrite( true );
  163. // Blending
  164. pShader->EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  165. pShaderShadow->EnableAlphaWrites( false );
  166. }
  167. DYNAMIC_STATE
  168. {
  169. // Reset render state manually since we're drawing from two materials
  170. pShaderAPI->SetDefaultState();
  171. #ifndef _X360
  172. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  173. #endif
  174. {
  175. // Set Vertex Shader Combos
  176. DECLARE_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  177. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  178. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  179. SET_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  180. // Set Vertex Shader Constants
  181. // None?
  182. // Set Pixel Shader Combos
  183. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  184. {
  185. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  186. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  187. }
  188. else
  189. {
  190. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  191. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  192. }
  193. }
  194. #ifndef _X360
  195. else
  196. {
  197. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  198. // Set Vertex Shader Combos
  199. DECLARE_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  200. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  201. SET_DYNAMIC_VERTEX_SHADER_COMBO( MORPHING, pShaderAPI->IsHWMorphingEnabled() );
  202. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  203. SET_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  204. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  205. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  206. }
  207. #endif
  208. // Bind textures
  209. pShader->BindTexture( SHADER_SAMPLER0, info.m_nBaseTexture );
  210. pShader->BindTexture( SHADER_SAMPLER1, info.m_nFlowTexture );
  211. pShader->BindTexture( SHADER_SAMPLER2, info.m_nEmissiveTexture );
  212. // Set Pixel Shader Constants
  213. //float vConstZero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  214. // This brings in the electricity and the second base texture when the second base texture is present
  215. float vPsConst0[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  216. if (1)
  217. {
  218. // Overall blend strength
  219. vPsConst0[0] = IS_PARAM_DEFINED( info.m_nBlendStrength ) ? params[info.m_nBlendStrength]->GetFloatValue() : kDefaultEmissiveBlendStrength;
  220. if ( vPsConst0[0] < 0.0f )
  221. vPsConst0[0] = 0.0f;
  222. if ( vPsConst0[0] > 1.0f )
  223. vPsConst0[0] = 1.0f;
  224. // Time % 1000 for scrolling
  225. vPsConst0[1] = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  226. vPsConst0[1] -= (float)( (int)( vPsConst0[1] / 1000.0f ) ) * 1000.0f;
  227. // Dest alpha value for warping mask - NOTE: If we want to use this, we have to modify the blending mode above!
  228. //if ( ( params[info.m_nWarpParam]->GetFloatValue() > 0.0f ) && ( params[info.m_nWarpParam]->GetFloatValue() < 1.0f ) )
  229. // tmpVec[2] = 1.0f;
  230. //else
  231. // tmpVec[2] = 0.0f;
  232. }
  233. pShaderAPI->SetPixelShaderConstant( 0, vPsConst0, 1 );
  234. // Scroll vector
  235. pShaderAPI->SetPixelShaderConstant( 1, IS_PARAM_DEFINED( info.m_nEmissiveScrollVector ) ? params[info.m_nEmissiveScrollVector]->GetVecValue() : kDefaultEmissiveScrollVector, 1 );
  236. // Self illum tint
  237. pShaderAPI->SetPixelShaderConstant( 2, IS_PARAM_DEFINED( info.m_nEmissiveTint ) ? params[info.m_nEmissiveTint]->GetVecValue() : kDefaultEmissiveTint, 1 );
  238. }
  239. pShader->Draw();
  240. }