createVerticesFromQuantizedTerrainMesh.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defined-26bd4a03', './Check-da037458', './freezeObject-2d83f591', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4', './RuntimeError-ad75c885', './WebGLConstants-497deb20', './ComponentDatatype-69643096', './when-ee12a2cb', './AttributeCompression-87682214', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './WebMercatorProjection-f2dc467d', './createTaskProcessorWorker', './EllipsoidTangentPlane-10c6053a', './OrientedBoundingBox-a786ab5d', './TerrainEncoding-4dcd0461'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, when, AttributeCompression, IndexDatatype, IntersectionTests, Plane, WebMercatorProjection, createTaskProcessorWorker, EllipsoidTangentPlane, OrientedBoundingBox, TerrainEncoding) { 'use strict';
  3. var maxShort = 32767;
  4. var cartesian3Scratch = new Cartesian2.Cartesian3();
  5. var scratchMinimum = new Cartesian2.Cartesian3();
  6. var scratchMaximum = new Cartesian2.Cartesian3();
  7. var cartographicScratch = new Cartesian2.Cartographic();
  8. var toPack = new Cartesian2.Cartesian2();
  9. var scratchNormal = new Cartesian2.Cartesian3();
  10. var scratchToENU = new Transforms.Matrix4();
  11. var scratchFromENU = new Transforms.Matrix4();
  12. function createVerticesFromQuantizedTerrainMesh(parameters, transferableObjects) {
  13. var quantizedVertices = parameters.quantizedVertices;
  14. var quantizedVertexCount = quantizedVertices.length / 3;
  15. var octEncodedNormals = parameters.octEncodedNormals;
  16. var edgeVertexCount = parameters.westIndices.length + parameters.eastIndices.length +
  17. parameters.southIndices.length + parameters.northIndices.length;
  18. var includeWebMercatorT = parameters.includeWebMercatorT;
  19. var rectangle = parameters.rectangle;
  20. var west = rectangle.west;
  21. var south = rectangle.south;
  22. var east = rectangle.east;
  23. var north = rectangle.north;
  24. var ellipsoid = Cartesian2.Ellipsoid.clone(parameters.ellipsoid);
  25. var exaggeration = parameters.exaggeration;
  26. var minimumHeight = parameters.minimumHeight * exaggeration;
  27. var maximumHeight = parameters.maximumHeight * exaggeration;
  28. var center = parameters.relativeToCenter;
  29. var fromENU = Transforms.Transforms.eastNorthUpToFixedFrame(center, ellipsoid);
  30. var toENU = Transforms.Matrix4.inverseTransformation(fromENU, new Transforms.Matrix4());
  31. var southMercatorY;
  32. var oneOverMercatorHeight;
  33. if (includeWebMercatorT) {
  34. southMercatorY = WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(south);
  35. oneOverMercatorHeight = 1.0 / (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(north) - southMercatorY);
  36. }
  37. var uBuffer = quantizedVertices.subarray(0, quantizedVertexCount);
  38. var vBuffer = quantizedVertices.subarray(quantizedVertexCount, 2 * quantizedVertexCount);
  39. var heightBuffer = quantizedVertices.subarray(quantizedVertexCount * 2, 3 * quantizedVertexCount);
  40. var hasVertexNormals = defined.defined(octEncodedNormals);
  41. var uvs = new Array(quantizedVertexCount);
  42. var heights = new Array(quantizedVertexCount);
  43. var positions = new Array(quantizedVertexCount);
  44. var webMercatorTs = includeWebMercatorT ? new Array(quantizedVertexCount) : [];
  45. var minimum = scratchMinimum;
  46. minimum.x = Number.POSITIVE_INFINITY;
  47. minimum.y = Number.POSITIVE_INFINITY;
  48. minimum.z = Number.POSITIVE_INFINITY;
  49. var maximum = scratchMaximum;
  50. maximum.x = Number.NEGATIVE_INFINITY;
  51. maximum.y = Number.NEGATIVE_INFINITY;
  52. maximum.z = Number.NEGATIVE_INFINITY;
  53. var minLongitude = Number.POSITIVE_INFINITY;
  54. var maxLongitude = Number.NEGATIVE_INFINITY;
  55. var minLatitude = Number.POSITIVE_INFINITY;
  56. var maxLatitude = Number.NEGATIVE_INFINITY;
  57. for (var i = 0; i < quantizedVertexCount; ++i) {
  58. var rawU = uBuffer[i];
  59. var rawV = vBuffer[i];
  60. var u = rawU / maxShort;
  61. var v = rawV / maxShort;
  62. var height = _Math.CesiumMath.lerp(minimumHeight, maximumHeight, heightBuffer[i] / maxShort);
  63. cartographicScratch.longitude = _Math.CesiumMath.lerp(west, east, u);
  64. cartographicScratch.latitude = _Math.CesiumMath.lerp(south, north, v);
  65. cartographicScratch.height = height;
  66. minLongitude = Math.min(cartographicScratch.longitude, minLongitude);
  67. maxLongitude = Math.max(cartographicScratch.longitude, maxLongitude);
  68. minLatitude = Math.min(cartographicScratch.latitude, minLatitude);
  69. maxLatitude = Math.max(cartographicScratch.latitude, maxLatitude);
  70. var position = ellipsoid.cartographicToCartesian(cartographicScratch);
  71. uvs[i] = new Cartesian2.Cartesian2(u, v);
  72. heights[i] = height;
  73. positions[i] = position;
  74. if (includeWebMercatorT) {
  75. webMercatorTs[i] = (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(cartographicScratch.latitude) - southMercatorY) * oneOverMercatorHeight;
  76. }
  77. Transforms.Matrix4.multiplyByPoint(toENU, position, cartesian3Scratch);
  78. Cartesian2.Cartesian3.minimumByComponent(cartesian3Scratch, minimum, minimum);
  79. Cartesian2.Cartesian3.maximumByComponent(cartesian3Scratch, maximum, maximum);
  80. }
  81. var westIndicesSouthToNorth = copyAndSort(parameters.westIndices, function(a, b) {
  82. return uvs[a].y - uvs[b].y;
  83. });
  84. var eastIndicesNorthToSouth = copyAndSort(parameters.eastIndices, function(a, b) {
  85. return uvs[b].y - uvs[a].y;
  86. });
  87. var southIndicesEastToWest = copyAndSort(parameters.southIndices, function(a, b) {
  88. return uvs[b].x - uvs[a].x;
  89. });
  90. var northIndicesWestToEast = copyAndSort(parameters.northIndices, function(a, b) {
  91. return uvs[a].x - uvs[b].x;
  92. });
  93. var orientedBoundingBox;
  94. var boundingSphere;
  95. if (exaggeration !== 1.0) {
  96. // Bounding volumes and horizon culling point need to be recomputed since the tile payload assumes no exaggeration.
  97. boundingSphere = Transforms.BoundingSphere.fromPoints(positions);
  98. orientedBoundingBox = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(rectangle, minimumHeight, maximumHeight, ellipsoid);
  99. }
  100. var hMin = minimumHeight;
  101. hMin = Math.min(hMin, findMinMaxSkirts(parameters.westIndices, parameters.westSkirtHeight, heights, uvs, rectangle, ellipsoid, toENU, minimum, maximum));
  102. hMin = Math.min(hMin, findMinMaxSkirts(parameters.southIndices, parameters.southSkirtHeight, heights, uvs, rectangle, ellipsoid, toENU, minimum, maximum));
  103. hMin = Math.min(hMin, findMinMaxSkirts(parameters.eastIndices, parameters.eastSkirtHeight, heights, uvs, rectangle, ellipsoid, toENU, minimum, maximum));
  104. hMin = Math.min(hMin, findMinMaxSkirts(parameters.northIndices, parameters.northSkirtHeight, heights, uvs, rectangle, ellipsoid, toENU, minimum, maximum));
  105. var aaBox = new EllipsoidTangentPlane.AxisAlignedBoundingBox(minimum, maximum, center);
  106. var encoding = new TerrainEncoding.TerrainEncoding(aaBox, hMin, maximumHeight, fromENU, hasVertexNormals, includeWebMercatorT);
  107. var vertexStride = encoding.getStride();
  108. var size = quantizedVertexCount * vertexStride + edgeVertexCount * vertexStride;
  109. var vertexBuffer = new Float32Array(size);
  110. var bufferIndex = 0;
  111. for (var j = 0; j < quantizedVertexCount; ++j) {
  112. if (hasVertexNormals) {
  113. var n = j * 2.0;
  114. toPack.x = octEncodedNormals[n];
  115. toPack.y = octEncodedNormals[n + 1];
  116. if (exaggeration !== 1.0) {
  117. var normal = AttributeCompression.AttributeCompression.octDecode(toPack.x, toPack.y, scratchNormal);
  118. var fromENUNormal = Transforms.Transforms.eastNorthUpToFixedFrame(positions[j], ellipsoid, scratchFromENU);
  119. var toENUNormal = Transforms.Matrix4.inverseTransformation(fromENUNormal, scratchToENU);
  120. Transforms.Matrix4.multiplyByPointAsVector(toENUNormal, normal, normal);
  121. normal.z *= exaggeration;
  122. Cartesian2.Cartesian3.normalize(normal, normal);
  123. Transforms.Matrix4.multiplyByPointAsVector(fromENUNormal, normal, normal);
  124. Cartesian2.Cartesian3.normalize(normal, normal);
  125. AttributeCompression.AttributeCompression.octEncode(normal, toPack);
  126. }
  127. }
  128. bufferIndex = encoding.encode(vertexBuffer, bufferIndex, positions[j], uvs[j], heights[j], toPack, webMercatorTs[j]);
  129. }
  130. var edgeTriangleCount = Math.max(0, (edgeVertexCount - 4) * 2);
  131. var indexBufferLength = parameters.indices.length + edgeTriangleCount * 3;
  132. var indexBuffer = IndexDatatype.IndexDatatype.createTypedArray(quantizedVertexCount + edgeVertexCount, indexBufferLength);
  133. indexBuffer.set(parameters.indices, 0);
  134. var percentage = 0.0001;
  135. var lonOffset = (maxLongitude - minLongitude) * percentage;
  136. var latOffset = (maxLatitude - minLatitude) * percentage;
  137. var westLongitudeOffset = -lonOffset;
  138. var westLatitudeOffset = 0.0;
  139. var eastLongitudeOffset = lonOffset;
  140. var eastLatitudeOffset = 0.0;
  141. var northLongitudeOffset = 0.0;
  142. var northLatitudeOffset = latOffset;
  143. var southLongitudeOffset = 0.0;
  144. var southLatitudeOffset = -latOffset;
  145. // Add skirts.
  146. var vertexBufferIndex = quantizedVertexCount * vertexStride;
  147. var indexBufferIndex = parameters.indices.length;
  148. indexBufferIndex = addSkirt(vertexBuffer, vertexBufferIndex, indexBuffer, indexBufferIndex, parameters.westIndices, encoding, heights, uvs, octEncodedNormals, ellipsoid, rectangle, parameters.westSkirtHeight, true, exaggeration, southMercatorY, oneOverMercatorHeight, westLongitudeOffset, westLatitudeOffset);
  149. vertexBufferIndex += parameters.westIndices.length * vertexStride;
  150. indexBufferIndex = addSkirt(vertexBuffer, vertexBufferIndex, indexBuffer, indexBufferIndex, parameters.southIndices, encoding, heights, uvs, octEncodedNormals, ellipsoid, rectangle, parameters.southSkirtHeight, false, exaggeration, southMercatorY, oneOverMercatorHeight, southLongitudeOffset, southLatitudeOffset);
  151. vertexBufferIndex += parameters.southIndices.length * vertexStride;
  152. indexBufferIndex = addSkirt(vertexBuffer, vertexBufferIndex, indexBuffer, indexBufferIndex, parameters.eastIndices, encoding, heights, uvs, octEncodedNormals, ellipsoid, rectangle, parameters.eastSkirtHeight, false, exaggeration, southMercatorY, oneOverMercatorHeight, eastLongitudeOffset, eastLatitudeOffset);
  153. vertexBufferIndex += parameters.eastIndices.length * vertexStride;
  154. addSkirt(vertexBuffer, vertexBufferIndex, indexBuffer, indexBufferIndex, parameters.northIndices, encoding, heights, uvs, octEncodedNormals, ellipsoid, rectangle, parameters.northSkirtHeight, true, exaggeration, southMercatorY, oneOverMercatorHeight, northLongitudeOffset, northLatitudeOffset);
  155. transferableObjects.push(vertexBuffer.buffer, indexBuffer.buffer);
  156. return {
  157. vertices : vertexBuffer.buffer,
  158. indices : indexBuffer.buffer,
  159. westIndicesSouthToNorth : westIndicesSouthToNorth,
  160. southIndicesEastToWest : southIndicesEastToWest,
  161. eastIndicesNorthToSouth : eastIndicesNorthToSouth,
  162. northIndicesWestToEast : northIndicesWestToEast,
  163. vertexStride : vertexStride,
  164. center : center,
  165. minimumHeight : minimumHeight,
  166. maximumHeight : maximumHeight,
  167. boundingSphere : boundingSphere,
  168. orientedBoundingBox : orientedBoundingBox,
  169. encoding : encoding,
  170. skirtIndex : parameters.indices.length
  171. };
  172. }
  173. function findMinMaxSkirts(edgeIndices, edgeHeight, heights, uvs, rectangle, ellipsoid, toENU, minimum, maximum) {
  174. var hMin = Number.POSITIVE_INFINITY;
  175. var north = rectangle.north;
  176. var south = rectangle.south;
  177. var east = rectangle.east;
  178. var west = rectangle.west;
  179. if (east < west) {
  180. east += _Math.CesiumMath.TWO_PI;
  181. }
  182. var length = edgeIndices.length;
  183. for (var i = 0; i < length; ++i) {
  184. var index = edgeIndices[i];
  185. var h = heights[index];
  186. var uv = uvs[index];
  187. cartographicScratch.longitude = _Math.CesiumMath.lerp(west, east, uv.x);
  188. cartographicScratch.latitude = _Math.CesiumMath.lerp(south, north, uv.y);
  189. cartographicScratch.height = h - edgeHeight;
  190. var position = ellipsoid.cartographicToCartesian(cartographicScratch, cartesian3Scratch);
  191. Transforms.Matrix4.multiplyByPoint(toENU, position, position);
  192. Cartesian2.Cartesian3.minimumByComponent(position, minimum, minimum);
  193. Cartesian2.Cartesian3.maximumByComponent(position, maximum, maximum);
  194. hMin = Math.min(hMin, cartographicScratch.height);
  195. }
  196. return hMin;
  197. }
  198. function addSkirt(vertexBuffer, vertexBufferIndex, indexBuffer, indexBufferIndex, edgeVertices, encoding, heights, uvs, octEncodedNormals, ellipsoid, rectangle, skirtLength, isWestOrNorthEdge, exaggeration, southMercatorY, oneOverMercatorHeight, longitudeOffset, latitudeOffset) {
  199. var start, end, increment;
  200. if (isWestOrNorthEdge) {
  201. start = edgeVertices.length - 1;
  202. end = -1;
  203. increment = -1;
  204. } else {
  205. start = 0;
  206. end = edgeVertices.length;
  207. increment = 1;
  208. }
  209. var previousIndex = -1;
  210. var hasVertexNormals = defined.defined(octEncodedNormals);
  211. var vertexStride = encoding.getStride();
  212. var vertexIndex = vertexBufferIndex / vertexStride;
  213. var north = rectangle.north;
  214. var south = rectangle.south;
  215. var east = rectangle.east;
  216. var west = rectangle.west;
  217. if (east < west) {
  218. east += _Math.CesiumMath.TWO_PI;
  219. }
  220. for (var i = start; i !== end; i += increment) {
  221. var index = edgeVertices[i];
  222. var h = heights[index];
  223. var uv = uvs[index];
  224. cartographicScratch.longitude = _Math.CesiumMath.lerp(west, east, uv.x) + longitudeOffset;
  225. cartographicScratch.latitude = _Math.CesiumMath.lerp(south, north, uv.y) + latitudeOffset;
  226. cartographicScratch.height = h - skirtLength;
  227. var position = ellipsoid.cartographicToCartesian(cartographicScratch, cartesian3Scratch);
  228. if (hasVertexNormals) {
  229. var n = index * 2.0;
  230. toPack.x = octEncodedNormals[n];
  231. toPack.y = octEncodedNormals[n + 1];
  232. if (exaggeration !== 1.0) {
  233. var normal = AttributeCompression.AttributeCompression.octDecode(toPack.x, toPack.y, scratchNormal);
  234. var fromENUNormal = Transforms.Transforms.eastNorthUpToFixedFrame(cartesian3Scratch, ellipsoid, scratchFromENU);
  235. var toENUNormal = Transforms.Matrix4.inverseTransformation(fromENUNormal, scratchToENU);
  236. Transforms.Matrix4.multiplyByPointAsVector(toENUNormal, normal, normal);
  237. normal.z *= exaggeration;
  238. Cartesian2.Cartesian3.normalize(normal, normal);
  239. Transforms.Matrix4.multiplyByPointAsVector(fromENUNormal, normal, normal);
  240. Cartesian2.Cartesian3.normalize(normal, normal);
  241. AttributeCompression.AttributeCompression.octEncode(normal, toPack);
  242. }
  243. }
  244. var webMercatorT;
  245. if (encoding.hasWebMercatorT) {
  246. webMercatorT = (WebMercatorProjection.WebMercatorProjection.geodeticLatitudeToMercatorAngle(cartographicScratch.latitude) - southMercatorY) * oneOverMercatorHeight;
  247. }
  248. vertexBufferIndex = encoding.encode(vertexBuffer, vertexBufferIndex, position, uv, cartographicScratch.height, toPack, webMercatorT);
  249. if (previousIndex !== -1) {
  250. indexBuffer[indexBufferIndex++] = previousIndex;
  251. indexBuffer[indexBufferIndex++] = vertexIndex - 1;
  252. indexBuffer[indexBufferIndex++] = index;
  253. indexBuffer[indexBufferIndex++] = vertexIndex - 1;
  254. indexBuffer[indexBufferIndex++] = vertexIndex;
  255. indexBuffer[indexBufferIndex++] = index;
  256. }
  257. previousIndex = index;
  258. ++vertexIndex;
  259. }
  260. return indexBufferIndex;
  261. }
  262. function copyAndSort(typedArray, comparator) {
  263. var copy;
  264. if (typeof typedArray.slice === 'function') {
  265. copy = typedArray.slice();
  266. if (typeof copy.sort !== 'function') {
  267. // Sliced typed array isn't sortable, so we can't use it.
  268. copy = undefined;
  269. }
  270. }
  271. if (!defined.defined(copy)) {
  272. copy = Array.prototype.slice.call(typedArray);
  273. }
  274. copy.sort(comparator);
  275. return copy;
  276. }
  277. var createVerticesFromQuantizedTerrainMesh$1 = createTaskProcessorWorker(createVerticesFromQuantizedTerrainMesh);
  278. return createVerticesFromQuantizedTerrainMesh$1;
  279. });