PrepassLight.hlsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. void VS(float4 iPos : POSITION,
  7. #ifdef DIRLIGHT
  8. out float2 oScreenPos : TEXCOORD0,
  9. #else
  10. out float4 oScreenPos : TEXCOORD0,
  11. #endif
  12. out float3 oFarRay : TEXCOORD1,
  13. #ifdef ORTHO
  14. out float3 oNearRay : TEXCOORD2,
  15. #endif
  16. out float4 oPos : POSITION)
  17. {
  18. float4x3 modelMatrix = iModelMatrix;
  19. float3 worldPos = GetWorldPos(modelMatrix);
  20. oPos = GetClipPos(worldPos);
  21. #ifdef DIRLIGHT
  22. oScreenPos = GetScreenPosPreDiv(oPos);
  23. oFarRay = GetFarRay(oPos);
  24. #ifdef ORTHO
  25. oNearRay = GetNearRay(oPos);
  26. #endif
  27. #else
  28. oScreenPos = GetScreenPos(oPos);
  29. oFarRay = GetFarRay(oPos) * oPos.w;
  30. #ifdef ORTHO
  31. oNearRay = GetNearRay(oPos) * oPos.w;
  32. #endif
  33. #endif
  34. }
  35. void PS(
  36. #ifdef DIRLIGHT
  37. float2 iScreenPos : TEXCOORD0,
  38. #else
  39. float4 iScreenPos : TEXCOORD0,
  40. #endif
  41. float3 iFarRay : TEXCOORD1,
  42. #ifdef ORTHO
  43. float3 iNearRay : TEXCOORD2,
  44. #endif
  45. out float4 oColor : COLOR0)
  46. {
  47. // If rendering a directional light quad, optimize out the w divide
  48. #ifdef DIRLIGHT
  49. #ifdef ORTHO
  50. float depth = tex2D(sDepthBuffer, iScreenPos).r;
  51. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  52. #else
  53. #ifdef LINEAR
  54. float depth = tex2D(sDepthBuffer, iScreenPos).r;
  55. #else
  56. float depth = ReconstructDepth(tex2D(sDepthBuffer, iScreenPos).r);
  57. #endif
  58. float3 worldPos = iFarRay * depth;
  59. #endif
  60. float4 normalInput = tex2D(sNormalBuffer, iScreenPos);
  61. #else
  62. #ifdef ORTHO
  63. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  64. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  65. #else
  66. #ifdef LINEAR
  67. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  68. #else
  69. float depth = ReconstructDepth(tex2Dproj(sDepthBuffer, iScreenPos).r);
  70. #endif
  71. float3 worldPos = iFarRay * depth / iScreenPos.w;
  72. #endif
  73. float4 normalInput = tex2Dproj(sNormalBuffer, iScreenPos);
  74. #endif
  75. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  76. float4 projWorldPos = float4(worldPos, 1.0);
  77. float3 lightColor;
  78. float3 lightDir;
  79. float diff;
  80. // Accumulate light at half intensity to allow 2x "overburn"
  81. #ifdef DIRLIGHT
  82. lightDir = cLightDirPS;
  83. diff = 0.5 * GetDiffuseDir(normal, lightDir);
  84. #else
  85. float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  86. diff = 0.5 * GetDiffusePointOrSpot(normal, lightVec, lightDir);
  87. #endif
  88. #ifdef SHADOW
  89. #if defined(DIRLIGHT)
  90. float4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
  91. diff *= saturate(GetShadow(shadowPos) + GetShadowFade(depth));
  92. #elif defined(SPOTLIGHT)
  93. float4 shadowPos = mul(projWorldPos, cLightMatricesPS[1]);
  94. diff *= GetShadow(shadowPos);
  95. #else
  96. float3 shadowPos = worldPos - cLightPosPS.xyz;
  97. diff *= GetCubeShadow(shadowPos);
  98. #endif
  99. #endif
  100. #if defined(SPOTLIGHT)
  101. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  102. lightColor = spotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  103. #elif defined(CUBEMASK)
  104. lightColor = texCUBE(sLightCubeMap, mul(lightVec, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  105. #else
  106. lightColor = cLightColor.rgb;
  107. #endif
  108. #ifdef SPECULAR
  109. float spec = lightColor.g * GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  110. oColor = diff * float4(lightColor, spec * cLightColor.a);
  111. #else
  112. oColor = diff * float4(lightColor, 0.0);
  113. #endif
  114. }