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.

268 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Header: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "shaderlib/cshader.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. DEFINE_FALLBACK_SHADER( Vortwarp, VortWarp_DX7 )
  12. float QuadraticBezier( float t, float A, float B, float C )
  13. {
  14. return Lerp( t, Lerp( t, A, B ), Lerp( t, B, C ) );
  15. }
  16. float CubicBezier( float t, float A, float B, float C, float D )
  17. {
  18. return QuadraticBezier( t, Lerp( t, A, B ), Lerp( t, B, C ), Lerp( t, C, D ) );
  19. }
  20. BEGIN_SHADER( VortWarp_DX7,
  21. "Help for VortWarp_DX7" )
  22. BEGIN_SHADER_PARAMS
  23. SHADER_PARAM( SELFILLUMTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  24. SHADER_PARAM( ENTITYORIGIN, SHADER_PARAM_TYPE_VEC3,"0.0","center if the model in world space" )
  25. SHADER_PARAM( WARPPARAM, SHADER_PARAM_TYPE_FLOAT,"0.0","animation param between 0 and 1" )
  26. SHADER_PARAM( SELFILLUMMAP, SHADER_PARAM_TYPE_TEXTURE, "", "self-illumination map" )
  27. SHADER_PARAM( UNLIT, SHADER_PARAM_TYPE_BOOL, "", "" )
  28. END_SHADER_PARAMS
  29. SHADER_INIT_PARAMS()
  30. {
  31. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  32. if( !params[SELFILLUMTINT]->IsDefined() )
  33. params[SELFILLUMTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  34. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  35. if( !params[UNLIT]->IsDefined() )
  36. {
  37. params[UNLIT]->SetIntValue( 0 );
  38. }
  39. }
  40. SHADER_FALLBACK
  41. {
  42. if (g_pHardwareConfig->GetDXSupportLevel() < 70)
  43. return "VertexLitGeneric_DX6";
  44. return 0;
  45. }
  46. SHADER_INIT
  47. {
  48. if (params[BASETEXTURE]->IsDefined())
  49. {
  50. LoadTexture( BASETEXTURE );
  51. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  52. {
  53. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  54. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  55. }
  56. }
  57. if( params[SELFILLUMMAP]->IsDefined() )
  58. {
  59. LoadTexture( SELFILLUMMAP );
  60. }
  61. // Don't alpha test if the alpha channel is used for other purposes
  62. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  63. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  64. }
  65. void DrawBaseTimesVertexColor( bool bUnlit, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  66. {
  67. SHADOW_STATE
  68. {
  69. // alpha test
  70. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  71. pShaderShadow->EnableCustomPixelPipe( true );
  72. pShaderShadow->CustomTextureStages( 1 );
  73. if( bUnlit )
  74. {
  75. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  76. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_SELECTARG1,
  77. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_NONE );
  78. }
  79. else
  80. {
  81. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  82. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATE2X,
  83. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_VERTEXCOLOR );
  84. }
  85. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  86. SHADER_TEXCHANNEL_ALPHA, SHADER_TEXOP_SELECTARG1,
  87. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_NONE );
  88. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  89. int flags = SHADER_DRAW_POSITION | SHADER_DRAW_NORMAL | SHADER_DRAW_TEXCOORD0;
  90. pShaderShadow->DrawFlags( flags );
  91. DefaultFog();
  92. pShaderShadow->CustomTextureStages( 2 );
  93. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  94. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_SELECTARG1,
  95. SHADER_TEXARG_PREVIOUSSTAGE, SHADER_TEXARG_NONE );
  96. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  97. SHADER_TEXCHANNEL_ALPHA, SHADER_TEXOP_ADD,
  98. SHADER_TEXARG_PREVIOUSSTAGE, SHADER_TEXARG_CONSTANTCOLOR );
  99. // for warping in
  100. pShaderShadow->EnableAlphaTest( true );
  101. pShaderShadow->AlphaFunc( SHADER_ALPHAFUNC_GEQUAL, 1.0f );
  102. // SetDefaultBlendingShadowState( BASETEXTURE, true );
  103. }
  104. DYNAMIC_STATE
  105. {
  106. SetFixedFunctionTextureTransform( MATERIAL_TEXTURE0, BASETEXTURETRANSFORM );
  107. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  108. float warp = params[WARPPARAM]->GetFloatValue();
  109. float t = warp;
  110. warp = CubicBezier( warp, 0.0f, 1.0f, 0.0f, 0.0f );
  111. warp = Lerp( t, warp, 1.0f );
  112. pShaderAPI->Color4f( 1.0f, 1.0f, 1.0f, warp );
  113. }
  114. Draw();
  115. }
  116. int GetDrawFlagsPass1(IMaterialVar** params)
  117. {
  118. int flags = SHADER_DRAW_POSITION;
  119. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  120. flags |= SHADER_DRAW_COLOR;
  121. if (params[BASETEXTURE]->IsTexture())
  122. flags |= SHADER_DRAW_TEXCOORD0;
  123. return flags;
  124. }
  125. void DrawUnlit( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  126. {
  127. SHADOW_STATE
  128. {
  129. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  130. SetModulationShadowState();
  131. SetNormalBlendingShadowState( BASETEXTURE, true );
  132. pShaderShadow->DrawFlags( GetDrawFlagsPass1(params) );
  133. FogToFogColor();
  134. }
  135. DYNAMIC_STATE
  136. {
  137. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  138. SetFixedFunctionTextureTransform( MATERIAL_TEXTURE0, BASETEXTURETRANSFORM );
  139. SetModulationDynamicState();
  140. }
  141. Draw( );
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Fixed function Self illumination pass
  145. //-----------------------------------------------------------------------------
  146. void ScrollySelfIllumPass( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  147. {
  148. SHADOW_STATE
  149. {
  150. SetInitialShadowState();
  151. // A little setup for self illum here...
  152. SetModulationShadowState( SELFILLUMTINT );
  153. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  154. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  155. // Don't bother with z writes here...
  156. s_pShaderShadow->EnableDepthWrites( false );
  157. pShaderShadow->EnableCustomPixelPipe( true );
  158. pShaderShadow->CustomTextureStages( 2 );
  159. // basetexture * selfillumtint
  160. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  161. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATE,
  162. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_CONSTANTCOLOR );
  163. // previous * selfillummap
  164. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  165. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATE,
  166. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_PREVIOUSSTAGE );
  167. // We're always blending
  168. EnableAlphaBlending( SHADER_BLEND_ONE, SHADER_BLEND_ONE );
  169. s_pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_TEXCOORD1 );
  170. FogToFogColor();
  171. }
  172. DYNAMIC_STATE
  173. {
  174. s_pShaderAPI->SetDefaultState();
  175. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  176. BindTexture( SHADER_SAMPLER1, SELFILLUMMAP, -1 );
  177. // -----------------------------------------------------------
  178. // Set up the self illum tint
  179. // -----------------------------------------------------------
  180. float selfIllumTint[3];
  181. params[SELFILLUMTINT]->GetVecValue( selfIllumTint, 3 );
  182. selfIllumTint[0] = clamp( selfIllumTint[0], 0.0f, 1.0f );
  183. selfIllumTint[1] = clamp( selfIllumTint[1], 0.0f, 1.0f );
  184. selfIllumTint[2] = clamp( selfIllumTint[2], 0.0f, 1.0f );
  185. // Warning( "selfillumtint: %f %f %f\n", selfIllumTint[0], selfIllumTint[1], selfIllumTint[2] );
  186. pShaderAPI->Color4f( selfIllumTint[0], selfIllumTint[1], selfIllumTint[2], 1.0f );
  187. // -----------------------------------------------------------
  188. // Set up the self illum scrolling
  189. // -----------------------------------------------------------
  190. float curTime = pShaderAPI->CurrentTime();
  191. pShaderAPI->MatrixMode( MATERIAL_TEXTURE0 );
  192. // only do the upper 3x3 since this is a 2D matrix
  193. float mat[16];
  194. mat[0] = 1.0f; mat[1] = 0.0f; mat[2] = 0.0f;
  195. mat[4] = 0.0f; mat[5] = 1.0f; mat[6] = 0.0f;
  196. mat[8] = .11f * curTime; mat[9] = .124 * curTime; mat[10] = 1.0f;
  197. // Better set the stuff we don't set with some sort of value!
  198. mat[3] = mat[7] = mat[11] = 0;
  199. mat[12] = mat[13] = mat[14] = 0;
  200. mat[15] = 1;
  201. s_pShaderAPI->LoadMatrix( mat );
  202. }
  203. Draw();
  204. }
  205. SHADER_DRAW
  206. {
  207. bool hasFlashlight = UsingFlashlight( params );
  208. if( hasFlashlight )
  209. {
  210. // DrawFlashlight_dx70( params, pShaderAPI, pShaderShadow, FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME );
  211. return;
  212. }
  213. // Draw base times lighting.
  214. // Lighting is either sent down per vertex from the app, or it's in the second
  215. // stream as color values.
  216. DrawBaseTimesVertexColor( params[UNLIT]->GetIntValue() != 0, params, pShaderAPI, pShaderShadow );
  217. // ScrollySelfIllumPass( params, pShaderAPI, pShaderShadow );
  218. }
  219. END_SHADER