AttributeCompression-84a90a13.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09'], function (exports, when, Check, _Math, Cartographic, Cartesian2) { 'use strict';
  24. var RIGHT_SHIFT = 1.0 / 256.0;
  25. var LEFT_SHIFT = 256.0;
  26. /**
  27. * Attribute compression and decompression functions.
  28. *
  29. * @exports AttributeCompression
  30. *
  31. * @private
  32. */
  33. var AttributeCompression = {};
  34. /**
  35. * Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
  36. *
  37. * Oct encoding is a compact representation of unit length vectors.
  38. * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
  39. * Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
  40. *
  41. * @param {Cartesian3} vector The normalized vector to be compressed into 2 component 'oct' encoding.
  42. * @param {Cartesian2} result The 2 component oct-encoded unit length vector.
  43. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  44. * @returns {Cartesian2} The 2 component oct-encoded unit length vector.
  45. *
  46. * @exception {DeveloperError} vector must be normalized.
  47. *
  48. * @see AttributeCompression.octDecodeInRange
  49. */
  50. AttributeCompression.octEncodeInRange = function(vector, rangeMax, result) {
  51. //>>includeStart('debug', pragmas.debug);
  52. Check.Check.defined('vector', vector);
  53. Check.Check.defined('result', result);
  54. var magSquared = Cartographic.Cartesian3.magnitudeSquared(vector);
  55. if (Math.abs(magSquared - 1.0) > _Math.CesiumMath.EPSILON6) {
  56. throw new Check.DeveloperError('vector must be normalized.');
  57. }
  58. //>>includeEnd('debug');
  59. result.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  60. result.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  61. if (vector.z < 0) {
  62. var x = result.x;
  63. var y = result.y;
  64. result.x = (1.0 - Math.abs(y)) * _Math.CesiumMath.signNotZero(x);
  65. result.y = (1.0 - Math.abs(x)) * _Math.CesiumMath.signNotZero(y);
  66. }
  67. result.x = _Math.CesiumMath.toSNorm(result.x, rangeMax);
  68. result.y = _Math.CesiumMath.toSNorm(result.y, rangeMax);
  69. return result;
  70. };
  71. /**
  72. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
  73. *
  74. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  75. * @param {Cartesian2} result The 2 byte oct-encoded unit length vector.
  76. * @returns {Cartesian2} The 2 byte oct-encoded unit length vector.
  77. *
  78. * @exception {DeveloperError} vector must be normalized.
  79. *
  80. * @see AttributeCompression.octEncodeInRange
  81. * @see AttributeCompression.octDecode
  82. */
  83. AttributeCompression.octEncode = function(vector, result) {
  84. return AttributeCompression.octEncodeInRange(vector, 255, result);
  85. };
  86. var octEncodeScratch = new Cartesian2.Cartesian2();
  87. var uint8ForceArray = new Uint8Array(1);
  88. function forceUint8(value) {
  89. uint8ForceArray[0] = value;
  90. return uint8ForceArray[0];
  91. }
  92. /**
  93. * @param {Cartesian3} vector The normalized vector to be compressed into 4 byte 'oct' encoding.
  94. * @param {Cartesian4} result The 4 byte oct-encoded unit length vector.
  95. * @returns {Cartesian4} The 4 byte oct-encoded unit length vector.
  96. *
  97. * @exception {DeveloperError} vector must be normalized.
  98. *
  99. * @see AttributeCompression.octEncodeInRange
  100. * @see AttributeCompression.octDecodeFromCartesian4
  101. */
  102. AttributeCompression.octEncodeToCartesian4 = function(vector, result) {
  103. AttributeCompression.octEncodeInRange(vector, 65535, octEncodeScratch);
  104. result.x = forceUint8(octEncodeScratch.x * RIGHT_SHIFT);
  105. result.y = forceUint8(octEncodeScratch.x);
  106. result.z = forceUint8(octEncodeScratch.y * RIGHT_SHIFT);
  107. result.w = forceUint8(octEncodeScratch.y);
  108. return result;
  109. };
  110. /**
  111. * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
  112. *
  113. * @param {Number} x The x component of the oct-encoded unit length vector.
  114. * @param {Number} y The y component of the oct-encoded unit length vector.
  115. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  116. * @param {Cartesian3} result The decoded and normalized vector
  117. * @returns {Cartesian3} The decoded and normalized vector.
  118. *
  119. * @exception {DeveloperError} x and y must be unsigned normalized integers between 0 and rangeMax.
  120. *
  121. * @see AttributeCompression.octEncodeInRange
  122. */
  123. AttributeCompression.octDecodeInRange = function(x, y, rangeMax, result) {
  124. //>>includeStart('debug', pragmas.debug);
  125. Check.Check.defined('result', result);
  126. if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
  127. throw new Check.DeveloperError('x and y must be unsigned normalized integers between 0 and ' + rangeMax);
  128. }
  129. //>>includeEnd('debug');
  130. result.x = _Math.CesiumMath.fromSNorm(x, rangeMax);
  131. result.y = _Math.CesiumMath.fromSNorm(y, rangeMax);
  132. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  133. if (result.z < 0.0)
  134. {
  135. var oldVX = result.x;
  136. result.x = (1.0 - Math.abs(result.y)) * _Math.CesiumMath.signNotZero(oldVX);
  137. result.y = (1.0 - Math.abs(oldVX)) * _Math.CesiumMath.signNotZero(result.y);
  138. }
  139. return Cartographic.Cartesian3.normalize(result, result);
  140. };
  141. /**
  142. * Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
  143. *
  144. * @param {Number} x The x component of the oct-encoded unit length vector.
  145. * @param {Number} y The y component of the oct-encoded unit length vector.
  146. * @param {Cartesian3} result The decoded and normalized vector.
  147. * @returns {Cartesian3} The decoded and normalized vector.
  148. *
  149. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and 255.
  150. *
  151. * @see AttributeCompression.octDecodeInRange
  152. */
  153. AttributeCompression.octDecode = function(x, y, result) {
  154. return AttributeCompression.octDecodeInRange(x, y, 255, result);
  155. };
  156. /**
  157. * Decodes a unit-length vector in 4 byte 'oct' encoding to a normalized 3-component vector.
  158. *
  159. * @param {Cartesian4} encoded The oct-encoded unit length vector.
  160. * @param {Cartesian3} result The decoded and normalized vector.
  161. * @returns {Cartesian3} The decoded and normalized vector.
  162. *
  163. * @exception {DeveloperError} x, y, z, and w must be unsigned normalized integers between 0 and 255.
  164. *
  165. * @see AttributeCompression.octDecodeInRange
  166. * @see AttributeCompression.octEncodeToCartesian4
  167. */
  168. AttributeCompression.octDecodeFromCartesian4 = function(encoded, result) {
  169. //>>includeStart('debug', pragmas.debug);
  170. Check.Check.typeOf.object('encoded', encoded);
  171. Check.Check.typeOf.object('result', result);
  172. //>>includeEnd('debug');
  173. var x = encoded.x;
  174. var y = encoded.y;
  175. var z = encoded.z;
  176. var w = encoded.w;
  177. //>>includeStart('debug', pragmas.debug);
  178. if (x < 0 || x > 255 || y < 0 || y > 255 || z < 0 || z > 255 || w < 0 || w > 255) {
  179. throw new Check.DeveloperError('x, y, z, and w must be unsigned normalized integers between 0 and 255');
  180. }
  181. //>>includeEnd('debug');
  182. var xOct16 = x * LEFT_SHIFT + y;
  183. var yOct16 = z * LEFT_SHIFT + w;
  184. return AttributeCompression.octDecodeInRange(xOct16, yOct16, 65535, result);
  185. };
  186. /**
  187. * Packs an oct encoded vector into a single floating-point number.
  188. *
  189. * @param {Cartesian2} encoded The oct encoded vector.
  190. * @returns {Number} The oct encoded vector packed into a single float.
  191. *
  192. */
  193. AttributeCompression.octPackFloat = function(encoded) {
  194. //>>includeStart('debug', pragmas.debug);
  195. Check.Check.defined('encoded', encoded);
  196. //>>includeEnd('debug');
  197. return 256.0 * encoded.x + encoded.y;
  198. };
  199. var scratchEncodeCart2 = new Cartesian2.Cartesian2();
  200. /**
  201. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
  202. * stores those values in a single float-point number.
  203. *
  204. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  205. * @returns {Number} The 2 byte oct-encoded unit length vector.
  206. *
  207. * @exception {DeveloperError} vector must be normalized.
  208. */
  209. AttributeCompression.octEncodeFloat = function(vector) {
  210. AttributeCompression.octEncode(vector, scratchEncodeCart2);
  211. return AttributeCompression.octPackFloat(scratchEncodeCart2);
  212. };
  213. /**
  214. * Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
  215. *
  216. * @param {Number} value The oct-encoded unit length vector stored as a single floating-point number.
  217. * @param {Cartesian3} result The decoded and normalized vector
  218. * @returns {Cartesian3} The decoded and normalized vector.
  219. *
  220. */
  221. AttributeCompression.octDecodeFloat = function(value, result) {
  222. //>>includeStart('debug', pragmas.debug);
  223. Check.Check.defined('value', value);
  224. //>>includeEnd('debug');
  225. var temp = value / 256.0;
  226. var x = Math.floor(temp);
  227. var y = (temp - x) * 256.0;
  228. return AttributeCompression.octDecode(x, y, result);
  229. };
  230. /**
  231. * Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
  232. * packs those into two floating-point numbers.
  233. *
  234. * @param {Cartesian3} v1 A normalized vector to be compressed.
  235. * @param {Cartesian3} v2 A normalized vector to be compressed.
  236. * @param {Cartesian3} v3 A normalized vector to be compressed.
  237. * @param {Cartesian2} result The 'oct' encoded vectors packed into two floating-point numbers.
  238. * @returns {Cartesian2} The 'oct' encoded vectors packed into two floating-point numbers.
  239. *
  240. */
  241. AttributeCompression.octPack = function(v1, v2, v3, result) {
  242. //>>includeStart('debug', pragmas.debug);
  243. Check.Check.defined('v1', v1);
  244. Check.Check.defined('v2', v2);
  245. Check.Check.defined('v3', v3);
  246. Check.Check.defined('result', result);
  247. //>>includeEnd('debug');
  248. var encoded1 = AttributeCompression.octEncodeFloat(v1);
  249. var encoded2 = AttributeCompression.octEncodeFloat(v2);
  250. var encoded3 = AttributeCompression.octEncode(v3, scratchEncodeCart2);
  251. result.x = 65536.0 * encoded3.x + encoded1;
  252. result.y = 65536.0 * encoded3.y + encoded2;
  253. return result;
  254. };
  255. /**
  256. * Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
  257. *
  258. * @param {Cartesian2} packed The three oct-encoded unit length vectors stored as two floating-point number.
  259. * @param {Cartesian3} v1 One decoded and normalized vector.
  260. * @param {Cartesian3} v2 One decoded and normalized vector.
  261. * @param {Cartesian3} v3 One decoded and normalized vector.
  262. */
  263. AttributeCompression.octUnpack = function(packed, v1, v2, v3) {
  264. //>>includeStart('debug', pragmas.debug);
  265. Check.Check.defined('packed', packed);
  266. Check.Check.defined('v1', v1);
  267. Check.Check.defined('v2', v2);
  268. Check.Check.defined('v3', v3);
  269. //>>includeEnd('debug');
  270. var temp = packed.x / 65536.0;
  271. var x = Math.floor(temp);
  272. var encodedFloat1 = (temp - x) * 65536.0;
  273. temp = packed.y / 65536.0;
  274. var y = Math.floor(temp);
  275. var encodedFloat2 = (temp - y) * 65536.0;
  276. AttributeCompression.octDecodeFloat(encodedFloat1, v1);
  277. AttributeCompression.octDecodeFloat(encodedFloat2, v2);
  278. AttributeCompression.octDecode(x, y, v3);
  279. };
  280. /**
  281. * Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
  282. *
  283. * @param {Cartesian2} textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
  284. * @returns {Number} The packed texture coordinates.
  285. *
  286. */
  287. AttributeCompression.compressTextureCoordinates = function(textureCoordinates) {
  288. //>>includeStart('debug', pragmas.debug);
  289. Check.Check.defined('textureCoordinates', textureCoordinates);
  290. //>>includeEnd('debug');
  291. // Move x and y to the range 0-4095;
  292. var x = (textureCoordinates.x * 4095.0) | 0;
  293. var y = (textureCoordinates.y * 4095.0) | 0;
  294. return 4096.0 * x + y;
  295. };
  296. /**
  297. * Decompresses texture coordinates that were packed into a single float.
  298. *
  299. * @param {Number} compressed The compressed texture coordinates.
  300. * @param {Cartesian2} result The decompressed texture coordinates.
  301. * @returns {Cartesian2} The modified result parameter.
  302. *
  303. */
  304. AttributeCompression.decompressTextureCoordinates = function(compressed, result) {
  305. //>>includeStart('debug', pragmas.debug);
  306. Check.Check.defined('compressed', compressed);
  307. Check.Check.defined('result', result);
  308. //>>includeEnd('debug');
  309. var temp = compressed / 4096.0;
  310. var xZeroTo4095 = Math.floor(temp);
  311. result.x = xZeroTo4095 / 4095.0;
  312. result.y = (compressed - xZeroTo4095 * 4096) / 4095;
  313. return result;
  314. };
  315. function zigZagDecode(value) {
  316. return (value >> 1) ^ (-(value & 1));
  317. }
  318. /**
  319. * Decodes delta and ZigZag encoded vertices. This modifies the buffers in place.
  320. *
  321. * @param {Uint16Array} uBuffer The buffer view of u values.
  322. * @param {Uint16Array} vBuffer The buffer view of v values.
  323. * @param {Uint16Array} [heightBuffer] The buffer view of height values.
  324. *
  325. * @see {@link https://github.com/AnalyticalGraphicsInc/quantized-mesh|quantized-mesh-1.0 terrain format}
  326. */
  327. AttributeCompression.zigZagDeltaDecode = function(uBuffer, vBuffer, heightBuffer) {
  328. //>>includeStart('debug', pragmas.debug);
  329. Check.Check.defined('uBuffer', uBuffer);
  330. Check.Check.defined('vBuffer', vBuffer);
  331. Check.Check.typeOf.number.equals('uBuffer.length', 'vBuffer.length', uBuffer.length, vBuffer.length);
  332. if (when.defined(heightBuffer)) {
  333. Check.Check.typeOf.number.equals('uBuffer.length', 'heightBuffer.length', uBuffer.length, heightBuffer.length);
  334. }
  335. //>>includeEnd('debug');
  336. var count = uBuffer.length;
  337. var u = 0;
  338. var v = 0;
  339. var height = 0;
  340. for (var i = 0; i < count; ++i) {
  341. u += zigZagDecode(uBuffer[i]);
  342. v += zigZagDecode(vBuffer[i]);
  343. uBuffer[i] = u;
  344. vBuffer[i] = v;
  345. if (when.defined(heightBuffer)) {
  346. height += zigZagDecode(heightBuffer[i]);
  347. heightBuffer[i] = height;
  348. }
  349. }
  350. };
  351. // 将oct压缩的short转化为float
  352. AttributeCompression.octShortToFloat = function(s){
  353. //var tempF = s * 1.0 / ((1 << 15) - 1);
  354. return _Math.CesiumMath.clamp(s * 0.00003051850947599719, -1.0, 1.0);
  355. };
  356. // 将oct压缩的两个short解压为三个float
  357. AttributeCompression.octShortDecode = function(x, y, result) {
  358. Check.Check.defined('result', result);
  359. result.x = AttributeCompression.octShortToFloat(x);
  360. result.y = AttributeCompression.octShortToFloat(y);
  361. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  362. if (result.z < 0.0)
  363. {
  364. var oldVX = result.x;
  365. result.x = (1.0 - Math.abs(result.y)) * _Math.CesiumMath.signNotZero(oldVX);
  366. result.y = (1.0 - Math.abs(oldVX)) * _Math.CesiumMath.signNotZero(result.y);
  367. }
  368. return Cartographic.Cartesian3.normalize(result, result);
  369. };
  370. exports.AttributeCompression = AttributeCompression;
  371. });