TerrainEncoding-4dcd0461.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './defined-26bd4a03', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './Transforms-65aba0a4', './ComponentDatatype-69643096', './AttributeCompression-87682214'], function (exports, defined, freezeObject, defaultValue, _Math, Cartesian2, Transforms, ComponentDatatype, AttributeCompression) { 'use strict';
  3. /**
  4. * This enumerated type is used to determine how the vertices of the terrain mesh are compressed.
  5. *
  6. * @exports TerrainQuantization
  7. *
  8. * @private
  9. */
  10. var TerrainQuantization = {
  11. /**
  12. * The vertices are not compressed.
  13. *
  14. * @type {Number}
  15. * @constant
  16. */
  17. NONE : 0,
  18. /**
  19. * The vertices are compressed to 12 bits.
  20. *
  21. * @type {Number}
  22. * @constant
  23. */
  24. BITS12 : 1
  25. };
  26. var TerrainQuantization$1 = freezeObject.freezeObject(TerrainQuantization);
  27. var cartesian3Scratch = new Cartesian2.Cartesian3();
  28. var cartesian3DimScratch = new Cartesian2.Cartesian3();
  29. var cartesian2Scratch = new Cartesian2.Cartesian2();
  30. var matrix4Scratch = new Transforms.Matrix4();
  31. var matrix4Scratch2 = new Transforms.Matrix4();
  32. var SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
  33. /**
  34. * Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
  35. * are unpacked in the vertex shader.
  36. *
  37. * @alias TerrainEncoding
  38. * @constructor
  39. *
  40. * @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
  41. * @param {Number} minimumHeight The minimum height.
  42. * @param {Number} maximumHeight The maximum height.
  43. * @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
  44. * @param {Boolean} hasVertexNormals If the mesh has vertex normals.
  45. * @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
  46. *
  47. * @private
  48. */
  49. function TerrainEncoding(axisAlignedBoundingBox, minimumHeight, maximumHeight, fromENU, hasVertexNormals, hasWebMercatorT) {
  50. var quantization = TerrainQuantization$1.NONE;
  51. var center;
  52. var toENU;
  53. var matrix;
  54. if (defined.defined(axisAlignedBoundingBox) && defined.defined(minimumHeight) && defined.defined(maximumHeight) && defined.defined(fromENU)) {
  55. var minimum = axisAlignedBoundingBox.minimum;
  56. var maximum = axisAlignedBoundingBox.maximum;
  57. var dimensions = Cartesian2.Cartesian3.subtract(maximum, minimum, cartesian3DimScratch);
  58. var hDim = maximumHeight - minimumHeight;
  59. var maxDim = Math.max(Cartesian2.Cartesian3.maximumComponent(dimensions), hDim);
  60. if (maxDim < SHIFT_LEFT_12 - 1.0) {
  61. quantization = TerrainQuantization$1.BITS12;
  62. } else {
  63. quantization = TerrainQuantization$1.NONE;
  64. }
  65. center = axisAlignedBoundingBox.center;
  66. toENU = Transforms.Matrix4.inverseTransformation(fromENU, new Transforms.Matrix4());
  67. var translation = Cartesian2.Cartesian3.negate(minimum, cartesian3Scratch);
  68. Transforms.Matrix4.multiply(Transforms.Matrix4.fromTranslation(translation, matrix4Scratch), toENU, toENU);
  69. var scale = cartesian3Scratch;
  70. scale.x = 1.0 / dimensions.x;
  71. scale.y = 1.0 / dimensions.y;
  72. scale.z = 1.0 / dimensions.z;
  73. Transforms.Matrix4.multiply(Transforms.Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
  74. matrix = Transforms.Matrix4.clone(fromENU);
  75. Transforms.Matrix4.setTranslation(matrix, Cartesian2.Cartesian3.ZERO, matrix);
  76. fromENU = Transforms.Matrix4.clone(fromENU, new Transforms.Matrix4());
  77. var translationMatrix = Transforms.Matrix4.fromTranslation(minimum, matrix4Scratch);
  78. var scaleMatrix = Transforms.Matrix4.fromScale(dimensions, matrix4Scratch2);
  79. var st = Transforms.Matrix4.multiply(translationMatrix, scaleMatrix,matrix4Scratch);
  80. Transforms.Matrix4.multiply(fromENU, st, fromENU);
  81. Transforms.Matrix4.multiply(matrix, st, matrix);
  82. }
  83. /**
  84. * How the vertices of the mesh were compressed.
  85. * @type {TerrainQuantization}
  86. */
  87. this.quantization = quantization;
  88. /**
  89. * The minimum height of the tile including the skirts.
  90. * @type {Number}
  91. */
  92. this.minimumHeight = minimumHeight;
  93. /**
  94. * The maximum height of the tile.
  95. * @type {Number}
  96. */
  97. this.maximumHeight = maximumHeight;
  98. /**
  99. * The center of the tile.
  100. * @type {Cartesian3}
  101. */
  102. this.center = center;
  103. /**
  104. * A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
  105. * it so each component is in the [0, 1] range.
  106. * @type {Matrix4}
  107. */
  108. this.toScaledENU = toENU;
  109. /**
  110. * A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
  111. * @type {Matrix4}
  112. */
  113. this.fromScaledENU = fromENU;
  114. /**
  115. * The matrix used to decompress the terrain vertices in the shader for RTE rendering.
  116. * @type {Matrix4}
  117. */
  118. this.matrix = matrix;
  119. /**
  120. * The terrain mesh contains normals.
  121. * @type {Boolean}
  122. */
  123. this.hasVertexNormals = hasVertexNormals;
  124. /**
  125. * The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
  126. * @type {Boolean}
  127. */
  128. this.hasWebMercatorT = defaultValue.defaultValue(hasWebMercatorT, false);
  129. }
  130. TerrainEncoding.prototype.encode = function(vertexBuffer, bufferIndex, position, uv, height, normalToPack, webMercatorT) {
  131. var u = uv.x;
  132. var v = uv.y;
  133. if (this.quantization === TerrainQuantization$1.BITS12) {
  134. position = Transforms.Matrix4.multiplyByPoint(this.toScaledENU, position, cartesian3Scratch);
  135. position.x = _Math.CesiumMath.clamp(position.x, 0.0, 1.0);
  136. position.y = _Math.CesiumMath.clamp(position.y, 0.0, 1.0);
  137. position.z = _Math.CesiumMath.clamp(position.z, 0.0, 1.0);
  138. var hDim = this.maximumHeight - this.minimumHeight;
  139. var h = _Math.CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
  140. Cartesian2.Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
  141. var compressed0 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  142. Cartesian2.Cartesian2.fromElements(position.z, h, cartesian2Scratch);
  143. var compressed1 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  144. Cartesian2.Cartesian2.fromElements(u, v, cartesian2Scratch);
  145. var compressed2 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  146. vertexBuffer[bufferIndex++] = compressed0;
  147. vertexBuffer[bufferIndex++] = compressed1;
  148. vertexBuffer[bufferIndex++] = compressed2;
  149. if (this.hasWebMercatorT) {
  150. Cartesian2.Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
  151. var compressed3 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  152. vertexBuffer[bufferIndex++] = compressed3;
  153. }
  154. } else {
  155. Cartesian2.Cartesian3.subtract(position, this.center, cartesian3Scratch);
  156. vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
  157. vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
  158. vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
  159. vertexBuffer[bufferIndex++] = height;
  160. vertexBuffer[bufferIndex++] = u;
  161. vertexBuffer[bufferIndex++] = v;
  162. if (this.hasWebMercatorT) {
  163. vertexBuffer[bufferIndex++] = webMercatorT;
  164. }
  165. }
  166. if (this.hasVertexNormals) {
  167. vertexBuffer[bufferIndex++] = AttributeCompression.AttributeCompression.octPackFloat(normalToPack);
  168. }
  169. return bufferIndex;
  170. };
  171. TerrainEncoding.prototype.decodePosition = function(buffer, index, result) {
  172. if (!defined.defined(result)) {
  173. result = new Cartesian2.Cartesian3();
  174. }
  175. index *= this.getStride();
  176. if (this.quantization === TerrainQuantization$1.BITS12) {
  177. var xy = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index], cartesian2Scratch);
  178. result.x = xy.x;
  179. result.y = xy.y;
  180. var zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  181. result.z = zh.x;
  182. return Transforms.Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
  183. }
  184. result.x = buffer[index];
  185. result.y = buffer[index + 1];
  186. result.z = buffer[index + 2];
  187. return Cartesian2.Cartesian3.add(result, this.center, result);
  188. };
  189. TerrainEncoding.prototype.decodeTextureCoordinates = function(buffer, index, result) {
  190. if (!defined.defined(result)) {
  191. result = new Cartesian2.Cartesian2();
  192. }
  193. index *= this.getStride();
  194. if (this.quantization === TerrainQuantization$1.BITS12) {
  195. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 2], result);
  196. }
  197. return Cartesian2.Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
  198. };
  199. TerrainEncoding.prototype.decodeHeight = function(buffer, index) {
  200. index *= this.getStride();
  201. if (this.quantization === TerrainQuantization$1.BITS12) {
  202. var zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  203. return zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight;
  204. }
  205. return buffer[index + 3];
  206. };
  207. TerrainEncoding.prototype.decodeWebMercatorT = function(buffer, index) {
  208. index *= this.getStride();
  209. if (this.quantization === TerrainQuantization$1.BITS12) {
  210. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 3], cartesian2Scratch).x;
  211. }
  212. return buffer[index + 6];
  213. };
  214. TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
  215. var stride = this.getStride();
  216. index = (index + 1) * stride - 1;
  217. var temp = buffer[index] / 256.0;
  218. var x = Math.floor(temp);
  219. var y = (temp - x) * 256.0;
  220. return Cartesian2.Cartesian2.fromElements(x, y, result);
  221. };
  222. TerrainEncoding.prototype.getStride = function() {
  223. var vertexStride;
  224. switch (this.quantization) {
  225. case TerrainQuantization$1.BITS12:
  226. vertexStride = 3;
  227. break;
  228. default:
  229. vertexStride = 6;
  230. }
  231. if (this.hasWebMercatorT) {
  232. ++vertexStride;
  233. }
  234. if (this.hasVertexNormals) {
  235. ++vertexStride;
  236. }
  237. return vertexStride;
  238. };
  239. var attributesNone = {
  240. position3DAndHeight : 0,
  241. textureCoordAndEncodedNormals : 1
  242. };
  243. var attributes = {
  244. compressed0 : 0,
  245. compressed1 : 1
  246. };
  247. TerrainEncoding.prototype.getAttributes = function(buffer) {
  248. var datatype = ComponentDatatype.ComponentDatatype.FLOAT;
  249. var sizeInBytes = ComponentDatatype.ComponentDatatype.getSizeInBytes(datatype);
  250. var stride;
  251. if (this.quantization === TerrainQuantization$1.NONE) {
  252. var position3DAndHeightLength = 4;
  253. var numTexCoordComponents = 2;
  254. if (this.hasWebMercatorT) {
  255. ++numTexCoordComponents;
  256. }
  257. if (this.hasVertexNormals) {
  258. ++numTexCoordComponents;
  259. }
  260. stride = (position3DAndHeightLength + numTexCoordComponents) * sizeInBytes;
  261. return [{
  262. index : attributesNone.position3DAndHeight,
  263. vertexBuffer : buffer,
  264. componentDatatype : datatype,
  265. componentsPerAttribute : position3DAndHeightLength,
  266. offsetInBytes : 0,
  267. strideInBytes : stride
  268. }, {
  269. index : attributesNone.textureCoordAndEncodedNormals,
  270. vertexBuffer : buffer,
  271. componentDatatype : datatype,
  272. componentsPerAttribute : numTexCoordComponents,
  273. offsetInBytes : position3DAndHeightLength * sizeInBytes,
  274. strideInBytes : stride
  275. }];
  276. }
  277. var numCompressed0 = 3;
  278. var numCompressed1 = 0;
  279. if (this.hasWebMercatorT || this.hasVertexNormals) {
  280. ++numCompressed0;
  281. }
  282. if (this.hasWebMercatorT && this.hasVertexNormals) {
  283. ++numCompressed1;
  284. stride = (numCompressed0 + numCompressed1) * sizeInBytes;
  285. return [{
  286. index : attributes.compressed0,
  287. vertexBuffer : buffer,
  288. componentDatatype : datatype,
  289. componentsPerAttribute : numCompressed0,
  290. offsetInBytes : 0,
  291. strideInBytes : stride
  292. }, {
  293. index : attributes.compressed1,
  294. vertexBuffer : buffer,
  295. componentDatatype : datatype,
  296. componentsPerAttribute : numCompressed1,
  297. offsetInBytes : numCompressed0 * sizeInBytes,
  298. strideInBytes : stride
  299. }];
  300. }
  301. return [{
  302. index : attributes.compressed0,
  303. vertexBuffer : buffer,
  304. componentDatatype : datatype,
  305. componentsPerAttribute : numCompressed0
  306. }];
  307. };
  308. TerrainEncoding.prototype.getAttributeLocations = function() {
  309. if (this.quantization === TerrainQuantization$1.NONE) {
  310. return attributesNone;
  311. }
  312. return attributes;
  313. };
  314. TerrainEncoding.clone = function(encoding, result) {
  315. if (!defined.defined(result)) {
  316. result = new TerrainEncoding();
  317. }
  318. result.quantization = encoding.quantization;
  319. result.minimumHeight = encoding.minimumHeight;
  320. result.maximumHeight = encoding.maximumHeight;
  321. result.center = Cartesian2.Cartesian3.clone(encoding.center);
  322. result.toScaledENU = Transforms.Matrix4.clone(encoding.toScaledENU);
  323. result.fromScaledENU = Transforms.Matrix4.clone(encoding.fromScaledENU);
  324. result.matrix = Transforms.Matrix4.clone(encoding.matrix);
  325. result.hasVertexNormals = encoding.hasVertexNormals;
  326. result.hasWebMercatorT = encoding.hasWebMercatorT;
  327. return result;
  328. };
  329. exports.TerrainEncoding = TerrainEncoding;
  330. });