Newer
Older
XinYang_SanWei+RongYun / public / static / Cesium / Workers / createVectorTilePolylines.js
@raoxianxuan raoxianxuan on 21 Dec 2021 8 KB gis
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 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/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-f2a06374', './Cartesian2-16a61632', './WebGLConstants-4c11ee5f', './AttributeCompression-c177f997', './IndexDatatype-9435b55f', './createTaskProcessorWorker'], function (when, Check, _Math, Cartographic, Cartesian2, WebGLConstants, AttributeCompression, IndexDatatype, createTaskProcessorWorker) { 'use strict';
  24.  
  25. var maxShort = 32767;
  26.  
  27. var scratchBVCartographic = new Cartographic.Cartographic();
  28. var scratchEncodedPosition = new Cartographic.Cartesian3();
  29.  
  30. function decodePositions(positions, rectangle, minimumHeight, maximumHeight, ellipsoid) {
  31. var positionsLength = positions.length / 3;
  32. var uBuffer = positions.subarray(0, positionsLength);
  33. var vBuffer = positions.subarray(positionsLength, 2 * positionsLength);
  34. var heightBuffer = positions.subarray(2 * positionsLength, 3 * positionsLength);
  35. AttributeCompression.AttributeCompression.zigZagDeltaDecode(uBuffer, vBuffer, heightBuffer);
  36.  
  37. var decoded = new Float32Array(positions.length);
  38. for (var i = 0; i < positionsLength; ++i) {
  39. var u = uBuffer[i];
  40. var v = vBuffer[i];
  41. var h = heightBuffer[i];
  42.  
  43. var lon = _Math.CesiumMath.lerp(rectangle.west, rectangle.east, u / maxShort);
  44. var lat = _Math.CesiumMath.lerp(rectangle.south, rectangle.north, v / maxShort);
  45. var alt = _Math.CesiumMath.lerp(minimumHeight, maximumHeight, h / maxShort);
  46.  
  47. var cartographic = Cartographic.Cartographic.fromRadians(lon, lat, alt, scratchBVCartographic);
  48. var decodedPosition = ellipsoid.cartographicToCartesian(cartographic, scratchEncodedPosition);
  49. Cartographic.Cartesian3.pack(decodedPosition, decoded, i * 3);
  50. }
  51. return decoded;
  52. }
  53.  
  54. var scratchRectangle = new Cartesian2.Rectangle();
  55. var scratchEllipsoid = new Cartesian2.Ellipsoid();
  56. var scratchCenter = new Cartographic.Cartesian3();
  57. var scratchMinMaxHeights = {
  58. min : undefined,
  59. max : undefined
  60. };
  61.  
  62. function unpackBuffer(packedBuffer) {
  63. packedBuffer = new Float64Array(packedBuffer);
  64.  
  65. var offset = 0;
  66. scratchMinMaxHeights.min = packedBuffer[offset++];
  67. scratchMinMaxHeights.max = packedBuffer[offset++];
  68.  
  69. Cartesian2.Rectangle.unpack(packedBuffer, offset, scratchRectangle);
  70. offset += Cartesian2.Rectangle.packedLength;
  71.  
  72. Cartesian2.Ellipsoid.unpack(packedBuffer, offset, scratchEllipsoid);
  73. offset += Cartesian2.Ellipsoid.packedLength;
  74.  
  75. Cartographic.Cartesian3.unpack(packedBuffer, offset, scratchCenter);
  76. }
  77.  
  78. var scratchP0 = new Cartographic.Cartesian3();
  79. var scratchP1 = new Cartographic.Cartesian3();
  80. var scratchPrev = new Cartographic.Cartesian3();
  81. var scratchCur = new Cartographic.Cartesian3();
  82. var scratchNext = new Cartographic.Cartesian3();
  83.  
  84. function createVectorTilePolylines(parameters, transferableObjects) {
  85. var encodedPositions = new Uint16Array(parameters.positions);
  86. var widths = new Uint16Array(parameters.widths);
  87. var counts = new Uint32Array(parameters.counts);
  88. var batchIds = new Uint16Array(parameters.batchIds);
  89.  
  90. unpackBuffer(parameters.packedBuffer);
  91. var rectangle = scratchRectangle;
  92. var ellipsoid = scratchEllipsoid;
  93. var center = scratchCenter;
  94. var minimumHeight = scratchMinMaxHeights.min;
  95. var maximumHeight = scratchMinMaxHeights.max;
  96.  
  97. var positions = decodePositions(encodedPositions, rectangle, minimumHeight, maximumHeight, ellipsoid);
  98.  
  99. var positionsLength = positions.length / 3;
  100. var size = positionsLength * 4 - 4;
  101.  
  102. var curPositions = new Float32Array(size * 3);
  103. var prevPositions = new Float32Array(size * 3);
  104. var nextPositions = new Float32Array(size * 3);
  105. var expandAndWidth = new Float32Array(size * 2);
  106. var vertexBatchIds = new Uint16Array(size);
  107.  
  108. var positionIndex = 0;
  109. var expandAndWidthIndex = 0;
  110. var batchIdIndex = 0;
  111.  
  112. var i;
  113. var offset = 0;
  114. var length = counts.length;
  115.  
  116. for (i = 0; i < length; ++i) {
  117. var count = counts [i];
  118. var width = widths[i];
  119. var batchId = batchIds[i];
  120.  
  121. for (var j = 0; j < count; ++j) {
  122. var previous;
  123. if (j === 0) {
  124. var p0 = Cartographic.Cartesian3.unpack(positions, offset * 3, scratchP0);
  125. var p1 = Cartographic.Cartesian3.unpack(positions, (offset + 1) * 3, scratchP1);
  126.  
  127. previous = Cartographic.Cartesian3.subtract(p0, p1, scratchPrev);
  128. Cartographic.Cartesian3.add(p0, previous, previous);
  129. } else {
  130. previous = Cartographic.Cartesian3.unpack(positions, (offset + j - 1) * 3, scratchPrev);
  131. }
  132.  
  133. var current = Cartographic.Cartesian3.unpack(positions, (offset + j) * 3, scratchCur);
  134.  
  135. var next;
  136. if (j === count - 1) {
  137. var p2 = Cartographic.Cartesian3.unpack(positions, (offset + count - 1) * 3, scratchP0);
  138. var p3 = Cartographic.Cartesian3.unpack(positions, (offset + count - 2) * 3, scratchP1);
  139.  
  140. next = Cartographic.Cartesian3.subtract(p2, p3, scratchNext);
  141. Cartographic.Cartesian3.add(p2, next, next);
  142. } else {
  143. next = Cartographic.Cartesian3.unpack(positions, (offset + j + 1) * 3, scratchNext);
  144. }
  145.  
  146. Cartographic.Cartesian3.subtract(previous, center, previous);
  147. Cartographic.Cartesian3.subtract(current, center, current);
  148. Cartographic.Cartesian3.subtract(next, center, next);
  149.  
  150. var startK = j === 0 ? 2 : 0;
  151. var endK = j === count - 1 ? 2 : 4;
  152.  
  153. for (var k = startK; k < endK; ++k) {
  154. Cartographic.Cartesian3.pack(current, curPositions, positionIndex);
  155. Cartographic.Cartesian3.pack(previous, prevPositions, positionIndex);
  156. Cartographic.Cartesian3.pack(next, nextPositions, positionIndex);
  157. positionIndex += 3;
  158.  
  159. var direction = (k - 2 < 0) ? -1.0 : 1.0;
  160. expandAndWidth[expandAndWidthIndex++] = 2 * (k % 2) - 1;
  161. expandAndWidth[expandAndWidthIndex++] = direction * width;
  162.  
  163. vertexBatchIds[batchIdIndex++] = batchId;
  164. }
  165. }
  166.  
  167. offset += count;
  168. }
  169.  
  170. var indices = IndexDatatype.IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
  171. var index = 0;
  172. var indicesIndex = 0;
  173. length = positionsLength - 1;
  174. for (i = 0; i < length; ++i) {
  175. indices[indicesIndex++] = index;
  176. indices[indicesIndex++] = index + 2;
  177. indices[indicesIndex++] = index + 1;
  178.  
  179. indices[indicesIndex++] = index + 1;
  180. indices[indicesIndex++] = index + 2;
  181. indices[indicesIndex++] = index + 3;
  182.  
  183. index += 4;
  184. }
  185.  
  186. transferableObjects.push(curPositions.buffer, prevPositions.buffer, nextPositions.buffer);
  187. transferableObjects.push(expandAndWidth.buffer, vertexBatchIds.buffer, indices.buffer);
  188.  
  189. return {
  190. indexDatatype : (indices.BYTES_PER_ELEMENT === 2) ? IndexDatatype.IndexDatatype.UNSIGNED_SHORT : IndexDatatype.IndexDatatype.UNSIGNED_INT,
  191. currentPositions : curPositions.buffer,
  192. previousPositions : prevPositions.buffer,
  193. nextPositions : nextPositions.buffer,
  194. expandAndWidth : expandAndWidth.buffer,
  195. batchIds : vertexBatchIds.buffer,
  196. indices : indices.buffer
  197. };
  198. }
  199. var createVectorTilePolylines$1 = createTaskProcessorWorker(createVectorTilePolylines);
  200.  
  201. return createVectorTilePolylines$1;
  202.  
  203. });