createVerticesFromQuantizedTerrainMesh.js 17 KB

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