Counter Strike : Global Offensive Source Code
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.

281 lines
11 KiB

  1. //========= Copyright � 1996-2006, 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. #if !defined( _X360 ) && !defined( _PS3 )
  86. #include "emissive_scroll_blended_pass_vs30.inc"
  87. #include "emissive_scroll_blended_pass_ps30.inc"
  88. #endif
  89. // NOTE: This has to be the last file included!
  90. #include "tier0/memdbgon.h"
  91. void InitParamsEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, EmissiveScrollBlendedPassVars_t &info )
  92. {
  93. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  94. if ( ( info.m_nEmissiveScrollVector != -1 ) && ( !params[info.m_nEmissiveScrollVector]->IsDefined() ) )
  95. {
  96. params[info.m_nEmissiveScrollVector]->SetVecValue( kDefaultEmissiveScrollVector, 4 );
  97. }
  98. if ( ( info.m_nBlendStrength != -1 ) && ( !params[info.m_nBlendStrength]->IsDefined() ) )
  99. {
  100. params[info.m_nBlendStrength]->SetFloatValue( kDefaultEmissiveBlendStrength );
  101. }
  102. if ( ( info.m_nEmissiveTint != -1 ) && ( !params[info.m_nEmissiveTint]->IsDefined() ) )
  103. {
  104. params[info.m_nEmissiveTint]->SetVecValue( kDefaultEmissiveTint, 4 );
  105. }
  106. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nTime, 0.0f );
  107. }
  108. void InitEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, EmissiveScrollBlendedPassVars_t &info )
  109. {
  110. // Load textures
  111. pShader->LoadTexture( info.m_nBaseTexture, TEXTUREFLAGS_SRGB );
  112. pShader->LoadTexture( info.m_nFlowTexture );
  113. pShader->LoadTexture( info.m_nEmissiveTexture, TEXTUREFLAGS_SRGB );
  114. }
  115. void DrawEmissiveScrollBlendedPass( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  116. IShaderShadow* pShaderShadow, EmissiveScrollBlendedPassVars_t &info, VertexCompressionType_t vertexCompression )
  117. {
  118. SHADOW_STATE
  119. {
  120. // Reset shadow state manually since we're drawing from two materials
  121. pShader->SetInitialShadowState();
  122. // Set stream format (note that this shader supports compression)
  123. unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_COMPRESSED;
  124. int nTexCoordCount = 1;
  125. int userDataSize = 0;
  126. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  127. #if !defined( _X360 ) && !defined( _PS3 )
  128. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  129. #endif
  130. {
  131. // Vertex Shader
  132. DECLARE_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  133. SET_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  134. // Pixel Shader
  135. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  136. {
  137. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  138. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  139. }
  140. else
  141. {
  142. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  143. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  144. }
  145. }
  146. #if !defined( _X360 ) && !defined( _PS3 )
  147. else
  148. {
  149. // The vertex shader uses the vertex id stream
  150. SET_FLAGS2( MATERIAL_VAR2_USES_VERTEXID );
  151. DECLARE_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  152. SET_STATIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  153. DECLARE_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  154. SET_STATIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  155. }
  156. #endif
  157. // Textures
  158. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  159. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER0, true );
  160. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  161. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER1, false ); // Flow texture not sRGB
  162. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  163. pShaderShadow->EnableSRGBRead( SHADER_SAMPLER2, true );
  164. pShaderShadow->EnableSRGBWrite( true );
  165. // Blending
  166. pShader->EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  167. pShaderShadow->EnableAlphaWrites( false );
  168. }
  169. DYNAMIC_STATE
  170. {
  171. // Reset render state manually since we're drawing from two materials
  172. pShaderAPI->SetDefaultState();
  173. #if !defined( _X360 ) && !defined( _PS3 )
  174. if ( !g_pHardwareConfig->HasFastVertexTextures() )
  175. #endif
  176. {
  177. // Set Vertex Shader Combos
  178. DECLARE_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  179. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  180. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  181. SET_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs20 );
  182. // Set Vertex Shader Constants
  183. // None?
  184. // Set Pixel Shader Combos
  185. if( g_pHardwareConfig->SupportsPixelShaders_2_b() )
  186. {
  187. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  188. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20b );
  189. }
  190. else
  191. {
  192. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  193. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps20 );
  194. }
  195. }
  196. #if !defined( _X360 ) && !defined( _PS3 )
  197. else
  198. {
  199. pShader->SetHWMorphVertexShaderState( VERTEX_SHADER_SHADER_SPECIFIC_CONST_6, VERTEX_SHADER_SHADER_SPECIFIC_CONST_7, SHADER_VERTEXTEXTURE_SAMPLER0 );
  200. // Set Vertex Shader Combos
  201. DECLARE_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  202. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, pShaderAPI->GetCurrentNumBones() > 0 );
  203. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  204. SET_DYNAMIC_VERTEX_SHADER( emissive_scroll_blended_pass_vs30 );
  205. DECLARE_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  206. SET_DYNAMIC_PIXEL_SHADER( emissive_scroll_blended_pass_ps30 );
  207. }
  208. #endif
  209. // Bind textures
  210. pShader->BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture );
  211. pShader->BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, info.m_nFlowTexture );
  212. pShader->BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nEmissiveTexture );
  213. // Set Pixel Shader Constants
  214. //float vConstZero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  215. // This brings in the electricity and the second base texture when the second base texture is present
  216. float vPsConst0[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  217. if (1)
  218. {
  219. // Overall blend strength
  220. vPsConst0[0] = IS_PARAM_DEFINED( info.m_nBlendStrength ) ? params[info.m_nBlendStrength]->GetFloatValue() : kDefaultEmissiveBlendStrength;
  221. if ( vPsConst0[0] < 0.0f )
  222. vPsConst0[0] = 0.0f;
  223. if ( vPsConst0[0] > 1.0f )
  224. vPsConst0[0] = 1.0f;
  225. // Time % 1000 for scrolling
  226. vPsConst0[1] = IS_PARAM_DEFINED( info.m_nTime ) && params[info.m_nTime]->GetFloatValue() > 0.0f ? params[info.m_nTime]->GetFloatValue() : pShaderAPI->CurrentTime();
  227. vPsConst0[1] -= (float)( (int)( vPsConst0[1] / 1000.0f ) ) * 1000.0f;
  228. // Dest alpha value for warping mask - NOTE: If we want to use this, we have to modify the blending mode above!
  229. //if ( ( params[info.m_nWarpParam]->GetFloatValue() > 0.0f ) && ( params[info.m_nWarpParam]->GetFloatValue() < 1.0f ) )
  230. // tmpVec[2] = 1.0f;
  231. //else
  232. // tmpVec[2] = 0.0f;
  233. }
  234. pShaderAPI->SetPixelShaderConstant( 0, vPsConst0, 1 );
  235. // Scroll vector
  236. pShaderAPI->SetPixelShaderConstant( 1, IS_PARAM_DEFINED( info.m_nEmissiveScrollVector ) ? params[info.m_nEmissiveScrollVector]->GetVecValue() : kDefaultEmissiveScrollVector, 1 );
  237. // Self illum tint
  238. pShaderAPI->SetPixelShaderConstant( 2, IS_PARAM_DEFINED( info.m_nEmissiveTint ) ? params[info.m_nEmissiveTint]->GetVecValue() : kDefaultEmissiveTint, 1 );
  239. }
  240. pShader->Draw();
  241. }