ImageBasedLighting.bslinc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  2. Technique
  3. : base("ImageBasedLighting")
  4. : inherits("ReflectionCubemapCommon") =
  5. {
  6. Pass =
  7. {
  8. Common =
  9. {
  10. // Arbitrary limit, increase if needed
  11. #define MAX_PROBES 512
  12. struct ReflProbeData
  13. {
  14. float3 position;
  15. float radius;
  16. float3 boxExtents;
  17. float4x4 invBoxTransform;
  18. float transitionDistance;
  19. uint cubemapIdx;
  20. uint type; // 0 - Sphere, 1 - Box
  21. };
  22. TextureCube gSkyReflectionTex;
  23. SamplerState gSkyReflectionSamp;
  24. TextureCube gSkyIrradianceTex;
  25. SamplerState gSkyIrradianceSamp;
  26. TextureCubeArray gReflProbeCubemaps;
  27. SamplerState gReflProbeSamp;
  28. Texture2D gPreintegratedEnvBRDF;
  29. SamplerState gPreintegratedEnvBRDFSamp;
  30. StructuredBuffer<ReflProbeData> gReflectionProbes;
  31. #ifdef USE_COMPUTE_INDICES
  32. groupshared uint gReflectionProbeIndices[MAX_PROBES];
  33. #endif
  34. #ifdef USE_LIGHT_GRID_INDICES
  35. Buffer<uint> gReflectionProbeIndices;
  36. #endif
  37. cbuffer ReflProbeParams
  38. {
  39. uint gReflCubemapNumMips;
  40. uint gNumProbes;
  41. uint gSkyCubemapAvailable;
  42. uint gUseReflectionMaps;
  43. uint gSkyCubemapNumMips;
  44. float gSkyBrightness;
  45. }
  46. float3 getSkyIndirectDiffuse(float3 dir)
  47. {
  48. return gSkyIrradianceTex.SampleLevel(gSkyIrradianceSamp, dir, 0).rgb * gSkyBrightness;
  49. }
  50. float getSphereReflectionContribution(float normalizedDistance)
  51. {
  52. // If closer than 60% to the probe radius, then full contribution is used.
  53. // For the other 40% we smoothstep and return contribution lower than 1 so other
  54. // reflection probes can be blended.
  55. // smoothstep from 1 to 0.6:
  56. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  57. // return t * t * (3.0 - 2.0 * t);
  58. float t = saturate(2.5 - 2.5 * normalizedDistance);
  59. return t * t * (3.0 - 2.0 * t);
  60. }
  61. float3 getLookupForSphereProxy(float3 originWS, float3 dirWS, float3 centerWS, float radius)
  62. {
  63. float radius2 = radius * radius;
  64. float3 originLS = originWS - centerWS;
  65. float a = dot(originLS, dirWS);
  66. float dist2 = a * a - dot(originLS, originLS) + radius2;
  67. float3 lookupDir = dirWS;
  68. [flatten]
  69. if(dist2 >= 0)
  70. {
  71. float farDist = sqrt(dist2) - a;
  72. lookupDir = originLS + farDist * dirWS;
  73. }
  74. return lookupDir;
  75. }
  76. float getDistBoxToPoint(float3 pt, float3 extents)
  77. {
  78. float3 d = max(max(-extents - pt, 0), pt - extents);
  79. return length(d);
  80. }
  81. float3 getLookupForBoxProxy(float3 originWS, float3 dirWS, float3 centerWS, float3 extents, float4x4 invBoxTransform, float transitionDistance, out float contribution)
  82. {
  83. // Transform origin and direction into box local space, where it is united sized and axis aligned
  84. float3 originLS = mul(invBoxTransform, float4(originWS, 1)).xyz;
  85. float3 dirLS = mul(invBoxTransform, float4(dirWS, 0)).xyz;
  86. // Get distance from 3 min planes and 3 max planes of the unit AABB
  87. // float3 unitVec = float3(1.0f, 1.0f, 1.0f);
  88. // float3 intersectsMax = (unitVec - originLS) / dirLS;
  89. // float3 intersectsMin = (-unitVec - originLS) / dirLS;
  90. float3 invDirLS = rcp(dirLS);
  91. float3 intersectsMax = invDirLS - originLS * invDirLS;
  92. float3 intersectsMin = -invDirLS - originLS * invDirLS;
  93. // Find nearest positive (along ray direction) intersection
  94. float3 positiveIntersections = max(intersectsMax, intersectsMin);
  95. float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  96. float3 intersectPositionWS = originWS + intersectDist * dirWS;
  97. float3 lookupDir = intersectPositionWS - centerWS;
  98. // Calculate contribution
  99. //// Shrink the box so fade out happens within box extents
  100. float3 reducedExtents = extents - float3(transitionDistance, transitionDistance, transitionDistance);
  101. float distToBox = getDistBoxToPoint(originLS * reducedExtents, reducedExtents);
  102. float normalizedDistance = distToBox / transitionDistance;
  103. // If closer than 70% to the probe radius, then full contribution is used.
  104. // For the other 30% we smoothstep and return contribution lower than 1 so other
  105. // reflection probes can be blended.
  106. // smoothstep from 1 to 0.7:
  107. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  108. // return t * t * (3.0 - 2.0 * t);
  109. float t = saturate(3.3333 - 3.3333 * normalizedDistance);
  110. contribution = t * t * (3.0 - 2.0 * t);
  111. return lookupDir;
  112. }
  113. float3 gatherReflectionRadiance(float3 worldPos, float3 dir, float roughness, float3 specularColor, uint probeOffset, uint numProbes)
  114. {
  115. if(gUseReflectionMaps == 0)
  116. return specularColor;
  117. float mipLevel = mapRoughnessToMipLevel(roughness, gReflCubemapNumMips);
  118. float3 output = 0;
  119. float leftoverContribution = 1.0f;
  120. for(uint i = 0; i < numProbes; i++)
  121. {
  122. if(leftoverContribution < 0.001f)
  123. break;
  124. uint probeIdx = gReflectionProbeIndices[probeOffset + i];
  125. ReflProbeData probeData = gReflectionProbes[probeIdx];
  126. float3 probeToPos = worldPos - probeData.position;
  127. float distToProbe = length(probeToPos);
  128. float normalizedDist = saturate(distToProbe / probeData.radius);
  129. if(distToProbe <= probeData.radius)
  130. {
  131. float3 correctedDir;
  132. float contribution = 0;
  133. if(probeData.type == 0) // Sphere
  134. {
  135. correctedDir = getLookupForSphereProxy(worldPos, dir, probeData.position, probeData.radius);
  136. contribution = getSphereReflectionContribution(normalizedDist);
  137. }
  138. else if(probeData.type == 1) // Box
  139. {
  140. correctedDir = getLookupForBoxProxy(worldPos, dir, probeData.position, probeData.boxExtents, probeData.invBoxTransform, probeData.transitionDistance, contribution);
  141. }
  142. float4 sample = gReflProbeCubemaps.SampleLevel(gReflProbeSamp, float4(correctedDir, probeData.cubemapIdx), mipLevel);
  143. sample *= contribution;
  144. output += sample.rgb * leftoverContribution;
  145. leftoverContribution *= (1.0f - contribution);
  146. }
  147. }
  148. if(gSkyCubemapAvailable > 0)
  149. {
  150. float skyMipLevel = mapRoughnessToMipLevel(roughness, gSkyCubemapNumMips);
  151. float4 sample = gSkyReflectionTex.SampleLevel(gSkyReflectionSamp, dir, skyMipLevel) * gSkyBrightness;
  152. output += sample.rgb * leftoverContribution;
  153. }
  154. return output;
  155. }
  156. float3 getImageBasedSpecular(float3 worldPos, float3 V, float3 R, SurfaceData surfaceData, uint probeOffset, uint numProbes)
  157. {
  158. // See C++ code for generation of gPreintegratedEnvBRDF to see why this code works as is
  159. float3 N = surfaceData.worldNormal.xyz;
  160. float NoV = saturate(dot(N, V));
  161. // Note: Using a fixed F0 value of 0.04 (plastic) for dielectrics, and using albedo as specular for conductors.
  162. // For more customizability allow the user to provide separate albedo/specular colors for both types.
  163. float3 specularColor = lerp(float3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, surfaceData.metalness);
  164. float3 radiance = gatherReflectionRadiance(worldPos, R, surfaceData.roughness, specularColor, probeOffset, numProbes);
  165. float2 envBRDF = gPreintegratedEnvBRDF.SampleLevel(gPreintegratedEnvBRDFSamp, float2(NoV, surfaceData.roughness), 0).rg;
  166. return radiance * (specularColor * envBRDF.x + envBRDF.y);
  167. }
  168. };
  169. };
  170. };