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.

258 lines
7.5 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( WorldTwoTextureBlend, WorldTwoTextureBlend_DX6 )
  12. BEGIN_SHADER( WorldTwoTextureBlend_DX6,
  13. "Help for WorldTwoTextureBlend" )
  14. BEGIN_SHADER_PARAMS
  15. SHADER_PARAM_OVERRIDE( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "shadertest/WorldTwoTextureBlend", "iris texture", 0 )
  16. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/WorldTwoTextureBlend_detail", "detail texture" )
  17. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "1.0", "scale of the detail texture" )
  18. SHADER_PARAM( DETAIL_ALPHA_MASK_BASE_TEXTURE, SHADER_PARAM_TYPE_BOOL, "0",
  19. "If this is 1, then when detail alpha=0, no base texture is blended and when "
  20. "detail alpha=1, you get detail*base*lightmap" )
  21. END_SHADER_PARAMS
  22. SHADER_INIT
  23. {
  24. LoadTexture( FLASHLIGHTTEXTURE );
  25. LoadTexture( BASETEXTURE );
  26. LoadTexture( DETAIL );
  27. }
  28. SHADER_INIT_PARAMS()
  29. {
  30. // FLASHLIGHTFIXME
  31. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  32. if( !params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->IsDefined() )
  33. params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->SetIntValue( 0 );
  34. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  35. }
  36. SHADER_DRAW
  37. {
  38. float detailScale = params[DETAILSCALE]->GetFloatValue();
  39. bool hasFlashlight = UsingFlashlight( params );
  40. if( hasFlashlight )
  41. {
  42. DrawFlashlight_dx70( params, pShaderAPI, pShaderShadow, FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME );
  43. return;
  44. }
  45. // DX6 fallback mode.
  46. if ( params[DETAIL_ALPHA_MASK_BASE_TEXTURE]->GetIntValue() )
  47. {
  48. DetailAlphaMaskPass1( pShaderShadow, pShaderAPI, params, detailScale );
  49. DetailAlphaMaskPass2( pShaderShadow, pShaderAPI, detailScale );
  50. }
  51. else
  52. {
  53. // FIXME: add multitexture support!
  54. NormalModePass1( pShaderShadow, pShaderAPI );
  55. NormalModePass2( pShaderShadow, pShaderAPI, params, detailScale );
  56. NormalModePass3( pShaderShadow, pShaderAPI, params, detailScale );
  57. }
  58. }
  59. // ------------------------------------------------------------------------------ //
  60. // "Normal" mode - doesn't use the detail texture's alpha mask.
  61. // ------------------------------------------------------------------------------ //
  62. void NormalModePass1(
  63. IShaderShadow *pShaderShadow,
  64. IShaderDynamicAPI *pShaderAPI )
  65. {
  66. SHADOW_STATE
  67. {
  68. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  69. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  70. FogToFogColor();
  71. }
  72. DYNAMIC_STATE
  73. {
  74. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  75. }
  76. Draw();
  77. }
  78. void NormalModePass2(
  79. IShaderShadow *pShaderShadow,
  80. IShaderDynamicAPI *pShaderAPI,
  81. IMaterialVar **params,
  82. float detailScale )
  83. {
  84. SHADOW_STATE
  85. {
  86. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  87. pShaderShadow->EnableBlending( true );
  88. pShaderShadow->BlendFunc( SHADER_BLEND_SRC_ALPHA, SHADER_BLEND_ONE_MINUS_SRC_ALPHA );
  89. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  90. FogToFogColor();
  91. }
  92. DYNAMIC_STATE
  93. {
  94. if ( detailScale != 1.0f )
  95. {
  96. pShaderAPI->MatrixMode( MATERIAL_TEXTURE0 );
  97. pShaderAPI->LoadIdentity();
  98. pShaderAPI->ScaleXY( detailScale, detailScale );
  99. }
  100. BindTexture( SHADER_SAMPLER0, DETAIL );
  101. }
  102. Draw();
  103. }
  104. void NormalModePass3(
  105. IShaderShadow *pShaderShadow,
  106. IShaderDynamicAPI *pShaderAPI,
  107. IMaterialVar **params,
  108. float detailScale )
  109. {
  110. SHADOW_STATE
  111. {
  112. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  113. SingleTextureLightmapBlendMode();
  114. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_LIGHTMAP_TEXCOORD0 );
  115. FogToOOOverbright();
  116. }
  117. DYNAMIC_STATE
  118. {
  119. if ( detailScale != 1.0f )
  120. pShaderAPI->LoadIdentity( );
  121. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_LIGHTMAP );
  122. }
  123. Draw();
  124. }
  125. // ------------------------------------------------------------------------------ //
  126. // "Detail alpha mask mode".
  127. // ------------------------------------------------------------------------------ //
  128. void DetailAlphaMaskPass1(
  129. IShaderShadow *pShaderShadow,
  130. IShaderDynamicAPI *pShaderAPI,
  131. IMaterialVar **params,
  132. float detailScale )
  133. {
  134. // The equation is [B*Da + (1-Da)] * [D * L]
  135. SHADOW_STATE
  136. {
  137. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  138. pShaderShadow->EnableCustomPixelPipe( true );
  139. pShaderShadow->CustomTextureStages( 2 );
  140. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  141. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  142. // Stage 0
  143. // Color = B*2
  144. // Note the 2x here.. we do 4x total in this shader and
  145. // the first 2x is here. The second is in SingleTextureLightmapBlendMode in the 2nd pass.
  146. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  147. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATE2X,
  148. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_CONSTANTCOLOR );
  149. // Stage 1 [where P = prev stage]
  150. // Color = B*Da + (1-Da)
  151. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  152. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATEINVCOLOR_ADDALPHA,
  153. SHADER_TEXARG_INVTEXTUREALPHA, SHADER_TEXARG_PREVIOUSSTAGE );
  154. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_TEXCOORD1 );
  155. FogToFogColor();
  156. }
  157. DYNAMIC_STATE
  158. {
  159. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  160. BindTexture( SHADER_SAMPLER1, DETAIL );
  161. pShaderAPI->Color4f( 1, 1, 1, 1 );
  162. if ( detailScale != 1.0f )
  163. {
  164. pShaderAPI->MatrixMode( MATERIAL_TEXTURE1 );
  165. pShaderAPI->LoadIdentity();
  166. pShaderAPI->ScaleXY( detailScale, detailScale );
  167. }
  168. }
  169. Draw();
  170. }
  171. void DetailAlphaMaskPass2( IShaderShadow *pShaderShadow, IShaderDynamicAPI *pShaderAPI, float detailScale )
  172. {
  173. SHADOW_STATE
  174. {
  175. s_pShaderShadow->EnableCustomPixelPipe( true );
  176. s_pShaderShadow->CustomTextureStages( 2 );
  177. s_pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  178. s_pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  179. s_pShaderShadow->EnableTexGen( SHADER_TEXTURE_STAGE0, true);
  180. // Make sure the texgen transform is applied to the texture coordinates and not to an auto-generated reflection vector or whatever.
  181. s_pShaderShadow->TexGen( SHADER_TEXTURE_STAGE0, SHADER_TEXGENPARAM_OBJECT_LINEAR );
  182. // This turns on blending and does overbrighting if it's enabled.
  183. SingleTextureLightmapBlendMode();
  184. // Stage 0, color = D
  185. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE0,
  186. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_SELECTARG1,
  187. SHADER_TEXARG_TEXTURE, SHADER_TEXARG_CONSTANTCOLOR );
  188. // Stage 1, color = D*L
  189. pShaderShadow->CustomTextureOperation( SHADER_TEXTURE_STAGE1,
  190. SHADER_TEXCHANNEL_COLOR, SHADER_TEXOP_MODULATE,
  191. SHADER_TEXARG_PREVIOUSSTAGE, SHADER_TEXARG_TEXTURE );
  192. // Use the lightmap coordinates in both stages.
  193. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 | SHADER_DRAW_LIGHTMAP_TEXCOORD1 );
  194. FogToFogColor();
  195. }
  196. DYNAMIC_STATE
  197. {
  198. BindTexture( SHADER_SAMPLER0, DETAIL);
  199. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  200. if ( detailScale != 1.0f )
  201. {
  202. pShaderAPI->MatrixMode( MATERIAL_TEXTURE1 );
  203. pShaderAPI->LoadIdentity();
  204. pShaderAPI->MatrixMode( MATERIAL_TEXTURE0 );
  205. pShaderAPI->LoadIdentity();
  206. pShaderAPI->ScaleXY( detailScale, detailScale );
  207. }
  208. }
  209. Draw();
  210. }
  211. END_SHADER