LightVolume.hlsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef FALLBACK
  49. #ifdef DIRLIGHT
  50. #ifdef ORTHO
  51. float depth = Sample(sDepthBuffer, iScreenPos).r;
  52. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  53. #else
  54. #ifdef LINEAR
  55. float depth = Sample(sDepthBuffer, iScreenPos).r;
  56. #else
  57. float depth = ReconstructDepth(Sample(sDepthBuffer, iScreenPos).r);
  58. #endif
  59. float3 worldPos = iFarRay * depth;
  60. #endif
  61. float4 normalInput = Sample(sNormalBuffer, iScreenPos);
  62. #else
  63. #ifdef ORTHO
  64. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  65. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  66. #else
  67. #ifdef LINEAR
  68. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  69. #else
  70. float depth = ReconstructDepth(tex2Dproj(sDepthBuffer, iScreenPos).r);
  71. #endif
  72. float3 worldPos = iFarRay * depth / iScreenPos.w;
  73. #endif
  74. float4 normalInput = tex2Dproj(sNormalBuffer, iScreenPos);
  75. #endif
  76. // With specular, normalization greatly improves stability of reflections,
  77. // considering input is only 8 bits per axis
  78. #ifdef SPECULAR
  79. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  80. #else
  81. float3 normal = normalInput.rgb * 2.0 - 1.0;
  82. #endif
  83. #else
  84. float3 normal;
  85. float depth;
  86. #ifdef DIRLIGHT
  87. UnpackNormalDepth(Sample(sNormalBuffer, iScreenPos), normal, depth);
  88. #ifdef ORTHO
  89. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  90. #else
  91. float3 worldPos = iFarRay * depth;
  92. #endif
  93. #else
  94. UnpackNormalDepth(tex2Dproj(sNormalBuffer, iScreenPos), normal, depth);
  95. #ifdef ORTHO
  96. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  97. #else
  98. float3 worldPos = iFarRay * depth / iScreenPos.w;
  99. #endif
  100. #endif
  101. #endif
  102. float3 lightColor;
  103. float3 lightDir;
  104. float diff;
  105. // Accumulate light at half intensity to allow 2x "overburn"
  106. #ifdef DIRLIGHT
  107. lightDir = cLightDirPS;
  108. diff = 0.5 * GetDiffuseDir(normal, lightDir);
  109. #else
  110. float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  111. diff = 0.5 * GetDiffusePointOrSpot(normal, lightVec, lightDir);
  112. #endif
  113. #ifdef SHADOW
  114. #if defined(DIRLIGHT)
  115. float4x4 shadowMatrix = GetDirShadowMatrix(depth);
  116. float4 shadowPos = mul(float4(worldPos, 1.0), shadowMatrix);
  117. diff *= saturate(GetShadow(shadowPos) + GetShadowFade(depth));
  118. #elif defined(SPOTLIGHT)
  119. float4 shadowPos = mul(float4(worldPos, 1.0), cShadowProjPS[1]);
  120. diff *= GetShadow(shadowPos);
  121. #else
  122. float3 shadowPos = worldPos - cLightPosPS.xyz;
  123. diff *= GetCubeShadow(shadowPos);
  124. #endif
  125. #endif
  126. #ifdef SPOTLIGHT
  127. float4 spotPos = mul(float4(worldPos, 1.0), cShadowProjPS[0]);
  128. lightColor = spotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  129. #else
  130. #ifdef CUBEMASK
  131. lightColor = texCUBE(sLightCubeMap, mul(lightVec, (float3x3)cShadowProjPS[0])).rgb * cLightColor.rgb;
  132. #else
  133. lightColor = cLightColor.rgb;
  134. #endif
  135. #endif
  136. #ifdef SPECULAR
  137. float spec = lightColor.g * GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  138. oColor = diff * float4(lightColor, spec * cLightColor.a);
  139. #else
  140. oColor = diff * float4(lightColor, 0.0);
  141. #endif
  142. }