ReflectionCubemapSampling.bslinc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. Technique : base("ReflectionCubemapSampling") =
  2. {
  3. Language = "HLSL11";
  4. Pass =
  5. {
  6. Common =
  7. {
  8. struct ReflProbeData
  9. {
  10. float3 position;
  11. float radius;
  12. float3 boxExtents;
  13. float4x4 invBoxTransform;
  14. float transitionDistance;
  15. uint cubemapIdx;
  16. uint type; // 0 - Sphere, 1 - Box
  17. };
  18. TextureCube gSkyCubemapTex;
  19. SamplerState gSkyCubemapSamp;
  20. TextureCubeArray gReflProbeCubmaps;
  21. SamplerState gReflProbeSamp;
  22. StructuredBuffer<ReflProbeData> gReflectionProbes;
  23. cbuffer ReflProbeParams
  24. {
  25. uint gReflCubemapNumMips;
  26. uint gNumProbes;
  27. uint gSkyCubemapAvailable;
  28. uint gSkyCubemapNumMips;
  29. }
  30. float3 getSkyReflection(float3 dir, float roughness)
  31. {
  32. float mipLevel = mapRoughnessToMipLevel(roughness, gReflCubemapNumMips);
  33. float3 reflection = gSkyCubemapTex.SampleLevel(gSkyCubemapSamp, dir, mipLevel);
  34. return reflection;
  35. }
  36. float3 getSphereReflectionContribution(float normalizedDistance)
  37. {
  38. // If closer than 60% to the probe radius, then full contribution is used.
  39. // For the other 40% we smoothstep and return contribution lower than 1 so other
  40. // reflection probes can be blended.
  41. // smoothstep from 1 to 0.6:
  42. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  43. // return t * t * (3.0 - 2.0 * t);
  44. float t = saturate(2.5 - 2.5 * normalizedDistance);
  45. return t * t * (3.0 - 2.0 * t);
  46. }
  47. float3 getLookupForSphereProxy(float3 originWS, float3 dirWS, float3 centerWS, float radius)
  48. {
  49. float radius2 = radius * radius;
  50. float3 originLS = originWS - centerWS;
  51. float a = dot(originLS, dirWS);
  52. float dist2 = a * a - dot(originLS, originLS) + radius2;
  53. float3 lookupDir = dirWS;
  54. [flatten]
  55. if(dist2 >= 0)
  56. {
  57. float farDist = sqrt(dist2) - a;
  58. lookupDir = originLS + farDist * dirWS;
  59. }
  60. return lookupDir;
  61. }
  62. float3 getDistBoxToPoint(float3 point, float3 extents)
  63. {
  64. float3 d = max(max(-extents - point, 0), point - extents);
  65. return length(d);
  66. }
  67. float3 getLookupForBoxProxy(float3 originWS, float3 dirWS, float3 centerWS, float3 extents, float4x4 invBoxTransform, float transitionDistance, out float contribution)
  68. {
  69. // Transform origin and direction into box local space, where it is united sized and axis aligned
  70. float3 originLS = mul(invBoxTransform, float4(originWS, 1)).xyz;
  71. float3 dirLS = mul(invBoxTransform, float4(dirWS, 0)).xyz;
  72. // Get distance from 3 min planes and 3 max planes of the unit AABB
  73. // float3 unitVec = float3(1.0f, 1.0f, 1.0f);
  74. // float3 intersectsMax = (unitVec - originLS) / dirLS;
  75. // float3 intersectsMin = (-unitVec - originLS) / dirLS;
  76. float3 invDirLS = rcp(dirLS);
  77. float3 intersectsMax = invDirLS - originLS * invDirLS;
  78. float3 intersectsMin = -invDirLS - originLS * invDirLS;
  79. // Find nearest positive (along ray direction) intersection
  80. float3 positiveIntersections = max(intersectsMax, intersectsMin);
  81. float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  82. float3 intersectPositionWS = originWS + intersectDist * dirWS;
  83. float3 lookupDir = intersectPositionWS - centerWS;
  84. // Calculate contribution
  85. //// Shrink the box so fade out happens within box extents
  86. float3 reducedExtents = extents - float3(transitionDistance, transitionDistance, transitionDistance);
  87. float distToBox = getDistBoxToPoint(originLS * reducedExtents, reducedExtents);
  88. float normalizedDistance = distToBox / transitionDistance;
  89. // If closer than 70% to the probe radius, then full contribution is used.
  90. // For the other 30% we smoothstep and return contribution lower than 1 so other
  91. // reflection probes can be blended.
  92. // smoothstep from 1 to 0.7:
  93. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  94. // return t * t * (3.0 - 2.0 * t);
  95. float t = saturate(3.3333 - 3.3333 * normalizedDistance);
  96. return t * t * (3.0 - 2.0 * t);
  97. return lookupDir;
  98. }
  99. float3 gatherReflectionRadiance(float3 worldPos, float3 dir, float roughness, uint probeOffset, uint numProbes)
  100. {
  101. #if FIXED_REFLECTION_COLOR
  102. return float3(1.0f, 1.0f, 1.0f);
  103. #else
  104. float mipLevel = mapRoughnessToMipLevel(roughness, gReflCubemapNumMips);
  105. float3 output = 0;
  106. float leftoverContribution = 1.0f;
  107. for(uint i = 0; i < numProbes; i++)
  108. {
  109. if(leftoverContribution < 0.001f)
  110. break;
  111. uint probeIdx = gReflectionProbeIndices[probeOffset + i];
  112. ReflProbeData probeData = gReflectionProbes[probeIdx];
  113. float3 probeToPos = worldPos - probeData.position;
  114. float distToProbe = length(probeToPos);
  115. float normalizedDist = saturate(distToProbe / probeData.radius);
  116. if(distToProbe <= probeData.radius)
  117. {
  118. float3 correctedDir;
  119. float contribution = 0;
  120. if(probeData.type == 0) // Sphere
  121. {
  122. correctedDir = getLookupForSphereProxy(worldPos, dir, probeData.position, probeData.radius);
  123. contibution = getSphereReflectionContribution(normalizedDist);
  124. }
  125. else if(probeData.type == 1) // Box
  126. {
  127. correctedDir = getLookupForBoxProxy(worldPos, dir, probeData.position, probeData.boxExtents, probeData.invBoxTransform, probeData.transitionDistance, contribution);
  128. }
  129. float4 sample = gReflProbeCubmaps.SampleLevel(gReflProbeSamp, float4(correctedDir, probeData.cubemapIdx), mipLevel);
  130. sample *= contribution;
  131. output += sample * leftoverContribution;
  132. leftoverContribution *= (1.0f - contribution);
  133. }
  134. }
  135. if(gSkyCubemapAvailable > 0)
  136. {
  137. float skyMipLevel = mapRoughnessToMipLevel(roughness, gSkyCubemapNumMips);
  138. float4 sample = gSkyCubemapTex.SampleLevel(gSkyCubemapSamp, dir, skyMipLevel);
  139. output += sample * leftoverContribution;
  140. }
  141. return output;
  142. #endif
  143. }
  144. };
  145. };
  146. };
  147. Technique : base("ReflectionCubemapSampling") =
  148. {
  149. Language = "GLSL";
  150. Pass =
  151. {
  152. Common =
  153. {
  154. };
  155. };
  156. };