ReflectionCubemapSampling.bslinc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Technique : base("ReflectionCubemapSampling") =
  2. {
  3. Language = "HLSL11";
  4. Pass =
  5. {
  6. Common =
  7. {
  8. TextureCube gSkyCubemapTex;
  9. SamplerState gSkyCubemapSamp;
  10. cbuffer ReflectionParams
  11. {
  12. int gSkyNumMips;
  13. }
  14. float3 getSkyReflection(float3 dir, float roughness)
  15. {
  16. float mipLevel = mapRoughnessToMipLevel(roughness, gSkyNumMips);
  17. float3 reflection = gSkyCubemapTex.SampleLevel(gSkyCubemapSamp, dir, mipLevel);
  18. return reflection;
  19. }
  20. float3 getSphereReflectionContribution(float normalizedDistance)
  21. {
  22. // If closer than 60% to the probe radius, then full contribution is used.
  23. // For the other 40% we smoothstep and return contribution lower than 1 so other
  24. // reflection probes can be blended.
  25. // smoothstep from 1 to 0.6:
  26. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  27. // return t * t * (3.0 - 2.0 * t);
  28. float t = saturate(2.5 - 2.5 * normalizedDistance);
  29. return t * t * (3.0 - 2.0 * t);
  30. }
  31. float3 getLookupForSphereProxy(float3 originWS, float3 dirWS, float3 centerWS, float radius)
  32. {
  33. float radius2 = radius * radius;
  34. float3 originLS = originWS - centerWS;
  35. float a = dot(originLS, dirWS);
  36. float dist2 = a * a - dot(originLS, originLS) + radius2;
  37. float3 lookupDir = dirWS;
  38. [flatten]
  39. if(dist2 >= 0)
  40. {
  41. float farDist = sqrt(dist2) - a;
  42. lookupDir = originLS + farDist * dirWS;
  43. }
  44. return lookupDir;
  45. }
  46. float3 getDistBoxToPoint(float3 point, float3 extents)
  47. {
  48. float3 d = max(max(-extents - point, 0), point - extents);
  49. return length(d);
  50. }
  51. float3 getLookupForBoxProxy(float3 originWS, float3 dirWS, float3 centerWS, float3 extents, float4x4 invBoxTransform, float transitionDistance, out float contribution)
  52. {
  53. // Transform origin and direction into box local space, where it is united sized and axis aligned
  54. float3 originLS = mul(invBoxTransform, float4(originWS, 1)).xyz;
  55. float3 dirLS = mul(invBoxTransform, float4(dirWS, 0)).xyz;
  56. // Get distance from 3 min planes and 3 max planes of the unit AABB
  57. // float3 unitVec = float3(1.0f, 1.0f, 1.0f);
  58. // float3 intersectsMax = (unitVec - originLS) / dirLS;
  59. // float3 intersectsMin = (-unitVec - originLS) / dirLS;
  60. float3 invDirLS = rcp(dirLS);
  61. float3 intersectsMax = invDirLS - originLS * invDirLS;
  62. float3 intersectsMin = -invDirLS - originLS * invDirLS;
  63. // Find nearest positive (along ray direction) intersection
  64. float3 positiveIntersections = max(intersectsMax, intersectsMin);
  65. float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  66. float3 intersectPositionWS = originWS + intersectDist * dirWS;
  67. float3 lookupDir = intersectPositionWS - centerWS;
  68. // Calculate contribution
  69. //// Shrink the box so fade out happens within box extents
  70. float3 reducedExtents = extents - float3(transitionDistance, transitionDistance, transitionDistance);
  71. float distToBox = getDistBoxToPoint(originLS * reducedExtents, reducedExtents);
  72. float normalizedDistance = distToBox / transitionDistance;
  73. // If closer than 70% to the probe radius, then full contribution is used.
  74. // For the other 30% we smoothstep and return contribution lower than 1 so other
  75. // reflection probes can be blended.
  76. // smoothstep from 1 to 0.7:
  77. // float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  78. // return t * t * (3.0 - 2.0 * t);
  79. float t = saturate(3.3333 - 3.3333 * normalizedDistance);
  80. return t * t * (3.0 - 2.0 * t);
  81. return lookupDir;
  82. }
  83. };
  84. };
  85. };
  86. Technique : base("ReflectionCubemapSampling") =
  87. {
  88. Language = "GLSL";
  89. Pass =
  90. {
  91. Common =
  92. {
  93. };
  94. };
  95. };