PrepassLight.hlsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 : OUTPOSITION)
  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 : OUTCOLOR0)
  46. {
  47. // If rendering a directional light quad, optimize out the w divide
  48. #ifdef DIRLIGHT
  49. float depth = Sample2DLod0(DepthBuffer, iScreenPos).r;
  50. #ifdef HWDEPTH
  51. depth = ReconstructDepth(depth);
  52. #endif
  53. #ifdef ORTHO
  54. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  55. #else
  56. float3 worldPos = iFarRay * depth;
  57. #endif
  58. float4 normalInput = Sample2DLod0(NormalBuffer, iScreenPos);
  59. #else
  60. float depth = Sample2DProj(DepthBuffer, iScreenPos).r;
  61. #ifdef HWDEPTH
  62. depth = ReconstructDepth(depth);
  63. #endif
  64. #ifdef ORTHO
  65. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  66. #else
  67. float3 worldPos = iFarRay * depth / iScreenPos.w;
  68. #endif
  69. float4 normalInput = Sample2DProj(NormalBuffer, iScreenPos);
  70. #endif
  71. // Position acquired via near/far ray is relative to camera. Bring position to world space
  72. float3 eyeVec = -worldPos;
  73. worldPos += cCameraPosPS;
  74. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  75. float4 projWorldPos = float4(worldPos, 1.0);
  76. float3 lightColor;
  77. float3 lightDir;
  78. // Accumulate light at half intensity to allow 2x "overburn"
  79. float diff = 0.5 * GetDiffuse(normal, worldPos, lightDir);
  80. #ifdef SHADOW
  81. diff *= GetShadowDeferred(projWorldPos, normal, depth);
  82. #endif
  83. #if defined(SPOTLIGHT)
  84. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  85. lightColor = spotPos.w > 0.0 ? Sample2DProj(LightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  86. #elif defined(CUBEMASK)
  87. lightColor = texCUBE(sLightCubeMap, mul(worldPos - cLightPosPS.xyz, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  88. #else
  89. lightColor = cLightColor.rgb;
  90. #endif
  91. #ifdef SPECULAR
  92. float spec = lightColor.g * GetSpecular(normal, eyeVec, lightDir, normalInput.a * 255.0);
  93. oColor = diff * float4(lightColor, spec * cLightColor.a);
  94. #else
  95. oColor = diff * float4(lightColor, 0.0);
  96. #endif
  97. }