ShaderVec3.h 947 B

12345678910111213141516171819202122232425262728
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. inline float3 JPH_Vec3DecompressUnit(uint inValue)
  5. {
  6. const float cOneOverSqrt2 = 0.70710678f;
  7. const uint cNumBits = 14;
  8. const uint cMask = (1u << cNumBits) - 1;
  9. const uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
  10. const float cScale = 2.0f * cOneOverSqrt2 / float(cMaxValue);
  11. // Restore two components
  12. float2 v2 = float2(float(inValue & cMask), float((inValue >> cNumBits) & cMask)) * cScale - float2(cOneOverSqrt2, cOneOverSqrt2);
  13. // Restore the highest component
  14. float3 v = float3(v2, sqrt(max(1.0f - dot(v2, v2), 0.0f)));
  15. // Extract sign
  16. if ((inValue & 0x80000000u) != 0)
  17. v = -v;
  18. // Swizzle the components in place
  19. uint max_element = (inValue >> 29) & 3;
  20. v = max_element == 0? v.zxy : (max_element == 1? v.xzy : v);
  21. return v;
  22. }