PrepassLight.hlsl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = Sample(sDepthBuffer, iScreenPos).r;
  51. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  52. #else
  53. float depth = Sample(sDepthBuffer, iScreenPos).r;
  54. float3 worldPos = iFarRay * depth;
  55. #endif
  56. float4 normalInput = Sample(sNormalBuffer, iScreenPos);
  57. #else
  58. #ifdef ORTHO
  59. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  60. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  61. #else
  62. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  63. float3 worldPos = iFarRay * depth / iScreenPos.w;
  64. #endif
  65. float4 normalInput = tex2Dproj(sNormalBuffer, iScreenPos);
  66. #endif
  67. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  68. float4 projWorldPos = float4(worldPos, 1.0);
  69. float3 lightColor;
  70. float3 lightDir;
  71. // Accumulate light at half intensity to allow 2x "overburn"
  72. float diff = 0.5 * GetDiffuse(normal, worldPos, lightDir);
  73. #ifdef SHADOW
  74. diff *= GetShadowDeferred(projWorldPos, depth);
  75. #endif
  76. #if defined(SPOTLIGHT)
  77. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  78. lightColor = spotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  79. #elif defined(CUBEMASK)
  80. lightColor = texCUBE(sLightCubeMap, mul(worldPos - cLightPosPS.xyz, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  81. #else
  82. lightColor = cLightColor.rgb;
  83. #endif
  84. #ifdef SPECULAR
  85. float spec = lightColor.g * GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  86. oColor = diff * float4(lightColor, spec * cLightColor.a);
  87. #else
  88. oColor = diff * float4(lightColor, 0.0);
  89. #endif
  90. }