Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GeometryUtilities.hlsl 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef GEO_UTILITIES_H
  2. #define GEO_UTILITIES_H
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
  5. struct CylinderBound
  6. {
  7. float3 center;
  8. float3 axis;
  9. float halfHeight;
  10. float capRadius;
  11. };
  12. struct SphereBound
  13. {
  14. float3 center;
  15. float radius;
  16. };
  17. float CompleteSinCos(float sinOrCos)
  18. {
  19. return sqrt(max(1.0f - sinOrCos*sinOrCos, 0.0f));
  20. }
  21. float2 ProjectedHalfLengths(CylinderBound cylinder, float3 planeNormal)
  22. {
  23. float absCosTheta = abs(dot(planeNormal, cylinder.axis));
  24. float sinTheta = CompleteSinCos(absCosTheta);
  25. float h = cylinder.halfHeight;
  26. float r = cylinder.capRadius;
  27. float halfLengthAlongNormal = absCosTheta * h + sinTheta * r;
  28. float halfLengthInPlane = max(sinTheta * h + absCosTheta * r, r); // ellipse, so use max of two axis lengths
  29. return float2(halfLengthInPlane, halfLengthAlongNormal);
  30. }
  31. struct BoundingObjectData
  32. {
  33. float3 frontCenterPosRWS;
  34. float2 centerPosNDC;
  35. float2 radialPosNDC;
  36. };
  37. BoundingObjectData CalculateBoundingObjectData(SphereBound boundingSphere,
  38. float4x4 viewProjMatrix,
  39. float4 viewOriginWorldSpace,
  40. float4 radialDirWorldSpace,
  41. float4 facingDirWorldSpace)
  42. {
  43. const float3 centerPosRWS = boundingSphere.center - viewOriginWorldSpace.xyz;
  44. const float3 radialVec = abs(boundingSphere.radius) * radialDirWorldSpace.xyz;
  45. const float3 facingVec = abs(boundingSphere.radius) * facingDirWorldSpace.xyz;
  46. BoundingObjectData data;
  47. data.centerPosNDC = ComputeNormalizedDeviceCoordinates(centerPosRWS, viewProjMatrix);
  48. data.radialPosNDC = ComputeNormalizedDeviceCoordinates(centerPosRWS + radialVec, viewProjMatrix);
  49. data.frontCenterPosRWS = centerPosRWS + facingVec;
  50. return data;
  51. }
  52. BoundingObjectData CalculateBoundingObjectData(CylinderBound cylinderBound,
  53. float4x4 viewProjMatrix,
  54. float4 viewOriginWorldSpace,
  55. float4 radialDirWorldSpace,
  56. float4 facingDirWorldSpace)
  57. {
  58. const float3 centerPosRWS = cylinderBound.center - viewOriginWorldSpace.xyz;
  59. const float2 halfLengths = ProjectedHalfLengths(cylinderBound, facingDirWorldSpace.xyz);
  60. const float3 radialVec = halfLengths.x * radialDirWorldSpace.xyz;
  61. BoundingObjectData data;
  62. data.centerPosNDC = ComputeNormalizedDeviceCoordinates(centerPosRWS, viewProjMatrix);
  63. data.radialPosNDC = ComputeNormalizedDeviceCoordinates(centerPosRWS + radialVec, viewProjMatrix);
  64. data.frontCenterPosRWS = centerPosRWS + halfLengths.y * facingDirWorldSpace.xyz;
  65. return data;
  66. }
  67. #endif