DeferredLight.hlsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 albedoInput = Sample2DLod0(AlbedoBuffer, iScreenPos);
  59. float4 normalInput = Sample2DLod0(NormalBuffer, iScreenPos);
  60. #else
  61. float depth = Sample2DProj(DepthBuffer, iScreenPos).r;
  62. #ifdef HWDEPTH
  63. depth = ReconstructDepth(depth);
  64. #endif
  65. #ifdef ORTHO
  66. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  67. #else
  68. float3 worldPos = iFarRay * depth / iScreenPos.w;
  69. #endif
  70. float4 albedoInput = Sample2DProj(AlbedoBuffer, iScreenPos);
  71. float4 normalInput = Sample2DProj(NormalBuffer, iScreenPos);
  72. #endif
  73. // Position acquired via near/far ray is relative to camera. Bring position to world space
  74. float3 eyeVec = -worldPos;
  75. worldPos += cCameraPosPS;
  76. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  77. float4 projWorldPos = float4(worldPos, 1.0);
  78. float3 lightColor;
  79. float3 lightDir;
  80. float diff = GetDiffuse(normal, worldPos, lightDir);
  81. #ifdef SHADOW
  82. diff *= GetShadowDeferred(projWorldPos, normal, depth);
  83. #endif
  84. #if defined(SPOTLIGHT)
  85. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  86. lightColor = spotPos.w > 0.0 ? Sample2DProj(LightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  87. #elif defined(CUBEMASK)
  88. lightColor = texCUBE(sLightCubeMap, mul(worldPos - cLightPosPS.xyz, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  89. #else
  90. lightColor = cLightColor.rgb;
  91. #endif
  92. #ifdef SPECULAR
  93. float spec = GetSpecular(normal, eyeVec, lightDir, normalInput.a * 255.0);
  94. oColor = diff * float4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  95. #else
  96. oColor = diff * float4(lightColor * albedoInput.rgb, 0.0);
  97. #endif
  98. }