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.

507 lines
19 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======//
  2. //
  3. //=============================================================================//
  4. // STATIC: "DETAILTEXTURE" "0..1"
  5. // STATIC: "CUBEMAP" "0..1"
  6. // STATIC: "DIFFUSELIGHTING" "0..1"
  7. // STATIC: "ENVMAPMASK" "0..1"
  8. // STATIC: "BASEALPHAENVMAPMASK" "0..1"
  9. // STATIC: "SELFILLUM" "0..1"
  10. // STATIC: "VERTEXCOLOR" "0..1"
  11. // STATIC: "FLASHLIGHT" "0..1"
  12. // STATIC: "SELFILLUM_ENVMAPMASK_ALPHA" "0..1"
  13. // STATIC: "DETAIL_BLEND_MODE" "0..9"
  14. // STATIC: "SEAMLESS_BASE" "0..1"
  15. // STATIC: "SEAMLESS_DETAIL" "0..1"
  16. // STATIC: "DISTANCEALPHA" "0..1"
  17. // STATIC: "DISTANCEALPHAFROMDETAIL" "0..1"
  18. // STATIC: "SOFT_MASK" "0..1"
  19. // STATIC: "OUTLINE" "0..1"
  20. // STATIC: "OUTER_GLOW" "0..1"
  21. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps20b] [PC]
  22. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..2" [ps30] [PC]
  23. // STATIC: "FLASHLIGHTDEPTHFILTERMODE" "0..0" [ps20b] [XBOX]
  24. // STATIC: "DEPTHBLEND" "0..1" [ps20b] [ps30]
  25. // STATIC: "BLENDTINTBYBASEALPHA" "0..1"
  26. // STATIC: "SRGB_INPUT_ADAPTER" "0..1" [ps20b]
  27. // STATIC: "CUBEMAP_SPHERE_LEGACY" "0..1"
  28. // DYNAMIC: "PIXELFOGTYPE" "0..1" [ps20]
  29. // DYNAMIC: "LIGHTING_PREVIEW" "0..2" [PC]
  30. // DYNAMIC: "LIGHTING_PREVIEW" "0..0" [XBOX]
  31. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps20b]
  32. // DYNAMIC: "FLASHLIGHTSHADOWS" "0..1" [ps30]
  33. // DYNAMIC: "STATIC_LIGHT_LIGHTMAP" "0..1" [ps20b] [ps30]
  34. // DYNAMIC: "STATIC_LIGHT_LIGHTMAP" "0..0" [ps20]
  35. // DYNAMIC: "DEBUG_LUXELS" "0..1" [ps20b] [ps30]
  36. // detail blend mode 6 = ps20b only
  37. // SKIP: $DETAIL_BLEND_MODE == 6 [ps20]
  38. // SKIP: ($DETAILTEXTURE == 0 ) && ( $DETAIL_BLEND_MODE != 0 )
  39. // SKIP: ($DETAILTEXTURE == 0 ) && ( $SEAMLESS_DETAIL )
  40. // SKIP: ($ENVMAPMASK || $SELFILLUM_ENVMAPMASK_ALPHA) && ($SEAMLESS_BASE || $SEAMLESS_DETAIL)
  41. // SKIP: $BASEALPHAENVMAPMASK && $ENVMAPMASK
  42. // SKIP: $BASEALPHAENVMAPMASK && $SELFILLUM
  43. // SKIP: $SELFILLUM && $SELFILLUM_ENVMAPMASK_ALPHA
  44. // SKIP: $SELFILLUM_ENVMAPMASK_ALPHA && (! $ENVMAPMASK)
  45. // SKIP: $ENVMAPMASK && ($FLASHLIGHT || $FLASHLIGHTSHADOWS) [PC]
  46. // SKIP: $BASEALPHAENVMAPMASK && ($SEAMLESS_BASE || $SEAMLESS_DETAIL)
  47. // SKIP: ($DISTANCEALPHA == 0) && ($DISTANCEALPHAFROMDETAIL || $SOFT_MASK || $OUTLINE || $OUTER_GLOW)
  48. // SKIP: ($DETAILTEXTURE == 0) && ($DISTANCEALPHAFROMDETAIL)
  49. // We don't care about flashlight depth unless the flashlight is on
  50. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 ) [ps20b]
  51. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTSHADOWS == 1 ) [ps30]
  52. // Flashlight shadow filter mode is irrelevant if there is no flashlight
  53. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTDEPTHFILTERMODE != 0 ) [ps20b]
  54. // SKIP: ( $FLASHLIGHT == 0 ) && ( $FLASHLIGHTDEPTHFILTERMODE != 0 ) [ps30]
  55. // DISTANCEALPHA-related skips
  56. // SKIP: ($DISTANCEALPHA) && ($ENVMAPMASK || $BASEALPHAENVMAPMASK || $SELFILLUM || $SELFILLUM_ENVMAPMASK_ALPHA )
  57. // SKIP: ($DISTANCEALPHA) && ($SEAMLESS_BASE || $SEAMLESS_DETAIL || $CUBEMAP || $LIGHTING_PREVIEW )
  58. // SKIP: ($DISTANCEALPHA) && ($WRITEWATERFOGTODESTALPHA || $PIXELFOGTYPE || $FLASHLIGHT || $FLASHLIGHTSHADOWS || $SRGB_INPUT_ADAPTER )
  59. // SKIP: $SEAMLESS_BASE && $SRGB_INPUT_ADAPTER
  60. // SKIP: $SEAMLESS_BASE && ($BLENDTINTBYBASEALPHA )
  61. // BlendTintByBaseAlpha is incompatible with other interpretations of alpha
  62. // SKIP: ($BLENDTINTBYBASEALPHA) && ($SELFILLUM || (($DISTANCEALPHA) && ($DISTANCEALPHAFROMDETAIL == 0)) || $BASEALPHAENVMAPMASK)
  63. // Only _XBOX allows flashlight and cubemap in the current implementation
  64. // SKIP: $FLASHLIGHT && $CUBEMAP [PC]
  65. // SKIP: $CUBEMAP_SPHERE_LEGACY && ($CUBEMAP == 0)
  66. // Debugging luxels only makes sense if we have lightmaps on this geometry.
  67. // SKIP: ($STATIC_LIGHT_LIGHTMAP == 0) && ($DEBUG_LUXELS == 1)
  68. #include "common_flashlight_fxc.h"
  69. #include "common_vertexlitgeneric_dx9.h"
  70. const float4 g_EnvmapTint_TintReplaceFactor : register( c0 );
  71. const float4 g_DiffuseModulation : register( c1 );
  72. const float4 g_EnvmapContrast_ShadowTweaks : register( c2 );
  73. const float4 g_EnvmapSaturation_SelfIllumMask : register( c3 );
  74. const float4 g_SelfIllumTint_and_BlendFactor : register( c4 );
  75. const float4 g_ShaderControls : register( c12 );
  76. const float4 g_DepthFeatheringConstants : register( c13 );
  77. const float4 g_EyePos : register( c20 );
  78. const float4 g_FogParams : register( c21 );
  79. #define g_SelfIllumTint g_SelfIllumTint_and_BlendFactor.xyz
  80. #define g_DetailBlendFactor g_SelfIllumTint_and_BlendFactor.w
  81. #define g_EnvmapSaturation g_EnvmapSaturation_SelfIllumMask.xyz
  82. #define g_SelfIllumMaskControl g_EnvmapSaturation_SelfIllumMask.w
  83. const float4 g_FlashlightAttenuationFactors : register( c22 );
  84. const HALF3 g_FlashlightPos : register( c23 );
  85. const float4x4 g_FlashlightWorldToTexture : register( c24 ); // through c27
  86. sampler BaseTextureSampler : register( s0 );
  87. sampler EnvmapSampler : register( s1 );
  88. sampler DetailSampler : register( s2 );
  89. sampler EnvmapMaskSampler : register( s4 );
  90. sampler RandRotSampler : register( s6 ); // RandomRotation sampler
  91. sampler FlashlightSampler : register( s7 );
  92. sampler ShadowDepthSampler : register( s8 ); // Flashlight shadow depth map sampler
  93. sampler DepthSampler : register( s10 ); //depth buffer sampler for depth blending
  94. sampler SelfIllumMaskSampler : register( s11 ); // selfillummask
  95. sampler LightMapSampler : register( s12 );
  96. struct PS_INPUT
  97. {
  98. #if SEAMLESS_BASE
  99. HALF3 baseTexCoord : TEXCOORD0; // Base texture coordinate
  100. #else
  101. HALF2 baseTexCoord : TEXCOORD0; // Base texture coordinate
  102. #endif
  103. #if SEAMLESS_DETAIL
  104. HALF3 detailTexCoord : TEXCOORD1; // Seamless texture coordinate
  105. #else
  106. HALF2 detailTexCoord : TEXCOORD1; // Detail texture coordinate
  107. #endif
  108. float4 color : TEXCOORD2; // Vertex color (from lighting or unlit)
  109. float3 worldVertToEyeVector : TEXCOORD3; // Necessary for reflection
  110. float3 worldSpaceNormal : TEXCOORD4; // Necessary for cubemaps and flashlight
  111. #if defined ( _X360 )
  112. #if FLASHLIGHT
  113. float4 flashlightSpacePos : TEXCOORD5;
  114. #endif
  115. #endif
  116. float4 projPos : TEXCOORD6;
  117. float4 worldPos_projPosZ : TEXCOORD7;
  118. float4 fogFactorW : COLOR1;
  119. #if SEAMLESS_BASE || SEAMLESS_DETAIL
  120. float3 SeamlessWeights : COLOR0; // x y z projection weights
  121. #endif
  122. };
  123. const float4 g_GlowParameters : register( c5 );
  124. const float4 g_GlowColor : register( c6 );
  125. #define GLOW_UV_OFFSET g_GlowParameters.xy
  126. #define OUTER_GLOW_MIN_DVALUE g_GlowParameters.z
  127. #define OUTER_GLOW_MAX_DVALUE g_GlowParameters.w
  128. #define OUTER_GLOW_COLOR g_GlowColor
  129. #define g_fPixelFogType g_ShaderControls.x
  130. #define g_fWriteDepthToAlpha g_ShaderControls.y
  131. #define g_fWriteWaterFogToDestAlpha g_ShaderControls.z
  132. #define g_fVertexAlpha g_ShaderControls.w
  133. const float4 g_DistanceAlphaParams : register( c7 );
  134. #define SOFT_MASK_MAX g_DistanceAlphaParams.x
  135. #define SOFT_MASK_MIN g_DistanceAlphaParams.y
  136. const float4 g_OutlineColor : register( c8 );
  137. #define OUTLINE_COLOR g_OutlineColor
  138. const float4 g_OutlineParams : register( c9 );
  139. // these are ordered this way for optimal ps20 swizzling
  140. #define OUTLINE_MIN_VALUE0 g_OutlineParams.x
  141. #define OUTLINE_MAX_VALUE1 g_OutlineParams.y
  142. #define OUTLINE_MAX_VALUE0 g_OutlineParams.z
  143. #define OUTLINE_MIN_VALUE1 g_OutlineParams.w
  144. #if DETAILTEXTURE
  145. const float3 g_DetailTint : register( c10 );
  146. #endif
  147. #if DEBUG_LUXELS
  148. const float4 g_LuxelScale : register( c11 );
  149. #endif
  150. // Calculate unified fog
  151. float CalcPixelFogFactorConst( float fPixelFogType, const float4 fogParams, const float flEyePosZ, const float flWorldPosZ, const float flProjPosZ )
  152. {
  153. float flDepthBelowWater = fPixelFogType*fogParams.y - flWorldPosZ; // above water = negative, below water = positive
  154. float flDepthBelowEye = fPixelFogType*flEyePosZ - flWorldPosZ; // above eye = negative, below eye = positive
  155. // if fPixelFogType == 0, then flDepthBelowWater == flDepthBelowEye and frac will be 1
  156. float frac = (flDepthBelowEye == 0) ? 1 : saturate(flDepthBelowWater/flDepthBelowEye);
  157. return saturate( min(fogParams.z, flProjPosZ * fogParams.w * frac - fogParams.x) );
  158. }
  159. // Blend both types of Fog and lerp to get result
  160. float3 BlendPixelFogConst( const float3 vShaderColor, float pixelFogFactor, const float3 vFogColor, float fPixelFogType )
  161. {
  162. //float3 fRangeResult = lerp( vShaderColor.rgb, vFogColor.rgb, pixelFogFactor * pixelFogFactor ); //squaring the factor will get the middle range mixing closer to hardware fog
  163. //float3 fHeightResult = lerp( vShaderColor.rgb, vFogColor.rgb, saturate( pixelFogFactor ) );
  164. //return lerp( fRangeResult, fHeightResult, fPixelFogType );
  165. pixelFogFactor = lerp( pixelFogFactor*pixelFogFactor, pixelFogFactor, fPixelFogType );
  166. return lerp( vShaderColor.rgb, vFogColor.rgb, pixelFogFactor );
  167. }
  168. float4 FinalOutputConst( const float4 vShaderColor, float pixelFogFactor, float fPixelFogType, const int iTONEMAP_SCALE_TYPE, float fWriteDepthToDestAlpha, const float flProjZ )
  169. {
  170. float4 result = vShaderColor;
  171. if( iTONEMAP_SCALE_TYPE == TONEMAP_SCALE_LINEAR )
  172. {
  173. result.rgb *= LINEAR_LIGHT_SCALE;
  174. }
  175. else if( iTONEMAP_SCALE_TYPE == TONEMAP_SCALE_GAMMA )
  176. {
  177. result.rgb *= GAMMA_LIGHT_SCALE;
  178. }
  179. result.a = lerp( result.a, DepthToDestAlpha( flProjZ ), fWriteDepthToDestAlpha );
  180. result.rgb = BlendPixelFogConst( result.rgb, pixelFogFactor, g_LinearFogColor.rgb, fPixelFogType );
  181. result.rgb = SRGBOutput( result.rgb ); //SRGB in pixel shader conversion
  182. return result;
  183. }
  184. #if LIGHTING_PREVIEW == 2
  185. LPREVIEW_PS_OUT main( PS_INPUT i ) : COLOR
  186. #else
  187. float4 main( PS_INPUT i ) : COLOR
  188. #endif
  189. {
  190. bool bDetailTexture = DETAILTEXTURE ? true : false;
  191. bool bCubemap = CUBEMAP ? true : false;
  192. bool bDiffuseLighting = DIFFUSELIGHTING ? true : false;
  193. bool bHasNormal = bCubemap || bDiffuseLighting;
  194. bool bEnvmapMask = ENVMAPMASK ? true : false;
  195. bool bBaseAlphaEnvmapMask = BASEALPHAENVMAPMASK ? true : false;
  196. bool bSelfIllum = SELFILLUM ? true : false;
  197. bool bVertexColor = VERTEXCOLOR ? true : false;
  198. bool bFlashlight = FLASHLIGHT ? true : false;
  199. bool bBlendTintByBaseAlpha = BLENDTINTBYBASEALPHA ? true : false;
  200. HALF4 baseColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
  201. #if SEAMLESS_BASE
  202. baseColor =
  203. i.SeamlessWeights.x * tex2D( BaseTextureSampler, i.baseTexCoord.yz )+
  204. i.SeamlessWeights.y * tex2D( BaseTextureSampler, i.baseTexCoord.zx )+
  205. i.SeamlessWeights.z * tex2D( BaseTextureSampler, i.baseTexCoord.xy );
  206. #else
  207. baseColor = tex2D( BaseTextureSampler, i.baseTexCoord.xy );
  208. #if SRGB_INPUT_ADAPTER
  209. baseColor.rgb = GammaToLinear( baseColor.rgb );
  210. #endif
  211. #endif // !SEAMLESS_BASE
  212. #if DISTANCEALPHA && (DISTANCEALPHAFROMDETAIL == 0)
  213. float distAlphaMask = baseColor.a;
  214. #endif
  215. #if DETAILTEXTURE
  216. #if SEAMLESS_DETAIL
  217. float4 detailColor =
  218. i.SeamlessWeights.x * tex2D( DetailSampler, i.detailTexCoord.yz )+
  219. i.SeamlessWeights.y * tex2D( DetailSampler, i.detailTexCoord.zx )+
  220. i.SeamlessWeights.z * tex2D( DetailSampler, i.detailTexCoord.xy );
  221. #else
  222. float4 detailColor = tex2D( DetailSampler, i.detailTexCoord.xy );
  223. #endif
  224. detailColor.rgb *= g_DetailTint;
  225. #if DISTANCEALPHA && (DISTANCEALPHAFROMDETAIL == 1)
  226. float distAlphaMask = detailColor.a;
  227. detailColor.a = 1.0; // make tcombine treat as 1.0
  228. #endif
  229. baseColor =
  230. TextureCombine( baseColor, detailColor, DETAIL_BLEND_MODE, g_DetailBlendFactor );
  231. #endif
  232. #if DISTANCEALPHA
  233. // now, do all distance alpha effects
  234. //if ( OUTLINE && ( distAlphaMask >= OUTLINE_MIN_VALUE0 ) && ( distAlphaMask <= OUTLINE_MAX_VALUE1 ) )
  235. //{
  236. // float oFactor=1.0;
  237. // if ( distAlphaMask <= OUTLINE_MIN_VALUE1 )
  238. // {
  239. // oFactor=smoothstep( OUTLINE_MIN_VALUE0, OUTLINE_MIN_VALUE1, distAlphaMask );
  240. // }
  241. // else
  242. // {
  243. // oFactor=smoothstep( OUTLINE_MAX_VALUE1, OUTLINE_MAX_VALUE0, distAlphaMask );
  244. // }
  245. // baseColor = lerp( baseColor, OUTLINE_COLOR, oFactor );
  246. //}
  247. if ( OUTLINE )
  248. {
  249. float4 oFactors = smoothstep(g_OutlineParams.xyzw, g_OutlineParams.wzyx, distAlphaMask );
  250. baseColor = lerp( baseColor, g_OutlineColor, oFactors.x * oFactors.y );
  251. }
  252. float mskUsed;
  253. if ( SOFT_MASK )
  254. {
  255. mskUsed = smoothstep( SOFT_MASK_MIN, SOFT_MASK_MAX, distAlphaMask );
  256. baseColor.a *= mskUsed;
  257. }
  258. else
  259. {
  260. mskUsed = distAlphaMask >= 0.5;
  261. if (DETAILTEXTURE )
  262. baseColor.a *= mskUsed;
  263. else
  264. baseColor.a = mskUsed;
  265. }
  266. if ( OUTER_GLOW )
  267. {
  268. #if DISTANCEALPHAFROMDETAIL
  269. float4 glowTexel = tex2D( DetailSampler, i.detailTexCoord.xy+GLOW_UV_OFFSET );
  270. #else
  271. float4 glowTexel = tex2D( BaseTextureSampler, i.baseTexCoord.xy+GLOW_UV_OFFSET );
  272. #endif
  273. float4 glowc = OUTER_GLOW_COLOR*smoothstep( OUTER_GLOW_MIN_DVALUE, OUTER_GLOW_MAX_DVALUE, glowTexel.a );
  274. baseColor = lerp( glowc, baseColor, mskUsed );
  275. }
  276. #endif // DISTANCEALPHA
  277. float3 specularFactor = 1.0f;
  278. float4 envmapMaskTexel;
  279. if( bEnvmapMask )
  280. {
  281. envmapMaskTexel = tex2D( EnvmapMaskSampler, i.baseTexCoord.xy );
  282. specularFactor *= envmapMaskTexel.xyz;
  283. }
  284. if( bBaseAlphaEnvmapMask )
  285. {
  286. specularFactor *= 1.0 - baseColor.a; // this blows!
  287. }
  288. float3 diffuseLighting = float3( 1.0f, 1.0f, 1.0f );
  289. if( bDiffuseLighting || bVertexColor )
  290. {
  291. diffuseLighting = i.color.rgb;
  292. }
  293. #if STATIC_LIGHT_LIGHTMAP
  294. // This matches the behavior of vertex lighting, which multiplies by cOverbright (which is not accessible here)
  295. // And converts from Gamma space to Linear space before being used.
  296. float2 lightmapTexCoords = i.baseTexCoord.xy;
  297. #if DEBUG_LUXELS
  298. lightmapTexCoords.xy *= g_LuxelScale.xy;
  299. #endif
  300. float3 f3LightmapColor = GammaToLinear( 2.0f * tex2D( LightMapSampler, lightmapTexCoords ).rgb );
  301. diffuseLighting = f3LightmapColor;
  302. #endif
  303. float3 albedo = baseColor;
  304. if (bBlendTintByBaseAlpha)
  305. {
  306. float3 tintedColor = albedo * g_DiffuseModulation.rgb;
  307. tintedColor = lerp(tintedColor, g_DiffuseModulation.rgb, g_EnvmapTint_TintReplaceFactor.w);
  308. albedo = lerp(albedo, tintedColor, baseColor.a);
  309. }
  310. else
  311. {
  312. albedo = albedo * g_DiffuseModulation.rgb;
  313. }
  314. float alpha = g_DiffuseModulation.a;
  315. if ( !bBaseAlphaEnvmapMask && !bSelfIllum && !bBlendTintByBaseAlpha )
  316. {
  317. alpha *= baseColor.a;
  318. }
  319. if( bFlashlight )
  320. {
  321. int nShadowSampleLevel = 0;
  322. bool bDoShadows = false;
  323. // On ps_2_b, we can do shadow mapping
  324. #if ( FLASHLIGHTSHADOWS && (defined(SHADER_MODEL_PS_2_B) || defined(SHADER_MODEL_PS_3_0) ) )
  325. nShadowSampleLevel = FLASHLIGHTDEPTHFILTERMODE;
  326. bDoShadows = true;
  327. #endif
  328. #if defined ( _X360 )
  329. float4 flashlightSpacePosition = i.flashlightSpacePos;
  330. #else
  331. float4 flashlightSpacePosition = mul( float4( i.worldPos_projPosZ.xyz, 1.0f ), g_FlashlightWorldToTexture );
  332. #endif
  333. // We want the N.L to happen on the flashlight pass, but can't afford it on ps20
  334. bool bUseWorldNormal = true;
  335. #if ( defined( SHADER_MODEL_PS_2_0 ) && ( DETAILTEXTURE ) )
  336. bUseWorldNormal = false;
  337. #endif
  338. float3 flashlightColor = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, flashlightSpacePosition,
  339. i.worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  340. g_FlashlightAttenuationFactors.w, FlashlightSampler, ShadowDepthSampler,
  341. RandRotSampler, nShadowSampleLevel, bDoShadows, false, i.projPos.xy / i.projPos.w, false, g_EnvmapContrast_ShadowTweaks, bUseWorldNormal );
  342. #if defined ( _X360 )
  343. diffuseLighting += flashlightColor;
  344. #else
  345. diffuseLighting = flashlightColor;
  346. #endif
  347. }
  348. if( bVertexColor && bDiffuseLighting )
  349. {
  350. albedo *= i.color.rgb;
  351. }
  352. alpha = lerp( alpha, alpha * i.color.a, g_fVertexAlpha );
  353. float3 diffuseComponent = albedo * diffuseLighting;
  354. #if DETAILTEXTURE
  355. diffuseComponent =
  356. TextureCombinePostLighting( diffuseComponent, detailColor, DETAIL_BLEND_MODE, g_DetailBlendFactor );
  357. #endif
  358. HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
  359. #if !FLASHLIGHT || defined ( _X360 )
  360. #if SELFILLUM_ENVMAPMASK_ALPHA
  361. // range of alpha:
  362. // 0 - 0.125 = lerp(diffuse,selfillum,alpha*8)
  363. // 0.125-1.0 = selfillum*(1+alpha-0.125)*8 (over bright glows)
  364. HALF3 selfIllumComponent = g_SelfIllumTint * albedo;
  365. half Adj_Alpha=8*envmapMaskTexel.a;
  366. diffuseComponent=( max( 0, 1-Adj_Alpha ) * diffuseComponent) + Adj_Alpha * selfIllumComponent;
  367. #else
  368. if ( bSelfIllum )
  369. {
  370. float3 vSelfIllumMask = tex2D( SelfIllumMaskSampler, i.baseTexCoord.xy );
  371. vSelfIllumMask = lerp( baseColor.aaa, vSelfIllumMask, g_SelfIllumMaskControl );
  372. diffuseComponent = lerp( diffuseComponent, g_SelfIllumTint * albedo, vSelfIllumMask );
  373. }
  374. #endif
  375. if( bCubemap )
  376. {
  377. #if CUBEMAP_SPHERE_LEGACY
  378. HALF3 reflectVect = normalize(CalcReflectionVectorUnnormalized( i.worldSpaceNormal, i.worldVertToEyeVector.xyz ));
  379. specularLighting = 0.5 * tex2D( EnvmapSampler, float2(reflectVect.x, reflectVect.y) ) * g_DiffuseModulation.rgb * diffuseLighting;
  380. #else
  381. HALF3 reflectVect = CalcReflectionVectorUnnormalized( i.worldSpaceNormal, i.worldVertToEyeVector.xyz );
  382. specularLighting = ENV_MAP_SCALE * texCUBE( EnvmapSampler, reflectVect );
  383. specularLighting *= specularFactor;
  384. specularLighting *= g_EnvmapTint_TintReplaceFactor.rgb;
  385. HALF3 specularLightingSquared = specularLighting * specularLighting;
  386. specularLighting = lerp( specularLighting, specularLightingSquared, g_EnvmapContrast_ShadowTweaks );
  387. HALF3 greyScale = dot( specularLighting, HALF3( 0.299f, 0.587f, 0.114f ) );
  388. specularLighting = lerp( greyScale, specularLighting, g_EnvmapSaturation );
  389. #endif
  390. }
  391. #endif
  392. HALF3 result = diffuseComponent + specularLighting;
  393. #if LIGHTING_PREVIEW
  394. # if LIGHTING_PREVIEW == 1
  395. float dotprod=0.7+0.25*dot(i.worldSpaceNormal,normalize(float3(1,2,-.5)));
  396. return FinalOutput( float4( dotprod*albedo.xyz, alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_LINEAR );
  397. # else
  398. LPREVIEW_PS_OUT ret;
  399. ret.flags=float4(1,1,1,1);
  400. ret.color=float4( albedo.xyz, alpha );
  401. ret.normal=float4(i.worldSpaceNormal,alpha);
  402. ret.position=float4(i.worldPos_projPosZ.xyz, alpha);
  403. return FinalOutput( ret, 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  404. # endif
  405. #else
  406. # if (DEPTHBLEND == 1)
  407. {
  408. float2 vScreenPos;
  409. vScreenPos.x = i.projPos.x;
  410. vScreenPos.y = -i.projPos.y;
  411. vScreenPos = (vScreenPos + i.projPos.w) * 0.5f;
  412. alpha *= DepthFeathering( DepthSampler, vScreenPos / i.projPos.w, i.projPos.w - i.projPos.z, i.projPos.w, g_DepthFeatheringConstants );
  413. }
  414. # endif
  415. #if defined( SHADER_MODEL_PS_2_0 )
  416. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.projPos.z );
  417. #if (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  418. alpha = lerp( alpha, fogFactor, g_fWriteWaterFogToDestAlpha );
  419. #endif
  420. return FinalOutput( float4( result.rgb, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, false, i.projPos.z );
  421. #else // 2b or higher
  422. float fogFactor = CalcPixelFogFactorConst( g_fPixelFogType, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.projPos.z );
  423. alpha = lerp( alpha, fogFactor, g_fWriteWaterFogToDestAlpha ); // Use the fog factor if it's height fog
  424. return FinalOutputConst( float4( result.rgb, alpha ), fogFactor, g_fPixelFogType, TONEMAP_SCALE_LINEAR, g_fWriteDepthToAlpha, i.projPos.z );
  425. #endif
  426. #endif
  427. }