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.

583 lines
18 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // SKIP: $BUMPMAP2 && $WARPLIGHTING
  3. // SKIP: $WARPLIGHTING && $DETAILTEXTURE
  4. // SKIP: $ENVMAPMASK && $BUMPMAP
  5. // SKIP: $NORMALMAPALPHAENVMAPMASK && $BASEALPHAENVMAPMASK
  6. // SKIP: $NORMALMAPALPHAENVMAPMASK && $ENVMAPMASK
  7. // SKIP: $BASEALPHAENVMAPMASK && $ENVMAPMASK
  8. // SKIP: $BASEALPHAENVMAPMASK && $SELFILLUM
  9. // SKIP: !$FASTPATH && $FASTPATHENVMAPCONTRAST
  10. // SKIP: !$FASTPATH && $FASTPATHENVMAPTINT
  11. // SKIP: !$BUMPMAP && $DIFFUSEBUMPMAP
  12. // SKIP: !$BUMPMAP && $BUMPMAP2
  13. // SKIP: $ENVMAPMASK && $BUMPMAP2
  14. // SKIP: $BASETEXTURENOENVMAP && ( !$BASETEXTURE2 || !$CUBEMAP )
  15. // SKIP: $BASETEXTURE2NOENVMAP && ( !$BASETEXTURE2 || !$CUBEMAP )
  16. // SKIP: $BASEALPHAENVMAPMASK && $BUMPMAP
  17. // SKIP: $PARALLAXMAP && $DETAILTEXTURE
  18. // SKIP: $SEAMLESS && $RELIEF_MAPPING
  19. // SKIP: $SEAMLESS && $DETAILTEXTURE
  20. // SKIP: $SEAMLESS && $MASKEDBLENDING
  21. // SKIP: $BUMPMASK && ( $SEAMLESS || $DETAILTEXTURE || $SELFILLUM || $BASETEXTURENOENVMAP || $BASETEXTURE2 )
  22. // SKIP: !$BUMPMAP && ($NORMAL_DECODE_MODE == 1)
  23. // SKIP: !$BUMPMAP && ($NORMAL_DECODE_MODE == 2)
  24. // SKIP: !$BUMPMAP && ($NORMALMASK_DECODE_MODE == 1)
  25. // SKIP: !$BUMPMAP && ($NORMALMASK_DECODE_MODE == 2)
  26. // NOSKIP: $FANCY_BLENDING && (!$FASTPATH)
  27. // 360 compiler craps out on some combo in this family. Content doesn't use blendmode 10 anyway
  28. // SKIP: $FASTPATH && $PIXELFOGTYPE && $BASETEXTURE2 && $DETAILTEXTURE && $CUBEMAP && ($DETAIL_BLEND_MODE == 10 ) [XBOX]
  29. // debug crap:
  30. // NOSKIP: $DETAILTEXTURE
  31. // NOSKIP: $CUBEMAP
  32. // NOSKIP: $ENVMAPMASK
  33. // NOSKIP: $BASEALPHAENVMAPMASK
  34. // NOSKIP: $SELFILLUM
  35. #define USE_32BIT_LIGHTMAPS_ON_360 //uncomment to use 32bit lightmaps, be sure to keep this in sync with the same #define in materialsystem/cmatlightmaps.cpp
  36. #include "common_ps_fxc.h"
  37. #include "common_flashlight_fxc.h"
  38. #include "common_lightmappedgeneric_fxc.h"
  39. #if SEAMLESS
  40. #define USE_FAST_PATH 1
  41. #else
  42. #define USE_FAST_PATH FASTPATH
  43. #endif
  44. const HALF4 g_EnvmapTint : register( c0 );
  45. #if USE_FAST_PATH == 1
  46. # if FASTPATHENVMAPCONTRAST == 0
  47. static const HALF3 g_EnvmapContrast = { 0.0f, 0.0f, 0.0f };
  48. # else
  49. static const HALF3 g_EnvmapContrast = { 1.0f, 1.0f, 1.0f };
  50. # endif
  51. static const HALF3 g_EnvmapSaturation = { 1.0f, 1.0f, 1.0f };
  52. static const HALF g_FresnelReflection = 1.0f;
  53. static const HALF g_OneMinusFresnelReflection = 0.0f;
  54. static const HALF4 g_SelfIllumTint = { 1.0f, 1.0f, 1.0f, 1.0f };
  55. # if OUTLINE
  56. const float4 g_OutlineParams : register( c2 );
  57. #define OUTLINE_MIN_VALUE0 g_OutlineParams.x
  58. #define OUTLINE_MIN_VALUE1 g_OutlineParams.y
  59. #define OUTLINE_MAX_VALUE0 g_OutlineParams.z
  60. #define OUTLINE_MAX_VALUE1 g_OutlineParams.w
  61. const float4 g_OutlineColor : register( c3 );
  62. #define OUTLINE_COLOR g_OutlineColor
  63. # endif
  64. # if SOFTEDGES
  65. const float4 g_EdgeSoftnessParms : register( c4 );
  66. #define SOFT_MASK_MIN g_EdgeSoftnessParms.x
  67. #define SOFT_MASK_MAX g_EdgeSoftnessParms.y
  68. # endif
  69. #else
  70. const HALF3 g_EnvmapContrast : register( c2 );
  71. const HALF3 g_EnvmapSaturation : register( c3 );
  72. const HALF4 g_FresnelReflectionReg : register( c4 );
  73. #define g_FresnelReflection g_FresnelReflectionReg.a
  74. #define g_OneMinusFresnelReflection g_FresnelReflectionReg.b
  75. const HALF4 g_SelfIllumTint : register( c7 );
  76. #endif
  77. const float4 g_DetailTint_and_BlendFactor : register( c8 );
  78. #define g_DetailTint (g_DetailTint_and_BlendFactor.rgb)
  79. #define g_DetailBlendFactor (g_DetailTint_and_BlendFactor.w)
  80. const HALF3 g_EyePos : register( c10 );
  81. const HALF4 g_FogParams : register( c11 );
  82. const float4 g_TintValuesAndLightmapScale : register( c12 );
  83. #define g_flAlpha2 g_TintValuesAndLightmapScale.w
  84. const float4 g_FlashlightAttenuationFactors : register( c13 );
  85. const float3 g_FlashlightPos : register( c14 );
  86. const float4x4 g_FlashlightWorldToTexture : register( c15 ); // through c18
  87. const float4 g_ShadowTweaks : register( c19 );
  88. sampler BaseTextureSampler : register( s0 );
  89. sampler LightmapSampler : register( s1 );
  90. sampler EnvmapSampler : register( s2 );
  91. #if FANCY_BLENDING
  92. sampler BlendModulationSampler : register( s3 );
  93. #endif
  94. #if DETAILTEXTURE
  95. sampler DetailSampler : register( s12 );
  96. #endif
  97. sampler BumpmapSampler : register( s4 );
  98. #if NORMAL_DECODE_MODE == NORM_DECODE_ATI2N_ALPHA
  99. sampler AlphaMapSampler : register( s9 ); // alpha
  100. #else
  101. #define AlphaMapSampler BumpmapSampler
  102. #endif
  103. #if BUMPMAP2 == 1
  104. sampler BumpmapSampler2 : register( s5 );
  105. #if NORMAL_DECODE_MODE == NORM_DECODE_ATI2N_ALPHA
  106. sampler AlphaMapSampler2 : register( s10 ); // alpha
  107. #else
  108. #define AlphaMapSampler2 BumpmapSampler2
  109. #endif
  110. #else
  111. sampler EnvmapMaskSampler : register( s5 );
  112. #endif
  113. #if WARPLIGHTING
  114. sampler WarpLightingSampler : register( s6 );
  115. #endif
  116. sampler BaseTextureSampler2 : register( s7 );
  117. #if BUMPMASK == 1
  118. sampler BumpMaskSampler : register( s8 );
  119. #if NORMALMASK_DECODE_MODE == NORM_DECODE_ATI2N_ALPHA
  120. sampler AlphaMaskSampler : register( s11 ); // alpha
  121. #else
  122. #define AlphaMaskSampler BumpMaskSampler
  123. #endif
  124. #endif
  125. #if defined( _X360 ) && FLASHLIGHT
  126. sampler FlashlightSampler : register( s13 );
  127. sampler ShadowDepthSampler : register( s14 );
  128. sampler RandRotSampler : register( s15 );
  129. #endif
  130. struct PS_INPUT
  131. {
  132. #if SEAMLESS
  133. float3 SeamlessTexCoord : TEXCOORD0; // zy xz
  134. float4 detailOrBumpAndEnvmapMaskTexCoord : TEXCOORD1; // envmap mask
  135. #else
  136. HALF2 baseTexCoord : TEXCOORD0;
  137. // detail textures and bumpmaps are mutually exclusive so that we have enough texcoords.
  138. #if ( RELIEF_MAPPING == 0 )
  139. HALF4 detailOrBumpAndEnvmapMaskTexCoord : TEXCOORD1;
  140. #endif
  141. #endif
  142. // CENTROID: TEXCOORD2
  143. HALF4 lightmapTexCoord1And2 : TEXCOORD2;
  144. // CENTROID: TEXCOORD3
  145. HALF4 lightmapTexCoord3 : TEXCOORD3;
  146. HALF4 worldPos_projPosZ : TEXCOORD4;
  147. HALF3x3 tangentSpaceTranspose : TEXCOORD5;
  148. // tangentSpaceTranspose : TEXCOORD6
  149. // tangentSpaceTranspose : TEXCOORD7
  150. HALF4 vertexColor : COLOR;
  151. float4 vertexBlendX_fogFactorW : COLOR1;
  152. // Extra iterators on 360, used in flashlight combo
  153. #if defined( _X360 ) && FLASHLIGHT
  154. float4 flashlightSpacePos : TEXCOORD8;
  155. float4 vProjPos : TEXCOORD9;
  156. #endif
  157. };
  158. #if LIGHTING_PREVIEW == 2
  159. LPREVIEW_PS_OUT main( PS_INPUT i ) : COLOR
  160. #else
  161. HALF4 main( PS_INPUT i ) : COLOR
  162. #endif
  163. {
  164. bool bBaseTexture2 = BASETEXTURE2 ? true : false;
  165. bool bDetailTexture = DETAILTEXTURE ? true : false;
  166. bool bBumpmap = BUMPMAP ? true : false;
  167. bool bDiffuseBumpmap = DIFFUSEBUMPMAP ? true : false;
  168. bool bCubemap = CUBEMAP ? true : false;
  169. bool bEnvmapMask = ENVMAPMASK ? true : false;
  170. bool bBaseAlphaEnvmapMask = BASEALPHAENVMAPMASK ? true : false;
  171. bool bSelfIllum = SELFILLUM ? true : false;
  172. bool bNormalMapAlphaEnvmapMask = NORMALMAPALPHAENVMAPMASK ? true : false;
  173. bool bBaseTextureNoEnvmap = BASETEXTURENOENVMAP ? true : false;
  174. bool bBaseTexture2NoEnvmap = BASETEXTURE2NOENVMAP ? true : false;
  175. float4 baseColor = 0.0f;
  176. float4 baseColor2 = 0.0f;
  177. float4 vNormal = float4(0, 0, 1, 1);
  178. float3 baseTexCoords = float3(0,0,0);
  179. #if SEAMLESS
  180. baseTexCoords = i.SeamlessTexCoord.xyz;
  181. #else
  182. baseTexCoords.xy = i.baseTexCoord.xy;
  183. #endif
  184. GetBaseTextureAndNormal( BaseTextureSampler, BaseTextureSampler2, BumpmapSampler, bBaseTexture2, bBumpmap || bNormalMapAlphaEnvmapMask,
  185. baseTexCoords, i.vertexColor.rgb, baseColor, baseColor2, vNormal );
  186. #if BUMPMAP == 1 // not ssbump
  187. vNormal.xyz = vNormal.xyz * 2.0f - 1.0f; // make signed if we're not ssbump
  188. #endif
  189. HALF3 lightmapColor1 = HALF3( 1.0f, 1.0f, 1.0f );
  190. HALF3 lightmapColor2 = HALF3( 1.0f, 1.0f, 1.0f );
  191. HALF3 lightmapColor3 = HALF3( 1.0f, 1.0f, 1.0f );
  192. #if LIGHTING_PREVIEW == 0
  193. if( bBumpmap && bDiffuseBumpmap )
  194. {
  195. HALF2 bumpCoord1;
  196. HALF2 bumpCoord2;
  197. HALF2 bumpCoord3;
  198. ComputeBumpedLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy,
  199. bumpCoord1, bumpCoord2, bumpCoord3 );
  200. lightmapColor1 = LightMapSample( LightmapSampler, bumpCoord1 );
  201. lightmapColor2 = LightMapSample( LightmapSampler, bumpCoord2 );
  202. lightmapColor3 = LightMapSample( LightmapSampler, bumpCoord3 );
  203. }
  204. else
  205. {
  206. HALF2 bumpCoord1 = ComputeLightmapCoordinates( i.lightmapTexCoord1And2, i.lightmapTexCoord3.xy );
  207. lightmapColor1 = LightMapSample( LightmapSampler, bumpCoord1 );
  208. }
  209. #endif
  210. #if RELIEF_MAPPING
  211. // in the parallax case, all texcoords must be the same in order to free
  212. // up an iterator for the tangent space view vector
  213. HALF2 detailTexCoord = i.baseTexCoord.xy;
  214. HALF2 bumpmapTexCoord = i.baseTexCoord.xy;
  215. HALF2 envmapMaskTexCoord = i.baseTexCoord.xy;
  216. #else
  217. #if ( DETAILTEXTURE == 1 )
  218. HALF2 detailTexCoord = i.detailOrBumpAndEnvmapMaskTexCoord.xy;
  219. HALF2 bumpmapTexCoord = i.baseTexCoord.xy;
  220. #elif ( BUMPMASK == 1 )
  221. HALF2 detailTexCoord = 0.0f;
  222. HALF2 bumpmapTexCoord = i.detailOrBumpAndEnvmapMaskTexCoord.xy;
  223. HALF2 bumpmap2TexCoord = i.detailOrBumpAndEnvmapMaskTexCoord.wz;
  224. #else
  225. HALF2 detailTexCoord = 0.0f;
  226. HALF2 bumpmapTexCoord = i.detailOrBumpAndEnvmapMaskTexCoord.xy;
  227. #endif
  228. HALF2 envmapMaskTexCoord = i.detailOrBumpAndEnvmapMaskTexCoord.wz;
  229. #endif // !RELIEF_MAPPING
  230. HALF4 detailColor = HALF4( 1.0f, 1.0f, 1.0f, 1.0f );
  231. #if DETAILTEXTURE
  232. #if SHADER_MODEL_PS_2_0
  233. detailColor = tex2D( DetailSampler, detailTexCoord );
  234. #else
  235. detailColor = float4( g_DetailTint, 1.0f ) * tex2D( DetailSampler, detailTexCoord );
  236. #endif
  237. #endif
  238. #if ( OUTLINE || SOFTEDGES )
  239. float distAlphaMask = baseColor.a;
  240. # if OUTLINE
  241. if ( ( distAlphaMask >= OUTLINE_MIN_VALUE0 ) &&
  242. ( distAlphaMask <= OUTLINE_MAX_VALUE1 ) )
  243. {
  244. float oFactor=1.0;
  245. if ( distAlphaMask <= OUTLINE_MIN_VALUE1 )
  246. {
  247. oFactor=smoothstep( OUTLINE_MIN_VALUE0, OUTLINE_MIN_VALUE1, distAlphaMask );
  248. }
  249. else
  250. {
  251. oFactor=smoothstep( OUTLINE_MAX_VALUE1, OUTLINE_MAX_VALUE0, distAlphaMask );
  252. }
  253. baseColor = lerp( baseColor, OUTLINE_COLOR, oFactor );
  254. }
  255. # endif
  256. # if SOFTEDGES
  257. baseColor.a *= smoothstep( SOFT_MASK_MAX, SOFT_MASK_MIN, distAlphaMask );
  258. # else
  259. baseColor.a *= distAlphaMask >= 0.5;
  260. # endif
  261. #endif
  262. #if LIGHTING_PREVIEW == 2
  263. baseColor.xyz=GammaToLinear(baseColor.xyz);
  264. #endif
  265. float blendedAlpha = baseColor.a;
  266. #if MASKEDBLENDING
  267. float blendfactor=0.5;
  268. #else
  269. float blendfactor=i.vertexBlendX_fogFactorW.r;
  270. #endif
  271. if( bBaseTexture2 )
  272. {
  273. #if (SELFILLUM == 0) && (PIXELFOGTYPE != PIXEL_FOG_TYPE_HEIGHT) && (FANCY_BLENDING)
  274. float4 modt=tex2D(BlendModulationSampler,i.lightmapTexCoord3.zw);
  275. #if MASKEDBLENDING
  276. // FXC is unable to optimize this, despite blendfactor=0.5 above
  277. //float minb=modt.g-modt.r;
  278. //float maxb=modt.g+modt.r;
  279. //blendfactor=smoothstep(minb,maxb,blendfactor);
  280. blendfactor=modt.g;
  281. #else
  282. float minb=saturate(modt.g-modt.r);
  283. float maxb=saturate(modt.g+modt.r);
  284. blendfactor=smoothstep(minb,maxb,blendfactor);
  285. #endif
  286. #endif
  287. baseColor.rgb = lerp( baseColor, baseColor2.rgb, blendfactor );
  288. blendedAlpha = lerp( baseColor.a, baseColor2.a, blendfactor );
  289. }
  290. HALF3 specularFactor = 1.0f;
  291. float4 vNormalMask = float4(0, 0, 1, 1);
  292. if( bBumpmap )
  293. {
  294. if( bBaseTextureNoEnvmap )
  295. {
  296. vNormal.a = 0.0f;
  297. }
  298. #if ( BUMPMAP2 == 1 )
  299. {
  300. #if ( BUMPMASK == 1 )
  301. HALF2 b2TexCoord = bumpmap2TexCoord;
  302. #else
  303. HALF2 b2TexCoord = bumpmapTexCoord;
  304. #endif
  305. HALF4 vNormal2;
  306. if ( BUMPMAP == 2 )
  307. vNormal2 = tex2D( BumpmapSampler2, b2TexCoord );
  308. else
  309. vNormal2 = DecompressNormal( BumpmapSampler2, b2TexCoord, NORMAL_DECODE_MODE, AlphaMapSampler2 ); // Bump 2 coords
  310. if( bBaseTexture2NoEnvmap )
  311. {
  312. vNormal2.a = 0.0f;
  313. }
  314. #if ( BUMPMASK == 1 )
  315. float3 vNormal1 = DecompressNormal( BumpmapSampler, i.detailOrBumpAndEnvmapMaskTexCoord.xy, NORMALMASK_DECODE_MODE, AlphaMapSampler );
  316. vNormal.xyz = normalize( vNormal1.xyz + vNormal2.xyz );
  317. // Third normal map...same coords as base
  318. vNormalMask = DecompressNormal( BumpMaskSampler, i.baseTexCoord.xy, NORMALMASK_DECODE_MODE, AlphaMaskSampler );
  319. vNormal.xyz = lerp( vNormalMask.xyz, vNormal.xyz, vNormalMask.a ); // Mask out normals from vNormal
  320. specularFactor = vNormalMask.a;
  321. #else // BUMPMASK == 0
  322. if ( FANCY_BLENDING && bNormalMapAlphaEnvmapMask )
  323. {
  324. vNormal = lerp( vNormal, vNormal2, blendfactor);
  325. }
  326. else
  327. {
  328. vNormal.xyz = lerp( vNormal.xyz, vNormal2.xyz, blendfactor);
  329. }
  330. #endif
  331. }
  332. #endif // BUMPMAP2 == 1
  333. if( bNormalMapAlphaEnvmapMask )
  334. {
  335. specularFactor *= vNormal.a;
  336. }
  337. }
  338. else if ( bNormalMapAlphaEnvmapMask )
  339. {
  340. specularFactor *= vNormal.a;
  341. }
  342. #if ( BUMPMAP2 == 0 )
  343. if( bEnvmapMask )
  344. {
  345. specularFactor *= tex2D( EnvmapMaskSampler, envmapMaskTexCoord ).xyz;
  346. }
  347. #endif
  348. if( bBaseAlphaEnvmapMask )
  349. {
  350. specularFactor *= 1.0 - blendedAlpha; // Reversing alpha blows!
  351. }
  352. float4 albedo = float4( 1.0f, 1.0f, 1.0f, 1.0f );
  353. float alpha = 1.0f;
  354. albedo *= baseColor;
  355. if( !bBaseAlphaEnvmapMask && !bSelfIllum )
  356. {
  357. alpha *= baseColor.a;
  358. }
  359. if( bDetailTexture )
  360. {
  361. albedo = TextureCombine( albedo, detailColor, DETAIL_BLEND_MODE, g_DetailBlendFactor );
  362. }
  363. // The vertex color contains the modulation color + vertex color combined
  364. #if ( SEAMLESS == 0 )
  365. albedo.xyz *= i.vertexColor;
  366. #endif
  367. alpha *= i.vertexColor.a * g_flAlpha2; // not sure about this one
  368. // Save this off for single-pass flashlight, since we'll still need the SSBump vector, not a real normal
  369. float3 vSSBumpVector = vNormal.xyz;
  370. HALF3 diffuseLighting;
  371. if( bBumpmap && bDiffuseBumpmap )
  372. {
  373. // ssbump
  374. #if ( BUMPMAP == 2 )
  375. diffuseLighting = vNormal.x * lightmapColor1 +
  376. vNormal.y * lightmapColor2 +
  377. vNormal.z * lightmapColor3;
  378. diffuseLighting *= g_TintValuesAndLightmapScale.rgb;
  379. // now, calculate vNormal for reflection purposes. if vNormal isn't needed, hopefully
  380. // the compiler will eliminate these calculations
  381. vNormal.xyz = normalize( bumpBasis[0]*vNormal.x + bumpBasis[1]*vNormal.y + bumpBasis[2]*vNormal.z);
  382. #else
  383. float3 dp;
  384. dp.x = saturate( dot( vNormal, bumpBasis[0] ) );
  385. dp.y = saturate( dot( vNormal, bumpBasis[1] ) );
  386. dp.z = saturate( dot( vNormal, bumpBasis[2] ) );
  387. dp *= dp;
  388. #if ( DETAIL_BLEND_MODE == TCOMBINE_SSBUMP_BUMP )
  389. dp *= 2*detailColor;
  390. #endif
  391. diffuseLighting = dp.x * lightmapColor1 +
  392. dp.y * lightmapColor2 +
  393. dp.z * lightmapColor3;
  394. float sum = dot( dp, float3( 1.0f, 1.0f, 1.0f ) );
  395. diffuseLighting *= g_TintValuesAndLightmapScale.rgb / sum;
  396. #endif
  397. }
  398. else
  399. {
  400. diffuseLighting = lightmapColor1 * g_TintValuesAndLightmapScale.rgb;
  401. }
  402. #if WARPLIGHTING && ( SEAMLESS == 0 )
  403. float len=0.5*length(diffuseLighting);
  404. // FIXME: 8-bit lookup textures like this need a "nice filtering" VTF option, which converts
  405. // them to 16-bit on load or does filtering in the shader (since most hardware - 360
  406. // included - interpolates 8-bit textures at 8-bit precision, which causes banding)
  407. diffuseLighting *= 2.0*tex2D(WarpLightingSampler,float2(len,0));
  408. #endif
  409. #if CUBEMAP || LIGHTING_PREVIEW || ( defined( _X360 ) && FLASHLIGHT )
  410. float3 worldSpaceNormal = mul( vNormal, i.tangentSpaceTranspose );
  411. #endif
  412. float3 diffuseComponent = albedo.xyz * diffuseLighting;
  413. #if defined( _X360 ) && FLASHLIGHT
  414. // ssbump doesn't pass a normal to the flashlight...it computes shadowing a different way
  415. #if ( BUMPMAP == 2 )
  416. bool bHasNormal = false;
  417. float3 worldPosToLightVector = g_FlashlightPos - i.worldPos_projPosZ.xyz;
  418. float3 tangentPosToLightVector;
  419. tangentPosToLightVector.x = dot( worldPosToLightVector, i.tangentSpaceTranspose[0] );
  420. tangentPosToLightVector.y = dot( worldPosToLightVector, i.tangentSpaceTranspose[1] );
  421. tangentPosToLightVector.z = dot( worldPosToLightVector, i.tangentSpaceTranspose[2] );
  422. tangentPosToLightVector = normalize( tangentPosToLightVector );
  423. float nDotL = saturate( vSSBumpVector.x*dot( tangentPosToLightVector, bumpBasis[0]) +
  424. vSSBumpVector.y*dot( tangentPosToLightVector, bumpBasis[1]) +
  425. vSSBumpVector.z*dot( tangentPosToLightVector, bumpBasis[2]) );
  426. #else
  427. bool bHasNormal = true;
  428. float nDotL = 1.0f;
  429. #endif
  430. float fFlashlight = DoFlashlight( g_FlashlightPos, i.worldPos_projPosZ.xyz, i.flashlightSpacePos,
  431. worldSpaceNormal, g_FlashlightAttenuationFactors.xyz,
  432. g_FlashlightAttenuationFactors.w, FlashlightSampler, ShadowDepthSampler,
  433. RandRotSampler, 0, true, false, i.vProjPos.xy / i.vProjPos.w, false, g_ShadowTweaks, bHasNormal );
  434. diffuseComponent = albedo.xyz * ( diffuseLighting + ( fFlashlight * nDotL ) );
  435. #endif
  436. if( bSelfIllum )
  437. {
  438. float3 selfIllumComponent = g_SelfIllumTint * albedo.xyz;
  439. diffuseComponent = lerp( diffuseComponent, selfIllumComponent, baseColor.a );
  440. }
  441. HALF3 specularLighting = HALF3( 0.0f, 0.0f, 0.0f );
  442. #if CUBEMAP
  443. if( bCubemap )
  444. {
  445. float3 worldVertToEyeVector = g_EyePos - i.worldPos_projPosZ.xyz;
  446. float3 reflectVect = CalcReflectionVectorUnnormalized( worldSpaceNormal, worldVertToEyeVector );
  447. // Calc Fresnel factor
  448. half3 eyeVect = normalize(worldVertToEyeVector);
  449. HALF fresnel = 1.0 - dot( worldSpaceNormal, eyeVect );
  450. fresnel = pow( fresnel, 5.0 );
  451. fresnel = fresnel * g_OneMinusFresnelReflection + g_FresnelReflection;
  452. specularLighting = ENV_MAP_SCALE * texCUBE( EnvmapSampler, reflectVect );
  453. specularLighting *= specularFactor;
  454. specularLighting *= g_EnvmapTint;
  455. #if FANCY_BLENDING == 0
  456. HALF3 specularLightingSquared = specularLighting * specularLighting;
  457. specularLighting = lerp( specularLighting, specularLightingSquared, g_EnvmapContrast );
  458. HALF3 greyScale = dot( specularLighting, HALF3( 0.299f, 0.587f, 0.114f ) );
  459. specularLighting = lerp( greyScale, specularLighting, g_EnvmapSaturation );
  460. #endif
  461. specularLighting *= fresnel;
  462. }
  463. #endif
  464. HALF3 result = diffuseComponent + specularLighting;
  465. #if LIGHTING_PREVIEW
  466. worldSpaceNormal = mul( vNormal, i.tangentSpaceTranspose );
  467. # if LIGHTING_PREVIEW == 1
  468. float dotprod = 0.7+0.25 * dot( worldSpaceNormal, normalize( float3( 1, 2, -.5 ) ) );
  469. return FinalOutput( HALF4( dotprod*albedo.xyz, alpha ), 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  470. # else
  471. LPREVIEW_PS_OUT ret;
  472. ret.color = float4( albedo.xyz,alpha );
  473. ret.normal = float4( worldSpaceNormal,alpha );
  474. ret.position = float4( i.worldPos_projPosZ.xyz, alpha );
  475. ret.flags = float4( 1, 1, 1, alpha );
  476. return FinalOutput( ret, 0, PIXEL_FOG_TYPE_NONE, TONEMAP_SCALE_NONE );
  477. # endif
  478. #else // == end LIGHTING_PREVIEW ==
  479. bool bWriteDepthToAlpha = false;
  480. // ps_2_b and beyond
  481. #if !(defined(SHADER_MODEL_PS_1_1) || defined(SHADER_MODEL_PS_1_4) || defined(SHADER_MODEL_PS_2_0))
  482. bWriteDepthToAlpha = ( WRITE_DEPTH_TO_DESTALPHA != 0 ) && ( WRITEWATERFOGTODESTALPHA == 0 );
  483. #endif
  484. float fogFactor = CalcPixelFogFactor( PIXELFOGTYPE, g_FogParams, g_EyePos.z, i.worldPos_projPosZ.z, i.worldPos_projPosZ.w );
  485. #if WRITEWATERFOGTODESTALPHA && (PIXELFOGTYPE == PIXEL_FOG_TYPE_HEIGHT)
  486. alpha = fogFactor;
  487. #endif
  488. return FinalOutput( float4( result.rgb, alpha ), fogFactor, PIXELFOGTYPE, TONEMAP_SCALE_LINEAR, bWriteDepthToAlpha, i.worldPos_projPosZ.w );
  489. #endif
  490. }