DeferredLight.hlsl 3.2 KB

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