Compression.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Quaternion compression from DOTSNET
  2. using System;
  3. using UnityEngine;
  4. namespace Mirror
  5. {
  6. /// <summary>Functions to Compress Quaternions and Floats</summary>
  7. public static class Compression
  8. {
  9. // quaternion compression //////////////////////////////////////////////
  10. // smallest three: https://gafferongames.com/post/snapshot_compression/
  11. // compresses 16 bytes quaternion into 4 bytes
  12. // helper function to find largest absolute element
  13. // returns the index of the largest one
  14. public static int LargestAbsoluteComponentIndex(Vector4 value, out float largestAbs, out Vector3 withoutLargest)
  15. {
  16. // convert to abs
  17. Vector4 abs = new Vector4(Mathf.Abs(value.x), Mathf.Abs(value.y), Mathf.Abs(value.z), Mathf.Abs(value.w));
  18. // set largest to first abs (x)
  19. largestAbs = abs.x;
  20. withoutLargest = new Vector3(value.y, value.z, value.w);
  21. int largestIndex = 0;
  22. // compare to the others, starting at second value
  23. // performance for 100k calls
  24. // for-loop: 25ms
  25. // manual checks: 22ms
  26. if (abs.y > largestAbs)
  27. {
  28. largestIndex = 1;
  29. largestAbs = abs.y;
  30. withoutLargest = new Vector3(value.x, value.z, value.w);
  31. }
  32. if (abs.z > largestAbs)
  33. {
  34. largestIndex = 2;
  35. largestAbs = abs.z;
  36. withoutLargest = new Vector3(value.x, value.y, value.w);
  37. }
  38. if (abs.w > largestAbs)
  39. {
  40. largestIndex = 3;
  41. largestAbs = abs.w;
  42. withoutLargest = new Vector3(value.x, value.y, value.z);
  43. }
  44. return largestIndex;
  45. }
  46. // scale a float within min/max range to an ushort between min/max range
  47. // note: can also use this for byte range from byte.MinValue to byte.MaxValue
  48. public static ushort ScaleFloatToUShort(float value, float minValue, float maxValue, ushort minTarget, ushort maxTarget)
  49. {
  50. // note: C# ushort - ushort => int, hence so many casts
  51. // max ushort - min ushort only fits into something bigger
  52. int targetRange = maxTarget - minTarget;
  53. float valueRange = maxValue - minValue;
  54. float valueRelative = value - minValue;
  55. return (ushort)(minTarget + (ushort)(valueRelative / valueRange * targetRange));
  56. }
  57. // scale an ushort within min/max range to a float between min/max range
  58. // note: can also use this for byte range from byte.MinValue to byte.MaxValue
  59. public static float ScaleUShortToFloat(ushort value, ushort minValue, ushort maxValue, float minTarget, float maxTarget)
  60. {
  61. // note: C# ushort - ushort => int, hence so many casts
  62. float targetRange = maxTarget - minTarget;
  63. ushort valueRange = (ushort)(maxValue - minValue);
  64. ushort valueRelative = (ushort)(value - minValue);
  65. return minTarget + (valueRelative / (float)valueRange * targetRange);
  66. }
  67. const float QuaternionMinRange = -0.707107f;
  68. const float QuaternionMaxRange = 0.707107f;
  69. const ushort TenBitsMax = 0x3FF;
  70. // helper function to access 'nth' component of quaternion
  71. static float QuaternionElement(Quaternion q, int element)
  72. {
  73. switch (element)
  74. {
  75. case 0: return q.x;
  76. case 1: return q.y;
  77. case 2: return q.z;
  78. case 3: return q.w;
  79. default: return 0;
  80. }
  81. }
  82. // note: assumes normalized quaternions
  83. public static uint CompressQuaternion(Quaternion q)
  84. {
  85. // note: assuming normalized quaternions is enough. no need to force
  86. // normalize here. we already normalize when decompressing.
  87. // find the largest component index [0,3] + value
  88. int largestIndex = LargestAbsoluteComponentIndex(new Vector4(q.x, q.y, q.z, q.w), out float _, out Vector3 withoutLargest);
  89. // from here on, we work with the 3 components without largest!
  90. // "You might think you need to send a sign bit for [largest] in
  91. // case it is negative, but you don’t, because you can make
  92. // [largest] always positive by negating the entire quaternion if
  93. // [largest] is negative. in quaternion space (x,y,z,w) and
  94. // (-x,-y,-z,-w) represent the same rotation."
  95. if (QuaternionElement(q, largestIndex) < 0)
  96. withoutLargest = -withoutLargest;
  97. // put index & three floats into one integer.
  98. // => index is 2 bits (4 values require 2 bits to store them)
  99. // => the three floats are between [-0.707107,+0.707107] because:
  100. // "If v is the absolute value of the largest quaternion
  101. // component, the next largest possible component value occurs
  102. // when two components have the same absolute value and the
  103. // other two components are zero. The length of that quaternion
  104. // (v,v,0,0) is 1, therefore v^2 + v^2 = 1, 2v^2 = 1,
  105. // v = 1/sqrt(2). This means you can encode the smallest three
  106. // components in [-0.707107,+0.707107] instead of [-1,+1] giving
  107. // you more precision with the same number of bits."
  108. // => the article recommends storing each float in 9 bits
  109. // => our uint has 32 bits, so we might as well store in (32-2)/3=10
  110. // 10 bits max value: 1023=0x3FF (use OSX calc to flip 10 bits)
  111. ushort aScaled = ScaleFloatToUShort(withoutLargest.x, QuaternionMinRange, QuaternionMaxRange, 0, TenBitsMax);
  112. ushort bScaled = ScaleFloatToUShort(withoutLargest.y, QuaternionMinRange, QuaternionMaxRange, 0, TenBitsMax);
  113. ushort cScaled = ScaleFloatToUShort(withoutLargest.z, QuaternionMinRange, QuaternionMaxRange, 0, TenBitsMax);
  114. // now we just need to pack them into one integer
  115. // -> index is 2 bit and needs to be shifted to 31..32
  116. // -> a is 10 bit and needs to be shifted 20..30
  117. // -> b is 10 bit and needs to be shifted 10..20
  118. // -> c is 10 bit and needs to be at 0..10
  119. return (uint)(largestIndex << 30 | aScaled << 20 | bScaled << 10 | cScaled);
  120. }
  121. // Quaternion normalizeSAFE from ECS math.normalizesafe()
  122. // => useful to produce valid quaternions even if client sends invalid
  123. // data
  124. static Quaternion QuaternionNormalizeSafe(Quaternion value)
  125. {
  126. // The smallest positive normal number representable in a float.
  127. const float FLT_MIN_NORMAL = 1.175494351e-38F;
  128. Vector4 v = new Vector4(value.x, value.y, value.z, value.w);
  129. float length = Vector4.Dot(v, v);
  130. return length > FLT_MIN_NORMAL
  131. ? value.normalized
  132. : Quaternion.identity;
  133. }
  134. // note: gives normalized quaternions
  135. public static Quaternion DecompressQuaternion(uint data)
  136. {
  137. // get cScaled which is at 0..10 and ignore the rest
  138. ushort cScaled = (ushort)(data & TenBitsMax);
  139. // get bScaled which is at 10..20 and ignore the rest
  140. ushort bScaled = (ushort)((data >> 10) & TenBitsMax);
  141. // get aScaled which is at 20..30 and ignore the rest
  142. ushort aScaled = (ushort)((data >> 20) & TenBitsMax);
  143. // get 2 bit largest index, which is at 31..32
  144. int largestIndex = (int)(data >> 30);
  145. // scale back to floats
  146. float a = ScaleUShortToFloat(aScaled, 0, TenBitsMax, QuaternionMinRange, QuaternionMaxRange);
  147. float b = ScaleUShortToFloat(bScaled, 0, TenBitsMax, QuaternionMinRange, QuaternionMaxRange);
  148. float c = ScaleUShortToFloat(cScaled, 0, TenBitsMax, QuaternionMinRange, QuaternionMaxRange);
  149. // calculate the omitted component based on a²+b²+c²+d²=1
  150. float d = Mathf.Sqrt(1 - a*a - b*b - c*c);
  151. // reconstruct based on largest index
  152. Vector4 value;
  153. switch (largestIndex)
  154. {
  155. case 0: value = new Vector4(d, a, b, c); break;
  156. case 1: value = new Vector4(a, d, b, c); break;
  157. case 2: value = new Vector4(a, b, d, c); break;
  158. default: value = new Vector4(a, b, c, d); break;
  159. }
  160. // ECS Rotation only works with normalized quaternions.
  161. // make sure that's always the case here to avoid ECS bugs where
  162. // everything stops moving if the quaternion isn't normalized.
  163. // => NormalizeSafe returns a normalized quaternion even if we pass
  164. // in NaN from deserializing invalid values!
  165. return QuaternionNormalizeSafe(new Quaternion(value.x, value.y, value.z, value.w));
  166. }
  167. // varint compression //////////////////////////////////////////////////
  168. // compress ulong varint.
  169. // same result for int, short and byte. only need one function.
  170. // NOT an extension. otherwise weaver might accidentally use it.
  171. public static void CompressVarUInt(NetworkWriter writer, ulong value)
  172. {
  173. if (value <= 240)
  174. {
  175. writer.Write((byte)value);
  176. return;
  177. }
  178. if (value <= 2287)
  179. {
  180. writer.Write((byte)(((value - 240) >> 8) + 241));
  181. writer.Write((byte)((value - 240) & 0xFF));
  182. return;
  183. }
  184. if (value <= 67823)
  185. {
  186. writer.Write((byte)249);
  187. writer.Write((byte)((value - 2288) >> 8));
  188. writer.Write((byte)((value - 2288) & 0xFF));
  189. return;
  190. }
  191. if (value <= 16777215)
  192. {
  193. writer.Write((byte)250);
  194. writer.Write((byte)(value & 0xFF));
  195. writer.Write((byte)((value >> 8) & 0xFF));
  196. writer.Write((byte)((value >> 16) & 0xFF));
  197. return;
  198. }
  199. if (value <= 4294967295)
  200. {
  201. writer.Write((byte)251);
  202. writer.Write((byte)(value & 0xFF));
  203. writer.Write((byte)((value >> 8) & 0xFF));
  204. writer.Write((byte)((value >> 16) & 0xFF));
  205. writer.Write((byte)((value >> 24) & 0xFF));
  206. return;
  207. }
  208. if (value <= 1099511627775)
  209. {
  210. writer.Write((byte)252);
  211. writer.Write((byte)(value & 0xFF));
  212. writer.Write((byte)((value >> 8) & 0xFF));
  213. writer.Write((byte)((value >> 16) & 0xFF));
  214. writer.Write((byte)((value >> 24) & 0xFF));
  215. writer.Write((byte)((value >> 32) & 0xFF));
  216. return;
  217. }
  218. if (value <= 281474976710655)
  219. {
  220. writer.Write((byte)253);
  221. writer.Write((byte)(value & 0xFF));
  222. writer.Write((byte)((value >> 8) & 0xFF));
  223. writer.Write((byte)((value >> 16) & 0xFF));
  224. writer.Write((byte)((value >> 24) & 0xFF));
  225. writer.Write((byte)((value >> 32) & 0xFF));
  226. writer.Write((byte)((value >> 40) & 0xFF));
  227. return;
  228. }
  229. if (value <= 72057594037927935)
  230. {
  231. writer.Write((byte)254);
  232. writer.Write((byte)(value & 0xFF));
  233. writer.Write((byte)((value >> 8) & 0xFF));
  234. writer.Write((byte)((value >> 16) & 0xFF));
  235. writer.Write((byte)((value >> 24) & 0xFF));
  236. writer.Write((byte)((value >> 32) & 0xFF));
  237. writer.Write((byte)((value >> 40) & 0xFF));
  238. writer.Write((byte)((value >> 48) & 0xFF));
  239. return;
  240. }
  241. // all others
  242. {
  243. writer.Write((byte)255);
  244. writer.Write((byte)(value & 0xFF));
  245. writer.Write((byte)((value >> 8) & 0xFF));
  246. writer.Write((byte)((value >> 16) & 0xFF));
  247. writer.Write((byte)((value >> 24) & 0xFF));
  248. writer.Write((byte)((value >> 32) & 0xFF));
  249. writer.Write((byte)((value >> 40) & 0xFF));
  250. writer.Write((byte)((value >> 48) & 0xFF));
  251. writer.Write((byte)((value >> 56) & 0xFF));
  252. }
  253. }
  254. // zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba
  255. public static void CompressVarInt(NetworkWriter writer, long i)
  256. {
  257. ulong zigzagged = (ulong)((i >> 63) ^ (i << 1));
  258. CompressVarUInt(writer, zigzagged);
  259. }
  260. // NOT an extension. otherwise weaver might accidentally use it.
  261. public static ulong DecompressVarUInt(NetworkReader reader)
  262. {
  263. byte a0 = reader.ReadByte();
  264. if (a0 < 241)
  265. {
  266. return a0;
  267. }
  268. byte a1 = reader.ReadByte();
  269. if (a0 >= 241 && a0 <= 248)
  270. {
  271. return 240 + ((a0 - (ulong)241) << 8) + a1;
  272. }
  273. byte a2 = reader.ReadByte();
  274. if (a0 == 249)
  275. {
  276. return 2288 + ((ulong)a1 << 8) + a2;
  277. }
  278. byte a3 = reader.ReadByte();
  279. if (a0 == 250)
  280. {
  281. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16);
  282. }
  283. byte a4 = reader.ReadByte();
  284. if (a0 == 251)
  285. {
  286. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24);
  287. }
  288. byte a5 = reader.ReadByte();
  289. if (a0 == 252)
  290. {
  291. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32);
  292. }
  293. byte a6 = reader.ReadByte();
  294. if (a0 == 253)
  295. {
  296. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40);
  297. }
  298. byte a7 = reader.ReadByte();
  299. if (a0 == 254)
  300. {
  301. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40) + (((ulong)a7) << 48);
  302. }
  303. byte a8 = reader.ReadByte();
  304. if (a0 == 255)
  305. {
  306. return a1 + (((ulong)a2) << 8) + (((ulong)a3) << 16) + (((ulong)a4) << 24) + (((ulong)a5) << 32) + (((ulong)a6) << 40) + (((ulong)a7) << 48) + (((ulong)a8) << 56);
  307. }
  308. throw new IndexOutOfRangeException("DecompressVarInt failure: " + a0);
  309. }
  310. // zigzag decoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba
  311. public static long DecompressVarInt(NetworkReader reader)
  312. {
  313. ulong data = DecompressVarUInt(reader);
  314. return ((long)(data >> 1)) ^ -((long)data & 1);
  315. }
  316. }
  317. }