AttributeCompression.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. import Cartesian2 from "./Cartesian2.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import ComponentDatatype from "./ComponentDatatype.js";
  4. import Check from "./Check.js";
  5. import defined from "./defined.js";
  6. import DeveloperError from "./DeveloperError.js";
  7. import CesiumMath from "./Math.js";
  8. import AttributeType from "../Scene/AttributeType.js";
  9. const RIGHT_SHIFT = 1.0 / 256.0;
  10. const LEFT_SHIFT = 256.0;
  11. /**
  12. * Attribute compression and decompression functions.
  13. *
  14. * @namespace AttributeCompression
  15. *
  16. * @private
  17. */
  18. const AttributeCompression = {};
  19. /**
  20. * Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
  21. *
  22. * Oct encoding is a compact representation of unit length vectors.
  23. * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
  24. * Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
  25. *
  26. * @param {Cartesian3} vector The normalized vector to be compressed into 2 component 'oct' encoding.
  27. * @param {Cartesian2} result The 2 component oct-encoded unit length vector.
  28. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  29. * @returns {Cartesian2} The 2 component oct-encoded unit length vector.
  30. *
  31. * @exception {DeveloperError} vector must be normalized.
  32. *
  33. * @see AttributeCompression.octDecodeInRange
  34. */
  35. AttributeCompression.octEncodeInRange = function (vector, rangeMax, result) {
  36. //>>includeStart('debug', pragmas.debug);
  37. Check.defined("vector", vector);
  38. Check.defined("result", result);
  39. const magSquared = Cartesian3.magnitudeSquared(vector);
  40. if (Math.abs(magSquared - 1.0) > CesiumMath.EPSILON6) {
  41. throw new DeveloperError("vector must be normalized.");
  42. }
  43. //>>includeEnd('debug');
  44. result.x =
  45. vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  46. result.y =
  47. vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  48. if (vector.z < 0) {
  49. const x = result.x;
  50. const y = result.y;
  51. result.x = (1.0 - Math.abs(y)) * CesiumMath.signNotZero(x);
  52. result.y = (1.0 - Math.abs(x)) * CesiumMath.signNotZero(y);
  53. }
  54. result.x = CesiumMath.toSNorm(result.x, rangeMax);
  55. result.y = CesiumMath.toSNorm(result.y, rangeMax);
  56. return result;
  57. };
  58. /**
  59. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
  60. *
  61. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  62. * @param {Cartesian2} result The 2 byte oct-encoded unit length vector.
  63. * @returns {Cartesian2} The 2 byte oct-encoded unit length vector.
  64. *
  65. * @exception {DeveloperError} vector must be normalized.
  66. *
  67. * @see AttributeCompression.octEncodeInRange
  68. * @see AttributeCompression.octDecode
  69. */
  70. AttributeCompression.octEncode = function (vector, result) {
  71. return AttributeCompression.octEncodeInRange(vector, 255, result);
  72. };
  73. const octEncodeScratch = new Cartesian2();
  74. const uint8ForceArray = new Uint8Array(1);
  75. function forceUint8(value) {
  76. uint8ForceArray[0] = value;
  77. return uint8ForceArray[0];
  78. }
  79. /**
  80. * @param {Cartesian3} vector The normalized vector to be compressed into 4 byte 'oct' encoding.
  81. * @param {Cartesian4} result The 4 byte oct-encoded unit length vector.
  82. * @returns {Cartesian4} The 4 byte oct-encoded unit length vector.
  83. *
  84. * @exception {DeveloperError} vector must be normalized.
  85. *
  86. * @see AttributeCompression.octEncodeInRange
  87. * @see AttributeCompression.octDecodeFromCartesian4
  88. */
  89. AttributeCompression.octEncodeToCartesian4 = function (vector, result) {
  90. AttributeCompression.octEncodeInRange(vector, 65535, octEncodeScratch);
  91. result.x = forceUint8(octEncodeScratch.x * RIGHT_SHIFT);
  92. result.y = forceUint8(octEncodeScratch.x);
  93. result.z = forceUint8(octEncodeScratch.y * RIGHT_SHIFT);
  94. result.w = forceUint8(octEncodeScratch.y);
  95. return result;
  96. };
  97. /**
  98. * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
  99. *
  100. * @param {Number} x The x component of the oct-encoded unit length vector.
  101. * @param {Number} y The y component of the oct-encoded unit length vector.
  102. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  103. * @param {Cartesian3} result The decoded and normalized vector
  104. * @returns {Cartesian3} The decoded and normalized vector.
  105. *
  106. * @exception {DeveloperError} x and y must be unsigned normalized integers between 0 and rangeMax.
  107. *
  108. * @see AttributeCompression.octEncodeInRange
  109. */
  110. AttributeCompression.octDecodeInRange = function (x, y, rangeMax, result) {
  111. //>>includeStart('debug', pragmas.debug);
  112. Check.defined("result", result);
  113. if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
  114. throw new DeveloperError(
  115. `x and y must be unsigned normalized integers between 0 and ${rangeMax}`
  116. );
  117. }
  118. //>>includeEnd('debug');
  119. result.x = CesiumMath.fromSNorm(x, rangeMax);
  120. result.y = CesiumMath.fromSNorm(y, rangeMax);
  121. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  122. if (result.z < 0.0) {
  123. const oldVX = result.x;
  124. result.x = (1.0 - Math.abs(result.y)) * CesiumMath.signNotZero(oldVX);
  125. result.y = (1.0 - Math.abs(oldVX)) * CesiumMath.signNotZero(result.y);
  126. }
  127. return Cartesian3.normalize(result, result);
  128. };
  129. /**
  130. * Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
  131. *
  132. * @param {Number} x The x component of the oct-encoded unit length vector.
  133. * @param {Number} y The y component of the oct-encoded unit length vector.
  134. * @param {Cartesian3} result The decoded and normalized vector.
  135. * @returns {Cartesian3} The decoded and normalized vector.
  136. *
  137. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and 255.
  138. *
  139. * @see AttributeCompression.octDecodeInRange
  140. */
  141. AttributeCompression.octDecode = function (x, y, result) {
  142. return AttributeCompression.octDecodeInRange(x, y, 255, result);
  143. };
  144. /**
  145. * Decodes a unit-length vector in 4 byte 'oct' encoding to a normalized 3-component vector.
  146. *
  147. * @param {Cartesian4} encoded The oct-encoded unit length vector.
  148. * @param {Cartesian3} result The decoded and normalized vector.
  149. * @returns {Cartesian3} The decoded and normalized vector.
  150. *
  151. * @exception {DeveloperError} x, y, z, and w must be unsigned normalized integers between 0 and 255.
  152. *
  153. * @see AttributeCompression.octDecodeInRange
  154. * @see AttributeCompression.octEncodeToCartesian4
  155. */
  156. AttributeCompression.octDecodeFromCartesian4 = function (encoded, result) {
  157. //>>includeStart('debug', pragmas.debug);
  158. Check.typeOf.object("encoded", encoded);
  159. Check.typeOf.object("result", result);
  160. //>>includeEnd('debug');
  161. const x = encoded.x;
  162. const y = encoded.y;
  163. const z = encoded.z;
  164. const w = encoded.w;
  165. //>>includeStart('debug', pragmas.debug);
  166. if (
  167. x < 0 ||
  168. x > 255 ||
  169. y < 0 ||
  170. y > 255 ||
  171. z < 0 ||
  172. z > 255 ||
  173. w < 0 ||
  174. w > 255
  175. ) {
  176. throw new DeveloperError(
  177. "x, y, z, and w must be unsigned normalized integers between 0 and 255"
  178. );
  179. }
  180. //>>includeEnd('debug');
  181. const xOct16 = x * LEFT_SHIFT + y;
  182. const yOct16 = z * LEFT_SHIFT + w;
  183. return AttributeCompression.octDecodeInRange(xOct16, yOct16, 65535, result);
  184. };
  185. /**
  186. * Packs an oct encoded vector into a single floating-point number.
  187. *
  188. * @param {Cartesian2} encoded The oct encoded vector.
  189. * @returns {Number} The oct encoded vector packed into a single float.
  190. *
  191. */
  192. AttributeCompression.octPackFloat = function (encoded) {
  193. //>>includeStart('debug', pragmas.debug);
  194. Check.defined("encoded", encoded);
  195. //>>includeEnd('debug');
  196. return 256.0 * encoded.x + encoded.y;
  197. };
  198. const scratchEncodeCart2 = new Cartesian2();
  199. /**
  200. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
  201. * stores those values in a single float-point number.
  202. *
  203. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  204. * @returns {Number} The 2 byte oct-encoded unit length vector.
  205. *
  206. * @exception {DeveloperError} vector must be normalized.
  207. */
  208. AttributeCompression.octEncodeFloat = function (vector) {
  209. AttributeCompression.octEncode(vector, scratchEncodeCart2);
  210. return AttributeCompression.octPackFloat(scratchEncodeCart2);
  211. };
  212. /**
  213. * Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
  214. *
  215. * @param {Number} value The oct-encoded unit length vector stored as a single floating-point number.
  216. * @param {Cartesian3} result The decoded and normalized vector
  217. * @returns {Cartesian3} The decoded and normalized vector.
  218. *
  219. */
  220. AttributeCompression.octDecodeFloat = function (value, result) {
  221. //>>includeStart('debug', pragmas.debug);
  222. Check.defined("value", value);
  223. //>>includeEnd('debug');
  224. const temp = value / 256.0;
  225. const x = Math.floor(temp);
  226. const y = (temp - x) * 256.0;
  227. return AttributeCompression.octDecode(x, y, result);
  228. };
  229. /**
  230. * Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
  231. * packs those into two floating-point numbers.
  232. *
  233. * @param {Cartesian3} v1 A normalized vector to be compressed.
  234. * @param {Cartesian3} v2 A normalized vector to be compressed.
  235. * @param {Cartesian3} v3 A normalized vector to be compressed.
  236. * @param {Cartesian2} result The 'oct' encoded vectors packed into two floating-point numbers.
  237. * @returns {Cartesian2} The 'oct' encoded vectors packed into two floating-point numbers.
  238. *
  239. */
  240. AttributeCompression.octPack = function (v1, v2, v3, result) {
  241. //>>includeStart('debug', pragmas.debug);
  242. Check.defined("v1", v1);
  243. Check.defined("v2", v2);
  244. Check.defined("v3", v3);
  245. Check.defined("result", result);
  246. //>>includeEnd('debug');
  247. const encoded1 = AttributeCompression.octEncodeFloat(v1);
  248. const encoded2 = AttributeCompression.octEncodeFloat(v2);
  249. const encoded3 = AttributeCompression.octEncode(v3, scratchEncodeCart2);
  250. result.x = 65536.0 * encoded3.x + encoded1;
  251. result.y = 65536.0 * encoded3.y + encoded2;
  252. return result;
  253. };
  254. /**
  255. * Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
  256. *
  257. * @param {Cartesian2} packed The three oct-encoded unit length vectors stored as two floating-point number.
  258. * @param {Cartesian3} v1 One decoded and normalized vector.
  259. * @param {Cartesian3} v2 One decoded and normalized vector.
  260. * @param {Cartesian3} v3 One decoded and normalized vector.
  261. */
  262. AttributeCompression.octUnpack = function (packed, v1, v2, v3) {
  263. //>>includeStart('debug', pragmas.debug);
  264. Check.defined("packed", packed);
  265. Check.defined("v1", v1);
  266. Check.defined("v2", v2);
  267. Check.defined("v3", v3);
  268. //>>includeEnd('debug');
  269. let temp = packed.x / 65536.0;
  270. const x = Math.floor(temp);
  271. const encodedFloat1 = (temp - x) * 65536.0;
  272. temp = packed.y / 65536.0;
  273. const y = Math.floor(temp);
  274. const encodedFloat2 = (temp - y) * 65536.0;
  275. AttributeCompression.octDecodeFloat(encodedFloat1, v1);
  276. AttributeCompression.octDecodeFloat(encodedFloat2, v2);
  277. AttributeCompression.octDecode(x, y, v3);
  278. };
  279. /**
  280. * Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
  281. *
  282. * @param {Cartesian2} textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
  283. * @returns {Number} The packed texture coordinates.
  284. *
  285. */
  286. AttributeCompression.compressTextureCoordinates = function (
  287. textureCoordinates
  288. ) {
  289. //>>includeStart('debug', pragmas.debug);
  290. Check.defined("textureCoordinates", textureCoordinates);
  291. //>>includeEnd('debug');
  292. // Move x and y to the range 0-4095;
  293. const x = (textureCoordinates.x * 4095.0) | 0;
  294. const y = (textureCoordinates.y * 4095.0) | 0;
  295. return 4096.0 * x + y;
  296. };
  297. /**
  298. * Decompresses texture coordinates that were packed into a single float.
  299. *
  300. * @param {Number} compressed The compressed texture coordinates.
  301. * @param {Cartesian2} result The decompressed texture coordinates.
  302. * @returns {Cartesian2} The modified result parameter.
  303. *
  304. */
  305. AttributeCompression.decompressTextureCoordinates = function (
  306. compressed,
  307. result
  308. ) {
  309. //>>includeStart('debug', pragmas.debug);
  310. Check.defined("compressed", compressed);
  311. Check.defined("result", result);
  312. //>>includeEnd('debug');
  313. const temp = compressed / 4096.0;
  314. const xZeroTo4095 = Math.floor(temp);
  315. result.x = xZeroTo4095 / 4095.0;
  316. result.y = (compressed - xZeroTo4095 * 4096) / 4095;
  317. return result;
  318. };
  319. function zigZagDecode(value) {
  320. return (value >> 1) ^ -(value & 1);
  321. }
  322. /**
  323. * Decodes delta and ZigZag encoded vertices. This modifies the buffers in place.
  324. *
  325. * @param {Uint16Array} uBuffer The buffer view of u values.
  326. * @param {Uint16Array} vBuffer The buffer view of v values.
  327. * @param {Uint16Array} [heightBuffer] The buffer view of height values.
  328. *
  329. * @see {@link https://github.com/CesiumGS/quantized-mesh|quantized-mesh-1.0 terrain format}
  330. */
  331. AttributeCompression.zigZagDeltaDecode = function (
  332. uBuffer,
  333. vBuffer,
  334. heightBuffer
  335. ) {
  336. //>>includeStart('debug', pragmas.debug);
  337. Check.defined("uBuffer", uBuffer);
  338. Check.defined("vBuffer", vBuffer);
  339. Check.typeOf.number.equals(
  340. "uBuffer.length",
  341. "vBuffer.length",
  342. uBuffer.length,
  343. vBuffer.length
  344. );
  345. if (defined(heightBuffer)) {
  346. Check.typeOf.number.equals(
  347. "uBuffer.length",
  348. "heightBuffer.length",
  349. uBuffer.length,
  350. heightBuffer.length
  351. );
  352. }
  353. //>>includeEnd('debug');
  354. const count = uBuffer.length;
  355. let u = 0;
  356. let v = 0;
  357. let height = 0;
  358. for (let i = 0; i < count; ++i) {
  359. u += zigZagDecode(uBuffer[i]);
  360. v += zigZagDecode(vBuffer[i]);
  361. uBuffer[i] = u;
  362. vBuffer[i] = v;
  363. if (defined(heightBuffer)) {
  364. height += zigZagDecode(heightBuffer[i]);
  365. heightBuffer[i] = height;
  366. }
  367. }
  368. };
  369. /**
  370. * Dequantizes a quantized typed array into a floating point typed array.
  371. *
  372. * @see {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data}
  373. *
  374. * @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array} typedArray The typed array for the quantized data.
  375. * @param {ComponentDatatype} componentDatatype The component datatype of the quantized data.
  376. * @param {AttributeType} type The attribute type of the quantized data.
  377. * @param {Number} count The number of attributes referenced in the dequantized array.
  378. *
  379. * @returns {Float32Array} The dequantized array.
  380. */
  381. AttributeCompression.dequantize = function (
  382. typedArray,
  383. componentDatatype,
  384. type,
  385. count
  386. ) {
  387. //>>includeStart('debug', pragmas.debug);
  388. Check.defined("typedArray", typedArray);
  389. Check.defined("componentDatatype", componentDatatype);
  390. Check.defined("type", type);
  391. Check.defined("count", count);
  392. //>>includeEnd('debug');
  393. const componentsPerAttribute = AttributeType.getNumberOfComponents(type);
  394. let divisor;
  395. switch (componentDatatype) {
  396. case ComponentDatatype.BYTE:
  397. divisor = 127.0;
  398. break;
  399. case ComponentDatatype.UNSIGNED_BYTE:
  400. divisor = 255.0;
  401. break;
  402. case ComponentDatatype.SHORT:
  403. divisor = 32767.0;
  404. break;
  405. case ComponentDatatype.UNSIGNED_SHORT:
  406. divisor = 65535.0;
  407. break;
  408. case ComponentDatatype.INT:
  409. divisor = 2147483647.0;
  410. break;
  411. case ComponentDatatype.UNSIGNED_INT:
  412. divisor = 4294967295.0;
  413. break;
  414. //>>includeStart('debug', pragmas.debug);
  415. default:
  416. throw new DeveloperError(
  417. `Cannot dequantize component datatype: ${componentDatatype}`
  418. );
  419. //>>includeEnd('debug');
  420. }
  421. const dequantizedTypedArray = new Float32Array(
  422. count * componentsPerAttribute
  423. );
  424. for (let i = 0; i < count; i++) {
  425. for (let j = 0; j < componentsPerAttribute; j++) {
  426. const index = i * componentsPerAttribute + j;
  427. dequantizedTypedArray[index] = Math.max(
  428. typedArray[index] / divisor,
  429. -1.0
  430. );
  431. }
  432. }
  433. return dequantizedTypedArray;
  434. };
  435. /**
  436. * Decode RGB565-encoded colors into a floating point typed array containing
  437. * normalized RGB values.
  438. *
  439. * @param {Uint16Array} typedArray Array of RGB565 values
  440. * @param {Float32Array} [result] Array to store the normalized VEC3 result
  441. */
  442. AttributeCompression.decodeRGB565 = function (typedArray, result) {
  443. //>>includeStart('debug', pragmas.debug);
  444. Check.defined("typedArray", typedArray);
  445. const expectedLength = typedArray.length * 3;
  446. if (defined(result)) {
  447. Check.typeOf.number.equals(
  448. "result.length",
  449. "typedArray.length * 3",
  450. result.length,
  451. expectedLength
  452. );
  453. }
  454. //>>includeEnd('debug');
  455. const count = typedArray.length;
  456. if (!defined(result)) {
  457. result = new Float32Array(count * 3);
  458. }
  459. const mask5 = (1 << 5) - 1;
  460. const mask6 = (1 << 6) - 1;
  461. const normalize5 = 1.0 / 31.0;
  462. const normalize6 = 1.0 / 63.0;
  463. for (let i = 0; i < count; i++) {
  464. const value = typedArray[i];
  465. const red = value >> 11;
  466. const green = (value >> 5) & mask6;
  467. const blue = value & mask5;
  468. const offset = 3 * i;
  469. result[offset] = red * normalize5;
  470. result[offset + 1] = green * normalize6;
  471. result[offset + 2] = blue * normalize5;
  472. }
  473. return result;
  474. };
  475. export default AttributeCompression;