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.

288 lines
8.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Lightmap only shader
  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( LightmappedGeneric, LightmappedGeneric_DX6 )
  12. BEGIN_SHADER( LightmappedGeneric_DX6,
  13. "Help for LightmappedGeneric_DX6" )
  14. BEGIN_SHADER_PARAMS
  15. SHADER_PARAM( SELFILLUMTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "Self-illumination tint" )
  16. SHADER_PARAM( DETAIL, SHADER_PARAM_TYPE_TEXTURE, "shadertest/detail", "detail texture" )
  17. SHADER_PARAM( DETAILSCALE, SHADER_PARAM_TYPE_FLOAT, "4", "scale of the detail texture" )
  18. SHADER_PARAM( ENVMAP, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_env", "envmap" )
  19. SHADER_PARAM( ENVMAPFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  20. SHADER_PARAM( ENVMAPMASK, SHADER_PARAM_TYPE_TEXTURE, "shadertest/shadertest_envmask", "envmap mask" )
  21. SHADER_PARAM( ENVMAPMASKFRAME, SHADER_PARAM_TYPE_INTEGER, "", "" )
  22. SHADER_PARAM( ENVMAPMASKSCALE, SHADER_PARAM_TYPE_FLOAT, "1", "envmap mask scale" )
  23. SHADER_PARAM( ENVMAPTINT, SHADER_PARAM_TYPE_COLOR, "[1 1 1]", "envmap tint" )
  24. SHADER_PARAM( ENVMAPOPTIONAL, SHADER_PARAM_TYPE_BOOL, "90", "Do specular pass only on dxlevel or higher (ie.80, 81, 90)" )
  25. END_SHADER_PARAMS
  26. SHADER_INIT_PARAMS()
  27. {
  28. // FLASHLIGHTFIXME
  29. params[FLASHLIGHTTEXTURE]->SetStringValue( "effects/flashlight001" );
  30. if( params[BASETEXTURE]->IsDefined() )
  31. {
  32. if( !IS_FLAG_SET(MATERIAL_VAR_MULTIPASS) )
  33. {
  34. params[ENVMAP]->SetUndefined();
  35. params[ENVMAPMASK]->SetUndefined();
  36. }
  37. }
  38. if( !params[ENVMAPMASKSCALE]->IsDefined() )
  39. params[ENVMAPMASKSCALE]->SetFloatValue( 1.0f );
  40. if( !params[ENVMAPTINT]->IsDefined() )
  41. params[ENVMAPTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  42. if( !params[SELFILLUMTINT]->IsDefined() )
  43. params[SELFILLUMTINT]->SetVecValue( 1.0f, 1.0f, 1.0f );
  44. if( !params[DETAILSCALE]->IsDefined() )
  45. params[DETAILSCALE]->SetFloatValue( 4.0f );
  46. // No texture means no self-illum or env mask in base alpha
  47. if ( !params[BASETEXTURE]->IsDefined() )
  48. {
  49. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  50. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  51. }
  52. // If in decal mode, no debug override...
  53. if (IS_FLAG_SET(MATERIAL_VAR_DECAL))
  54. {
  55. SET_FLAGS( MATERIAL_VAR_NO_DEBUG_OVERRIDE );
  56. }
  57. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_LIGHTMAP );
  58. // Get rid of the envmap if it's optional for this dx level.
  59. if( params[ENVMAPOPTIONAL]->IsDefined() && params[ENVMAPOPTIONAL]->GetIntValue() )
  60. {
  61. params[ENVMAP]->SetUndefined();
  62. }
  63. // If mat_specular 0, then get rid of envmap
  64. if( !g_pConfig->UseSpecular() && params[ENVMAP]->IsDefined() && params[BASETEXTURE]->IsDefined() )
  65. {
  66. params[ENVMAP]->SetUndefined();
  67. }
  68. SET_FLAGS2( MATERIAL_VAR2_NEEDS_FIXED_FUNCTION_FLASHLIGHT );
  69. }
  70. SHADER_INIT
  71. {
  72. LoadTexture( FLASHLIGHTTEXTURE );
  73. if (params[BASETEXTURE]->IsDefined())
  74. {
  75. LoadTexture( BASETEXTURE );
  76. if (!params[BASETEXTURE]->GetTextureValue()->IsTranslucent())
  77. {
  78. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM))
  79. CLEAR_FLAGS( MATERIAL_VAR_SELFILLUM );
  80. if (IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK))
  81. CLEAR_FLAGS( MATERIAL_VAR_BASEALPHAENVMAPMASK );
  82. }
  83. }
  84. if (params[DETAIL]->IsDefined())
  85. {
  86. LoadTexture( DETAIL );
  87. }
  88. // Don't alpha test if the alpha channel is used for other purposes
  89. if (IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) || IS_FLAG_SET(MATERIAL_VAR_BASEALPHAENVMAPMASK) )
  90. CLEAR_FLAGS( MATERIAL_VAR_ALPHATEST );
  91. if (params[ENVMAP]->IsDefined())
  92. {
  93. if( !IS_FLAG_SET(MATERIAL_VAR_ENVMAPSPHERE) )
  94. {
  95. LoadCubeMap( ENVMAP );
  96. }
  97. else
  98. {
  99. LoadTexture( ENVMAP );
  100. }
  101. if( !g_pHardwareConfig->SupportsCubeMaps() )
  102. {
  103. SET_FLAGS( MATERIAL_VAR_ENVMAPSPHERE );
  104. }
  105. if (params[ENVMAPMASK]->IsDefined())
  106. {
  107. LoadTexture( ENVMAPMASK );
  108. }
  109. }
  110. }
  111. SHADER_FALLBACK
  112. {
  113. return 0;
  114. }
  115. int GetDrawFlagsPass1(IMaterialVar** params)
  116. {
  117. int flags = SHADER_DRAW_POSITION | SHADER_DRAW_LIGHTMAP_TEXCOORD0;
  118. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  119. flags |= SHADER_DRAW_COLOR;
  120. if (params[BASETEXTURE]->IsTexture())
  121. flags |= SHADER_DRAW_TEXCOORD1;
  122. return flags;
  123. }
  124. void DrawLightmapOnly( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  125. {
  126. SHADOW_STATE
  127. {
  128. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  129. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE0, OVERBRIGHT );
  130. SetModulationShadowState();
  131. SetDefaultBlendingShadowState( );
  132. pShaderShadow->DrawFlags( GetDrawFlagsPass1( params ) );
  133. DefaultFog();
  134. }
  135. DYNAMIC_STATE
  136. {
  137. pShaderAPI->BindStandardTexture( SHADER_SAMPLER0, TEXTURE_LIGHTMAP );
  138. SetModulationDynamicState();
  139. }
  140. Draw();
  141. }
  142. void DrawBaseTimesLightmap( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  143. {
  144. // Base times lightmap
  145. SHADOW_STATE
  146. {
  147. // alpha test
  148. pShaderShadow->EnableAlphaTest( IS_FLAG_SET(MATERIAL_VAR_ALPHATEST) );
  149. // Alpha blending
  150. SetDefaultBlendingShadowState( BASETEXTURE, true );
  151. // Independenly configure alpha and color
  152. pShaderShadow->EnableAlphaPipe( true );
  153. // color channel
  154. // base
  155. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  156. // lightmap
  157. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  158. pShaderShadow->OverbrightValue( SHADER_TEXTURE_STAGE1, OVERBRIGHT );
  159. pShaderShadow->EnableConstantColor( IsColorModulating() );
  160. // alpha channel
  161. pShaderShadow->EnableConstantAlpha( IsAlphaModulating() );
  162. if ((! IsColorModulating()) && ( ! IsAlphaModulating()))
  163. pShaderShadow->EnableVertexAlpha( IS_FLAG_SET(MATERIAL_VAR_VERTEXALPHA) );
  164. pShaderShadow->EnableTextureAlpha( SHADER_TEXTURE_STAGE0, TextureIsTranslucent(BASETEXTURE, true) );
  165. pShaderShadow->EnableTextureAlpha( SHADER_TEXTURE_STAGE1, false );
  166. int flags = SHADER_DRAW_POSITION | SHADER_DRAW_LIGHTMAP_TEXCOORD1;
  167. if (IS_FLAG_SET(MATERIAL_VAR_VERTEXCOLOR))
  168. {
  169. if ( (! IsColorModulating()) && ( ! IsAlphaModulating()))
  170. flags |= SHADER_DRAW_COLOR;
  171. }
  172. if (params[BASETEXTURE]->IsTexture())
  173. {
  174. flags |= SHADER_DRAW_TEXCOORD0;
  175. }
  176. pShaderShadow->DrawFlags( flags );
  177. DefaultFog();
  178. }
  179. DYNAMIC_STATE
  180. {
  181. pShaderAPI->BindStandardTexture( SHADER_SAMPLER1, TEXTURE_LIGHTMAP );
  182. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  183. SetFixedFunctionTextureTransform( MATERIAL_TEXTURE0, BASETEXTURETRANSFORM );
  184. SetModulationDynamicState();
  185. }
  186. Draw();
  187. SHADOW_STATE
  188. {
  189. pShaderShadow->EnableAlphaPipe( false );
  190. }
  191. }
  192. void DrawMode1( IMaterialVar** params, IShaderDynamicAPI *pShaderAPI, IShaderShadow* pShaderShadow )
  193. {
  194. // Pass 1 : Base * lightmap or just lightmap
  195. if ( params[BASETEXTURE]->IsTexture() )
  196. {
  197. DrawBaseTimesLightmap( params, pShaderAPI, pShaderShadow );
  198. FixedFunctionMultiplyByDetailPass(
  199. BASETEXTURE, FRAME, BASETEXTURETRANSFORM, DETAIL, DETAILSCALE );
  200. // Draw the selfillum pass
  201. if ( IS_FLAG_SET(MATERIAL_VAR_SELFILLUM) )
  202. {
  203. FixedFunctionSelfIlluminationPass(
  204. SHADER_SAMPLER0, BASETEXTURE, FRAME, BASETEXTURETRANSFORM, SELFILLUMTINT );
  205. }
  206. }
  207. else
  208. {
  209. DrawLightmapOnly( params, pShaderAPI, pShaderShadow );
  210. FixedFunctionMultiplyByDetailPass(
  211. BASETEXTURE, FRAME, BASETEXTURETRANSFORM, DETAIL, DETAILSCALE );
  212. }
  213. // Pass 2 : Masked environment map
  214. if (params[ENVMAP]->IsTexture())
  215. {
  216. FixedFunctionAdditiveMaskedEnvmapPass(
  217. ENVMAP, ENVMAPMASK, BASETEXTURE,
  218. ENVMAPFRAME, ENVMAPMASKFRAME, FRAME,
  219. BASETEXTURETRANSFORM, ENVMAPMASKSCALE, ENVMAPTINT );
  220. }
  221. }
  222. SHADER_DRAW
  223. {
  224. bool hasFlashlight = UsingFlashlight( params );
  225. if( hasFlashlight )
  226. {
  227. DrawFlashlight_dx70( params, pShaderAPI, pShaderShadow, FLASHLIGHTTEXTURE, FLASHLIGHTTEXTUREFRAME );
  228. }
  229. else
  230. {
  231. // Base * Lightmap + env
  232. DrawMode1( params, pShaderAPI, pShaderShadow );
  233. }
  234. }
  235. END_SHADER
  236. //-----------------------------------------------------------------------------
  237. // This allows us to use a block labelled 'Water_DX60' in the water materials
  238. //-----------------------------------------------------------------------------
  239. BEGIN_INHERITED_SHADER( Water_DX60, LightmappedGeneric_DX6, "Help for Water_DX60" )
  240. END_INHERITED_SHADER