説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SpaceFillingCurves.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Unity.Mathematics;
  2. namespace UnityEngine.Rendering.Universal
  3. {
  4. static class SpaceFillingCurves
  5. {
  6. // "Insert" a 0 bit after each of the 16 low bits of x.
  7. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  8. static uint Part1By1(uint x)
  9. {
  10. x &= 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
  11. x = (x ^ (x << 8)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
  12. x = (x ^ (x << 4)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
  13. x = (x ^ (x << 2)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
  14. x = (x ^ (x << 1)) & 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
  15. return x;
  16. }
  17. // Inverse of Part1By1 - "delete" all odd-indexed bits.
  18. // Ref: https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  19. static uint Compact1By1(uint x)
  20. {
  21. x &= 0x55555555; // x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
  22. x = (x ^ (x >> 1)) & 0x33333333; // x = --fe --dc --ba --98 --76 --54 --32 --10
  23. x = (x ^ (x >> 2)) & 0x0f0f0f0f; // x = ---- fedc ---- ba98 ---- 7654 ---- 3210
  24. x = (x ^ (x >> 4)) & 0x00ff00ff; // x = ---- ---- fedc ba98 ---- ---- 7654 3210
  25. x = (x ^ (x >> 8)) & 0x0000ffff; // x = ---- ---- ---- ---- fedc ba98 7654 3210
  26. return x;
  27. }
  28. public static uint EncodeMorton2D(uint2 coord)
  29. {
  30. return (Part1By1(coord.y) << 1) + Part1By1(coord.x);
  31. }
  32. public static uint2 DecodeMorton2D(uint code)
  33. {
  34. return math.uint2(Compact1By1(code >> 0), Compact1By1(code >> 1));
  35. }
  36. }
  37. }