Primitives.cginc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef VOLUMETRIC_FOG_2_PRIMITIVES
  2. #define VOLUMETRIC_FOG_2_PRIMITIVES
  3. float3 _BoundsCenter, _BoundsExtents;
  4. float4x4 _InvRotMatrix;
  5. float4x4 _RotMatrix;
  6. float BoxIntersection(float3 origin, float3 viewDir) {
  7. float3 ro = origin - _BoundsCenter;
  8. float3 invR = 1.0.xxx / viewDir;
  9. float3 tbot = invR * (-_BoundsExtents - ro);
  10. float3 ttop = invR * (_BoundsExtents - ro);
  11. float3 tmin = min (ttop, tbot);
  12. float2 tt0 = max (tmin.xx, tmin.yz);
  13. float t0 = max(tt0.x, tt0.y);
  14. return max(t0, 0);
  15. }
  16. void SphereIntersection(float3 origin, float3 viewDir, out float t0, out float t1) {
  17. float3 oc = origin - _BoundsCenter;
  18. float b = dot(viewDir, oc);
  19. float c = dot(oc,oc) - _BoundsExtents.x;
  20. float t = b*b - c;
  21. if (t>0) t = sqrt(t);
  22. t0 = -b-t;
  23. t1 = -b+t;
  24. if (t1>t0) {
  25. t0 = max(t0, 0);
  26. }
  27. }
  28. inline float3 RotateInv(float3 wpos) {
  29. wpos -= _BoundsCenter.xyz;
  30. wpos = mul((float3x3)_InvRotMatrix, wpos);
  31. wpos += _BoundsCenter.xyz;
  32. return wpos;
  33. }
  34. inline float3 Rotate(float3 wpos) {
  35. wpos -= _BoundsCenter.xyz;
  36. wpos = mul((float3x3)_RotMatrix, wpos);
  37. wpos += _BoundsCenter.xyz;
  38. return wpos;
  39. }
  40. #endif // VOLUMETRIC_FOG_2_PRIMITIVES