Lighting.hlsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma warning(disable:3571)
  2. float GetDiffuseDir(float3 normal, float3 lightDir)
  3. {
  4. return saturate(dot(normal, lightDir));
  5. }
  6. float GetDiffusePointOrSpot(float3 normal, float3 lightVec, out float3 lightDir)
  7. {
  8. float lightDist = length(lightVec);
  9. lightDir = lightVec / lightDist;
  10. return saturate(dot(normal, lightDir)) * tex1D(sLightRampMap, lightDist).r;
  11. }
  12. float GetDiffuseDirVolumetric()
  13. {
  14. return 1.0;
  15. }
  16. float GetDiffusePointOrSpotVolumetric(float3 lightVec)
  17. {
  18. float lightDist = length(lightVec);
  19. return tex1D(sLightRampMap, lightDist).r;
  20. }
  21. float GetSpecular(float3 normal, float3 eyeVec, float3 lightDir, float specularPower)
  22. {
  23. float3 halfVec = normalize(normalize(eyeVec) + lightDir);
  24. return pow(dot(normal, halfVec), specularPower);
  25. }
  26. float GetVertexLight(int index, float3 worldPos, float3 normal)
  27. {
  28. float3 lightDir = cVertexLights[index * 3 + 1].xyz;
  29. float3 lightPos = cVertexLights[index * 3 + 2].xyz;
  30. float invRange = cVertexLights[index * 3].w;
  31. float cutoff = cVertexLights[index * 3 + 1].w;
  32. float invCutoff = cVertexLights[index * 3 + 2].w;
  33. // Directional light
  34. if (invRange == 0.0)
  35. {
  36. float NdotL = max(dot(normal, lightDir), 0.0);
  37. return NdotL;
  38. }
  39. // Point/spot light
  40. else
  41. {
  42. float3 lightVec = (lightPos - worldPos) * invRange;
  43. float lightDist = length(lightVec);
  44. float3 localDir = lightVec / lightDist;
  45. float NdotL = max(dot(normal, localDir), 0.0);
  46. float atten = saturate(1.0 - lightDist * lightDist);
  47. float spotEffect = dot(localDir, lightDir);
  48. float spotAtten = saturate((spotEffect - cutoff) * invCutoff);
  49. return NdotL * atten * spotAtten;
  50. }
  51. }
  52. float3 GetAmbient(float zonePos)
  53. {
  54. return cAmbientStartColor + zonePos * cAmbientEndColor;
  55. }
  56. float GetShadow(float4 shadowPos)
  57. {
  58. // Note: in case of sampling a point light cube shadow, we optimize out the w divide as it has already been performed
  59. #ifndef FALLBACK
  60. #ifndef LQSHADOW
  61. // Take four samples and average them
  62. #ifndef POINTLIGHT
  63. float2 offsets = cSampleOffsets * shadowPos.w;
  64. #else
  65. float2 offsets = cSampleOffsets;
  66. #endif
  67. float4 inLight = float4(
  68. tex2Dproj(sShadowMap, shadowPos).r,
  69. tex2Dproj(sShadowMap, float4(shadowPos.x + offsets.x, shadowPos.yzw)).r,
  70. tex2Dproj(sShadowMap, float4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r,
  71. tex2Dproj(sShadowMap, float4(shadowPos.xy + offsets.xy, shadowPos.zw)).r
  72. );
  73. #ifdef HWSHADOW
  74. return cShadowIntensity.z + dot(inLight, cShadowIntensity.y);
  75. #else
  76. #ifndef POINTLIGHT
  77. return cShadowIntensity.z + dot(inLight * shadowPos.w > shadowPos.z, cShadowIntensity.y);
  78. #else
  79. return cShadowIntensity.z + dot(inLight > shadowPos.z, cShadowIntensity.y);
  80. #endif
  81. #endif
  82. #else
  83. // Take one sample
  84. float inLight = tex2Dproj(sShadowMap, shadowPos).r;
  85. #ifdef HWSHADOW
  86. return cShadowIntensity.z + cShadowIntensity.x * inLight;
  87. #else
  88. #ifndef POINTLIGHT
  89. return cShadowIntensity.z + cShadowIntensity.x * (inLight * shadowPos.w > shadowPos.z);
  90. #else
  91. return cShadowIntensity.z + cShadowIntensity.x * (inLight > shadowPos.z);
  92. #endif
  93. #endif
  94. #endif
  95. #else
  96. // Fallback mode: take two samples, depth needs to be decoded from RG channels
  97. #ifndef POINTLIGHT
  98. float2 offsets = cSampleOffsets * shadowPos.w;
  99. #else
  100. float2 offsets = cSampleOffsets;
  101. #endif
  102. float2 inLight = float2(
  103. DecodeDepth(tex2Dproj(sShadowMap, shadowPos).rg),
  104. DecodeDepth(tex2Dproj(sShadowMap, float4(shadowPos.x + offsets.x, shadowPos.yzw)).rg)
  105. );
  106. #ifndef POINTLIGHT
  107. return cShadowIntensity.z + dot(inLight * shadowPos.w > shadowPos.z, cShadowIntensity.y);
  108. #else
  109. return cShadowIntensity.z + dot(inLight > shadowPos.z, cShadowIntensity.y);
  110. #endif
  111. #endif
  112. }
  113. float GetCubeShadow(float3 lightVec)
  114. {
  115. float3 axis = texCUBE(sFaceSelectCubeMap, lightVec).rgb;
  116. float depth = dot(abs(lightVec), axis);
  117. // Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
  118. // and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
  119. // case filtering across faces is wrong
  120. const float factor = 1.0 / 256.0;
  121. lightVec += factor * axis * lightVec;
  122. // Read the 2D UV coordinates, adjust according to shadow map size and add face offset
  123. float4 indirectPos = texCUBE(sIndirectionCubeMap, lightVec);
  124. indirectPos.xy *= cShadowCubeAdjust.xy;
  125. indirectPos.xy += float2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
  126. float4 shadowPos = float4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
  127. return GetShadow(shadowPos);
  128. }
  129. float GetShadowFade(float depth)
  130. {
  131. return saturate((depth - cShadowDepthFade.z) * cShadowDepthFade.w);
  132. }
  133. float4 GetDirShadowPos(const float4 iShadowPos[4], float depth)
  134. {
  135. if (depth < cShadowSplits.x)
  136. return iShadowPos[0];
  137. else if (depth < cShadowSplits.y)
  138. return iShadowPos[1];
  139. else if (depth < cShadowSplits.z)
  140. return iShadowPos[2];
  141. else
  142. return iShadowPos[3];
  143. }
  144. float4x4 GetDirShadowMatrix(float depth)
  145. {
  146. #ifdef SM3
  147. if (depth < cShadowSplits.x)
  148. return cShadowProjPS[0];
  149. else if (depth < cShadowSplits.y)
  150. return cShadowProjPS[1];
  151. else if (depth < cShadowSplits.z)
  152. return cShadowProjPS[2];
  153. else
  154. return cShadowProjPS[3];
  155. #else
  156. if (depth < cShadowSplits.x)
  157. return cShadowProjPS[0];
  158. else if (depth < cShadowSplits.y)
  159. return cShadowProjPS[1];
  160. else
  161. return cShadowProjPS[2];
  162. #endif
  163. }
  164. float GetIntensity(float3 color)
  165. {
  166. return dot(color, float3(0.333, 0.333, 0.333));
  167. }