upsampleQuantizedTerrainMesh.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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/main/LICENSE.md for full licensing details.
  22. */
  23. define(['./AttributeCompression-d0b97a83', './Transforms-f0a54c7b', './Matrix2-d35cf4b5', './defaultValue-81eec7ed', './TerrainEncoding-a8a1f120', './IndexDatatype-bed3935d', './RuntimeError-8952249c', './ComponentDatatype-9e86ac8f', './OrientedBoundingBox-0b41570b', './createTaskProcessorWorker', './_commonjsHelpers-3aae1032-26891ab7', './combine-3c023bda', './WebGLConstants-508b9636', './EllipsoidTangentPlane-2abe082d', './AxisAlignedBoundingBox-7b93960a', './IntersectionTests-a25e058d', './Plane-24f22488'], (function (AttributeCompression, Transforms, Matrix2, defaultValue, TerrainEncoding, IndexDatatype, RuntimeError, ComponentDatatype, OrientedBoundingBox, createTaskProcessorWorker, _commonjsHelpers3aae1032, combine, WebGLConstants, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane) { 'use strict';
  24. /**
  25. * Contains functions for operating on 2D triangles.
  26. *
  27. * @namespace Intersections2D
  28. */
  29. const Intersections2D = {};
  30. /**
  31. * Splits a 2D triangle at given axis-aligned threshold value and returns the resulting
  32. * polygon on a given side of the threshold. The resulting polygon may have 0, 1, 2,
  33. * 3, or 4 vertices.
  34. *
  35. * @param {Number} threshold The threshold coordinate value at which to clip the triangle.
  36. * @param {Boolean} keepAbove true to keep the portion of the triangle above the threshold, or false
  37. * to keep the portion below.
  38. * @param {Number} u0 The coordinate of the first vertex in the triangle, in counter-clockwise order.
  39. * @param {Number} u1 The coordinate of the second vertex in the triangle, in counter-clockwise order.
  40. * @param {Number} u2 The coordinate of the third vertex in the triangle, in counter-clockwise order.
  41. * @param {Number[]} [result] The array into which to copy the result. If this parameter is not supplied,
  42. * a new array is constructed and returned.
  43. * @returns {Number[]} The polygon that results after the clip, specified as a list of
  44. * vertices. The vertices are specified in counter-clockwise order.
  45. * Each vertex is either an index from the existing list (identified as
  46. * a 0, 1, or 2) or -1 indicating a new vertex not in the original triangle.
  47. * For new vertices, the -1 is followed by three additional numbers: the
  48. * index of each of the two original vertices forming the line segment that
  49. * the new vertex lies on, and the fraction of the distance from the first
  50. * vertex to the second one.
  51. *
  52. * @example
  53. * const result = Cesium.Intersections2D.clipTriangleAtAxisAlignedThreshold(0.5, false, 0.2, 0.6, 0.4);
  54. * // result === [2, 0, -1, 1, 0, 0.25, -1, 1, 2, 0.5]
  55. */
  56. Intersections2D.clipTriangleAtAxisAlignedThreshold = function (
  57. threshold,
  58. keepAbove,
  59. u0,
  60. u1,
  61. u2,
  62. result
  63. ) {
  64. //>>includeStart('debug', pragmas.debug);
  65. if (!defaultValue.defined(threshold)) {
  66. throw new RuntimeError.DeveloperError("threshold is required.");
  67. }
  68. if (!defaultValue.defined(keepAbove)) {
  69. throw new RuntimeError.DeveloperError("keepAbove is required.");
  70. }
  71. if (!defaultValue.defined(u0)) {
  72. throw new RuntimeError.DeveloperError("u0 is required.");
  73. }
  74. if (!defaultValue.defined(u1)) {
  75. throw new RuntimeError.DeveloperError("u1 is required.");
  76. }
  77. if (!defaultValue.defined(u2)) {
  78. throw new RuntimeError.DeveloperError("u2 is required.");
  79. }
  80. //>>includeEnd('debug');
  81. if (!defaultValue.defined(result)) {
  82. result = [];
  83. } else {
  84. result.length = 0;
  85. }
  86. let u0Behind;
  87. let u1Behind;
  88. let u2Behind;
  89. if (keepAbove) {
  90. u0Behind = u0 < threshold;
  91. u1Behind = u1 < threshold;
  92. u2Behind = u2 < threshold;
  93. } else {
  94. u0Behind = u0 > threshold;
  95. u1Behind = u1 > threshold;
  96. u2Behind = u2 > threshold;
  97. }
  98. const numBehind = u0Behind + u1Behind + u2Behind;
  99. let u01Ratio;
  100. let u02Ratio;
  101. let u12Ratio;
  102. let u10Ratio;
  103. let u20Ratio;
  104. let u21Ratio;
  105. if (numBehind === 1) {
  106. if (u0Behind) {
  107. u01Ratio = (threshold - u0) / (u1 - u0);
  108. u02Ratio = (threshold - u0) / (u2 - u0);
  109. result.push(1);
  110. result.push(2);
  111. if (u02Ratio !== 1.0) {
  112. result.push(-1);
  113. result.push(0);
  114. result.push(2);
  115. result.push(u02Ratio);
  116. }
  117. if (u01Ratio !== 1.0) {
  118. result.push(-1);
  119. result.push(0);
  120. result.push(1);
  121. result.push(u01Ratio);
  122. }
  123. } else if (u1Behind) {
  124. u12Ratio = (threshold - u1) / (u2 - u1);
  125. u10Ratio = (threshold - u1) / (u0 - u1);
  126. result.push(2);
  127. result.push(0);
  128. if (u10Ratio !== 1.0) {
  129. result.push(-1);
  130. result.push(1);
  131. result.push(0);
  132. result.push(u10Ratio);
  133. }
  134. if (u12Ratio !== 1.0) {
  135. result.push(-1);
  136. result.push(1);
  137. result.push(2);
  138. result.push(u12Ratio);
  139. }
  140. } else if (u2Behind) {
  141. u20Ratio = (threshold - u2) / (u0 - u2);
  142. u21Ratio = (threshold - u2) / (u1 - u2);
  143. result.push(0);
  144. result.push(1);
  145. if (u21Ratio !== 1.0) {
  146. result.push(-1);
  147. result.push(2);
  148. result.push(1);
  149. result.push(u21Ratio);
  150. }
  151. if (u20Ratio !== 1.0) {
  152. result.push(-1);
  153. result.push(2);
  154. result.push(0);
  155. result.push(u20Ratio);
  156. }
  157. }
  158. } else if (numBehind === 2) {
  159. if (!u0Behind && u0 !== threshold) {
  160. u10Ratio = (threshold - u1) / (u0 - u1);
  161. u20Ratio = (threshold - u2) / (u0 - u2);
  162. result.push(0);
  163. result.push(-1);
  164. result.push(1);
  165. result.push(0);
  166. result.push(u10Ratio);
  167. result.push(-1);
  168. result.push(2);
  169. result.push(0);
  170. result.push(u20Ratio);
  171. } else if (!u1Behind && u1 !== threshold) {
  172. u21Ratio = (threshold - u2) / (u1 - u2);
  173. u01Ratio = (threshold - u0) / (u1 - u0);
  174. result.push(1);
  175. result.push(-1);
  176. result.push(2);
  177. result.push(1);
  178. result.push(u21Ratio);
  179. result.push(-1);
  180. result.push(0);
  181. result.push(1);
  182. result.push(u01Ratio);
  183. } else if (!u2Behind && u2 !== threshold) {
  184. u02Ratio = (threshold - u0) / (u2 - u0);
  185. u12Ratio = (threshold - u1) / (u2 - u1);
  186. result.push(2);
  187. result.push(-1);
  188. result.push(0);
  189. result.push(2);
  190. result.push(u02Ratio);
  191. result.push(-1);
  192. result.push(1);
  193. result.push(2);
  194. result.push(u12Ratio);
  195. }
  196. } else if (numBehind !== 3) {
  197. // Completely in front of threshold
  198. result.push(0);
  199. result.push(1);
  200. result.push(2);
  201. }
  202. // else Completely behind threshold
  203. return result;
  204. };
  205. /**
  206. * Compute the barycentric coordinates of a 2D position within a 2D triangle.
  207. *
  208. * @param {Number} x The x coordinate of the position for which to find the barycentric coordinates.
  209. * @param {Number} y The y coordinate of the position for which to find the barycentric coordinates.
  210. * @param {Number} x1 The x coordinate of the triangle's first vertex.
  211. * @param {Number} y1 The y coordinate of the triangle's first vertex.
  212. * @param {Number} x2 The x coordinate of the triangle's second vertex.
  213. * @param {Number} y2 The y coordinate of the triangle's second vertex.
  214. * @param {Number} x3 The x coordinate of the triangle's third vertex.
  215. * @param {Number} y3 The y coordinate of the triangle's third vertex.
  216. * @param {Cartesian3} [result] The instance into to which to copy the result. If this parameter
  217. * is undefined, a new instance is created and returned.
  218. * @returns {Cartesian3} The barycentric coordinates of the position within the triangle.
  219. *
  220. * @example
  221. * const result = Cesium.Intersections2D.computeBarycentricCoordinates(0.0, 0.0, 0.0, 1.0, -1, -0.5, 1, -0.5);
  222. * // result === new Cesium.Cartesian3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0);
  223. */
  224. Intersections2D.computeBarycentricCoordinates = function (
  225. x,
  226. y,
  227. x1,
  228. y1,
  229. x2,
  230. y2,
  231. x3,
  232. y3,
  233. result
  234. ) {
  235. //>>includeStart('debug', pragmas.debug);
  236. if (!defaultValue.defined(x)) {
  237. throw new RuntimeError.DeveloperError("x is required.");
  238. }
  239. if (!defaultValue.defined(y)) {
  240. throw new RuntimeError.DeveloperError("y is required.");
  241. }
  242. if (!defaultValue.defined(x1)) {
  243. throw new RuntimeError.DeveloperError("x1 is required.");
  244. }
  245. if (!defaultValue.defined(y1)) {
  246. throw new RuntimeError.DeveloperError("y1 is required.");
  247. }
  248. if (!defaultValue.defined(x2)) {
  249. throw new RuntimeError.DeveloperError("x2 is required.");
  250. }
  251. if (!defaultValue.defined(y2)) {
  252. throw new RuntimeError.DeveloperError("y2 is required.");
  253. }
  254. if (!defaultValue.defined(x3)) {
  255. throw new RuntimeError.DeveloperError("x3 is required.");
  256. }
  257. if (!defaultValue.defined(y3)) {
  258. throw new RuntimeError.DeveloperError("y3 is required.");
  259. }
  260. //>>includeEnd('debug');
  261. const x1mx3 = x1 - x3;
  262. const x3mx2 = x3 - x2;
  263. const y2my3 = y2 - y3;
  264. const y1my3 = y1 - y3;
  265. const inverseDeterminant = 1.0 / (y2my3 * x1mx3 + x3mx2 * y1my3);
  266. const ymy3 = y - y3;
  267. const xmx3 = x - x3;
  268. const l1 = (y2my3 * xmx3 + x3mx2 * ymy3) * inverseDeterminant;
  269. const l2 = (-y1my3 * xmx3 + x1mx3 * ymy3) * inverseDeterminant;
  270. const l3 = 1.0 - l1 - l2;
  271. if (defaultValue.defined(result)) {
  272. result.x = l1;
  273. result.y = l2;
  274. result.z = l3;
  275. return result;
  276. }
  277. return new Matrix2.Cartesian3(l1, l2, l3);
  278. };
  279. /**
  280. * Compute the intersection between 2 line segments
  281. *
  282. * @param {Number} x00 The x coordinate of the first line's first vertex.
  283. * @param {Number} y00 The y coordinate of the first line's first vertex.
  284. * @param {Number} x01 The x coordinate of the first line's second vertex.
  285. * @param {Number} y01 The y coordinate of the first line's second vertex.
  286. * @param {Number} x10 The x coordinate of the second line's first vertex.
  287. * @param {Number} y10 The y coordinate of the second line's first vertex.
  288. * @param {Number} x11 The x coordinate of the second line's second vertex.
  289. * @param {Number} y11 The y coordinate of the second line's second vertex.
  290. * @param {Cartesian2} [result] The instance into to which to copy the result. If this parameter
  291. * is undefined, a new instance is created and returned.
  292. * @returns {Cartesian2} The intersection point, undefined if there is no intersection point or lines are coincident.
  293. *
  294. * @example
  295. * const result = Cesium.Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, -1, 1, 1, 1);
  296. * // result === new Cesium.Cartesian2(0.0, 1.0);
  297. */
  298. Intersections2D.computeLineSegmentLineSegmentIntersection = function (
  299. x00,
  300. y00,
  301. x01,
  302. y01,
  303. x10,
  304. y10,
  305. x11,
  306. y11,
  307. result
  308. ) {
  309. //>>includeStart('debug', pragmas.debug);
  310. RuntimeError.Check.typeOf.number("x00", x00);
  311. RuntimeError.Check.typeOf.number("y00", y00);
  312. RuntimeError.Check.typeOf.number("x01", x01);
  313. RuntimeError.Check.typeOf.number("y01", y01);
  314. RuntimeError.Check.typeOf.number("x10", x10);
  315. RuntimeError.Check.typeOf.number("y10", y10);
  316. RuntimeError.Check.typeOf.number("x11", x11);
  317. RuntimeError.Check.typeOf.number("y11", y11);
  318. //>>includeEnd('debug');
  319. const numerator1A = (x11 - x10) * (y00 - y10) - (y11 - y10) * (x00 - x10);
  320. const numerator1B = (x01 - x00) * (y00 - y10) - (y01 - y00) * (x00 - x10);
  321. const denominator1 = (y11 - y10) * (x01 - x00) - (x11 - x10) * (y01 - y00);
  322. // If denominator = 0, then lines are parallel. If denominator = 0 and both numerators are 0, then coincident
  323. if (denominator1 === 0) {
  324. return;
  325. }
  326. const ua1 = numerator1A / denominator1;
  327. const ub1 = numerator1B / denominator1;
  328. if (ua1 >= 0 && ua1 <= 1 && ub1 >= 0 && ub1 <= 1) {
  329. if (!defaultValue.defined(result)) {
  330. result = new Matrix2.Cartesian2();
  331. }
  332. result.x = x00 + ua1 * (x01 - x00);
  333. result.y = y00 + ua1 * (y01 - y00);
  334. return result;
  335. }
  336. };
  337. const maxShort = 32767;
  338. const halfMaxShort = (maxShort / 2) | 0;
  339. const clipScratch = [];
  340. const clipScratch2 = [];
  341. const verticesScratch = [];
  342. const cartographicScratch = new Matrix2.Cartographic();
  343. let cartesian3Scratch = new Matrix2.Cartesian3();
  344. const uScratch = [];
  345. const vScratch = [];
  346. const heightScratch = [];
  347. const indicesScratch = [];
  348. const normalsScratch = [];
  349. const horizonOcclusionPointScratch = new Matrix2.Cartesian3();
  350. const boundingSphereScratch = new Transforms.BoundingSphere();
  351. const orientedBoundingBoxScratch = new OrientedBoundingBox.OrientedBoundingBox();
  352. const decodeTexCoordsScratch = new Matrix2.Cartesian2();
  353. const octEncodedNormalScratch = new Matrix2.Cartesian3();
  354. function upsampleQuantizedTerrainMesh(parameters, transferableObjects) {
  355. const isEastChild = parameters.isEastChild;
  356. const isNorthChild = parameters.isNorthChild;
  357. const minU = isEastChild ? halfMaxShort : 0;
  358. const maxU = isEastChild ? maxShort : halfMaxShort;
  359. const minV = isNorthChild ? halfMaxShort : 0;
  360. const maxV = isNorthChild ? maxShort : halfMaxShort;
  361. const uBuffer = uScratch;
  362. const vBuffer = vScratch;
  363. const heightBuffer = heightScratch;
  364. const normalBuffer = normalsScratch;
  365. uBuffer.length = 0;
  366. vBuffer.length = 0;
  367. heightBuffer.length = 0;
  368. normalBuffer.length = 0;
  369. const indices = indicesScratch;
  370. indices.length = 0;
  371. const vertexMap = {};
  372. const parentVertices = parameters.vertices;
  373. let parentIndices = parameters.indices;
  374. parentIndices = parentIndices.subarray(0, parameters.indexCountWithoutSkirts);
  375. const encoding = TerrainEncoding.TerrainEncoding.clone(parameters.encoding);
  376. const hasVertexNormals = encoding.hasVertexNormals;
  377. let vertexCount = 0;
  378. const quantizedVertexCount = parameters.vertexCountWithoutSkirts;
  379. const parentMinimumHeight = parameters.minimumHeight;
  380. const parentMaximumHeight = parameters.maximumHeight;
  381. const parentUBuffer = new Array(quantizedVertexCount);
  382. const parentVBuffer = new Array(quantizedVertexCount);
  383. const parentHeightBuffer = new Array(quantizedVertexCount);
  384. const parentNormalBuffer = hasVertexNormals
  385. ? new Array(quantizedVertexCount * 2)
  386. : undefined;
  387. const threshold = 20;
  388. let height;
  389. let i, n;
  390. let u, v;
  391. for (i = 0, n = 0; i < quantizedVertexCount; ++i, n += 2) {
  392. const texCoords = encoding.decodeTextureCoordinates(
  393. parentVertices,
  394. i,
  395. decodeTexCoordsScratch
  396. );
  397. height = encoding.decodeHeight(parentVertices, i);
  398. u = ComponentDatatype.CesiumMath.clamp((texCoords.x * maxShort) | 0, 0, maxShort);
  399. v = ComponentDatatype.CesiumMath.clamp((texCoords.y * maxShort) | 0, 0, maxShort);
  400. parentHeightBuffer[i] = ComponentDatatype.CesiumMath.clamp(
  401. (((height - parentMinimumHeight) /
  402. (parentMaximumHeight - parentMinimumHeight)) *
  403. maxShort) |
  404. 0,
  405. 0,
  406. maxShort
  407. );
  408. if (u < threshold) {
  409. u = 0;
  410. }
  411. if (v < threshold) {
  412. v = 0;
  413. }
  414. if (maxShort - u < threshold) {
  415. u = maxShort;
  416. }
  417. if (maxShort - v < threshold) {
  418. v = maxShort;
  419. }
  420. parentUBuffer[i] = u;
  421. parentVBuffer[i] = v;
  422. if (hasVertexNormals) {
  423. const encodedNormal = encoding.getOctEncodedNormal(
  424. parentVertices,
  425. i,
  426. octEncodedNormalScratch
  427. );
  428. parentNormalBuffer[n] = encodedNormal.x;
  429. parentNormalBuffer[n + 1] = encodedNormal.y;
  430. }
  431. if (
  432. ((isEastChild && u >= halfMaxShort) ||
  433. (!isEastChild && u <= halfMaxShort)) &&
  434. ((isNorthChild && v >= halfMaxShort) ||
  435. (!isNorthChild && v <= halfMaxShort))
  436. ) {
  437. vertexMap[i] = vertexCount;
  438. uBuffer.push(u);
  439. vBuffer.push(v);
  440. heightBuffer.push(parentHeightBuffer[i]);
  441. if (hasVertexNormals) {
  442. normalBuffer.push(parentNormalBuffer[n]);
  443. normalBuffer.push(parentNormalBuffer[n + 1]);
  444. }
  445. ++vertexCount;
  446. }
  447. }
  448. const triangleVertices = [];
  449. triangleVertices.push(new Vertex());
  450. triangleVertices.push(new Vertex());
  451. triangleVertices.push(new Vertex());
  452. const clippedTriangleVertices = [];
  453. clippedTriangleVertices.push(new Vertex());
  454. clippedTriangleVertices.push(new Vertex());
  455. clippedTriangleVertices.push(new Vertex());
  456. let clippedIndex;
  457. let clipped2;
  458. for (i = 0; i < parentIndices.length; i += 3) {
  459. const i0 = parentIndices[i];
  460. const i1 = parentIndices[i + 1];
  461. const i2 = parentIndices[i + 2];
  462. const u0 = parentUBuffer[i0];
  463. const u1 = parentUBuffer[i1];
  464. const u2 = parentUBuffer[i2];
  465. triangleVertices[0].initializeIndexed(
  466. parentUBuffer,
  467. parentVBuffer,
  468. parentHeightBuffer,
  469. parentNormalBuffer,
  470. i0
  471. );
  472. triangleVertices[1].initializeIndexed(
  473. parentUBuffer,
  474. parentVBuffer,
  475. parentHeightBuffer,
  476. parentNormalBuffer,
  477. i1
  478. );
  479. triangleVertices[2].initializeIndexed(
  480. parentUBuffer,
  481. parentVBuffer,
  482. parentHeightBuffer,
  483. parentNormalBuffer,
  484. i2
  485. );
  486. // Clip triangle on the east-west boundary.
  487. const clipped = Intersections2D.clipTriangleAtAxisAlignedThreshold(
  488. halfMaxShort,
  489. isEastChild,
  490. u0,
  491. u1,
  492. u2,
  493. clipScratch
  494. );
  495. // Get the first clipped triangle, if any.
  496. clippedIndex = 0;
  497. if (clippedIndex >= clipped.length) {
  498. continue;
  499. }
  500. clippedIndex = clippedTriangleVertices[0].initializeFromClipResult(
  501. clipped,
  502. clippedIndex,
  503. triangleVertices
  504. );
  505. if (clippedIndex >= clipped.length) {
  506. continue;
  507. }
  508. clippedIndex = clippedTriangleVertices[1].initializeFromClipResult(
  509. clipped,
  510. clippedIndex,
  511. triangleVertices
  512. );
  513. if (clippedIndex >= clipped.length) {
  514. continue;
  515. }
  516. clippedIndex = clippedTriangleVertices[2].initializeFromClipResult(
  517. clipped,
  518. clippedIndex,
  519. triangleVertices
  520. );
  521. // Clip the triangle against the North-south boundary.
  522. clipped2 = Intersections2D.clipTriangleAtAxisAlignedThreshold(
  523. halfMaxShort,
  524. isNorthChild,
  525. clippedTriangleVertices[0].getV(),
  526. clippedTriangleVertices[1].getV(),
  527. clippedTriangleVertices[2].getV(),
  528. clipScratch2
  529. );
  530. addClippedPolygon(
  531. uBuffer,
  532. vBuffer,
  533. heightBuffer,
  534. normalBuffer,
  535. indices,
  536. vertexMap,
  537. clipped2,
  538. clippedTriangleVertices,
  539. hasVertexNormals
  540. );
  541. // If there's another vertex in the original clipped result,
  542. // it forms a second triangle. Clip it as well.
  543. if (clippedIndex < clipped.length) {
  544. clippedTriangleVertices[2].clone(clippedTriangleVertices[1]);
  545. clippedTriangleVertices[2].initializeFromClipResult(
  546. clipped,
  547. clippedIndex,
  548. triangleVertices
  549. );
  550. clipped2 = Intersections2D.clipTriangleAtAxisAlignedThreshold(
  551. halfMaxShort,
  552. isNorthChild,
  553. clippedTriangleVertices[0].getV(),
  554. clippedTriangleVertices[1].getV(),
  555. clippedTriangleVertices[2].getV(),
  556. clipScratch2
  557. );
  558. addClippedPolygon(
  559. uBuffer,
  560. vBuffer,
  561. heightBuffer,
  562. normalBuffer,
  563. indices,
  564. vertexMap,
  565. clipped2,
  566. clippedTriangleVertices,
  567. hasVertexNormals
  568. );
  569. }
  570. }
  571. const uOffset = isEastChild ? -maxShort : 0;
  572. const vOffset = isNorthChild ? -maxShort : 0;
  573. const westIndices = [];
  574. const southIndices = [];
  575. const eastIndices = [];
  576. const northIndices = [];
  577. let minimumHeight = Number.MAX_VALUE;
  578. let maximumHeight = -minimumHeight;
  579. const cartesianVertices = verticesScratch;
  580. cartesianVertices.length = 0;
  581. const ellipsoid = Matrix2.Ellipsoid.clone(parameters.ellipsoid);
  582. const rectangle = Matrix2.Rectangle.clone(parameters.childRectangle);
  583. const north = rectangle.north;
  584. const south = rectangle.south;
  585. let east = rectangle.east;
  586. const west = rectangle.west;
  587. if (east < west) {
  588. east += ComponentDatatype.CesiumMath.TWO_PI;
  589. }
  590. for (i = 0; i < uBuffer.length; ++i) {
  591. u = Math.round(uBuffer[i]);
  592. if (u <= minU) {
  593. westIndices.push(i);
  594. u = 0;
  595. } else if (u >= maxU) {
  596. eastIndices.push(i);
  597. u = maxShort;
  598. } else {
  599. u = u * 2 + uOffset;
  600. }
  601. uBuffer[i] = u;
  602. v = Math.round(vBuffer[i]);
  603. if (v <= minV) {
  604. southIndices.push(i);
  605. v = 0;
  606. } else if (v >= maxV) {
  607. northIndices.push(i);
  608. v = maxShort;
  609. } else {
  610. v = v * 2 + vOffset;
  611. }
  612. vBuffer[i] = v;
  613. height = ComponentDatatype.CesiumMath.lerp(
  614. parentMinimumHeight,
  615. parentMaximumHeight,
  616. heightBuffer[i] / maxShort
  617. );
  618. if (height < minimumHeight) {
  619. minimumHeight = height;
  620. }
  621. if (height > maximumHeight) {
  622. maximumHeight = height;
  623. }
  624. heightBuffer[i] = height;
  625. cartographicScratch.longitude = ComponentDatatype.CesiumMath.lerp(west, east, u / maxShort);
  626. cartographicScratch.latitude = ComponentDatatype.CesiumMath.lerp(south, north, v / maxShort);
  627. cartographicScratch.height = height;
  628. ellipsoid.cartographicToCartesian(cartographicScratch, cartesian3Scratch);
  629. cartesianVertices.push(cartesian3Scratch.x);
  630. cartesianVertices.push(cartesian3Scratch.y);
  631. cartesianVertices.push(cartesian3Scratch.z);
  632. }
  633. const boundingSphere = Transforms.BoundingSphere.fromVertices(
  634. cartesianVertices,
  635. Matrix2.Cartesian3.ZERO,
  636. 3,
  637. boundingSphereScratch
  638. );
  639. const orientedBoundingBox = OrientedBoundingBox.OrientedBoundingBox.fromRectangle(
  640. rectangle,
  641. minimumHeight,
  642. maximumHeight,
  643. ellipsoid,
  644. orientedBoundingBoxScratch
  645. );
  646. const occluder = new TerrainEncoding.EllipsoidalOccluder(ellipsoid);
  647. const horizonOcclusionPoint = occluder.computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid(
  648. boundingSphere.center,
  649. cartesianVertices,
  650. 3,
  651. boundingSphere.center,
  652. minimumHeight,
  653. horizonOcclusionPointScratch
  654. );
  655. const heightRange = maximumHeight - minimumHeight;
  656. const vertices = new Uint16Array(
  657. uBuffer.length + vBuffer.length + heightBuffer.length
  658. );
  659. for (i = 0; i < uBuffer.length; ++i) {
  660. vertices[i] = uBuffer[i];
  661. }
  662. let start = uBuffer.length;
  663. for (i = 0; i < vBuffer.length; ++i) {
  664. vertices[start + i] = vBuffer[i];
  665. }
  666. start += vBuffer.length;
  667. for (i = 0; i < heightBuffer.length; ++i) {
  668. vertices[start + i] =
  669. (maxShort * (heightBuffer[i] - minimumHeight)) / heightRange;
  670. }
  671. const indicesTypedArray = IndexDatatype.IndexDatatype.createTypedArray(
  672. uBuffer.length,
  673. indices
  674. );
  675. let encodedNormals;
  676. if (hasVertexNormals) {
  677. const normalArray = new Uint8Array(normalBuffer);
  678. transferableObjects.push(
  679. vertices.buffer,
  680. indicesTypedArray.buffer,
  681. normalArray.buffer
  682. );
  683. encodedNormals = normalArray.buffer;
  684. } else {
  685. transferableObjects.push(vertices.buffer, indicesTypedArray.buffer);
  686. }
  687. return {
  688. vertices: vertices.buffer,
  689. encodedNormals: encodedNormals,
  690. indices: indicesTypedArray.buffer,
  691. minimumHeight: minimumHeight,
  692. maximumHeight: maximumHeight,
  693. westIndices: westIndices,
  694. southIndices: southIndices,
  695. eastIndices: eastIndices,
  696. northIndices: northIndices,
  697. boundingSphere: boundingSphere,
  698. orientedBoundingBox: orientedBoundingBox,
  699. horizonOcclusionPoint: horizonOcclusionPoint,
  700. };
  701. }
  702. function Vertex() {
  703. this.vertexBuffer = undefined;
  704. this.index = undefined;
  705. this.first = undefined;
  706. this.second = undefined;
  707. this.ratio = undefined;
  708. }
  709. Vertex.prototype.clone = function (result) {
  710. if (!defaultValue.defined(result)) {
  711. result = new Vertex();
  712. }
  713. result.uBuffer = this.uBuffer;
  714. result.vBuffer = this.vBuffer;
  715. result.heightBuffer = this.heightBuffer;
  716. result.normalBuffer = this.normalBuffer;
  717. result.index = this.index;
  718. result.first = this.first;
  719. result.second = this.second;
  720. result.ratio = this.ratio;
  721. return result;
  722. };
  723. Vertex.prototype.initializeIndexed = function (
  724. uBuffer,
  725. vBuffer,
  726. heightBuffer,
  727. normalBuffer,
  728. index
  729. ) {
  730. this.uBuffer = uBuffer;
  731. this.vBuffer = vBuffer;
  732. this.heightBuffer = heightBuffer;
  733. this.normalBuffer = normalBuffer;
  734. this.index = index;
  735. this.first = undefined;
  736. this.second = undefined;
  737. this.ratio = undefined;
  738. };
  739. Vertex.prototype.initializeFromClipResult = function (
  740. clipResult,
  741. index,
  742. vertices
  743. ) {
  744. let nextIndex = index + 1;
  745. if (clipResult[index] !== -1) {
  746. vertices[clipResult[index]].clone(this);
  747. } else {
  748. this.vertexBuffer = undefined;
  749. this.index = undefined;
  750. this.first = vertices[clipResult[nextIndex]];
  751. ++nextIndex;
  752. this.second = vertices[clipResult[nextIndex]];
  753. ++nextIndex;
  754. this.ratio = clipResult[nextIndex];
  755. ++nextIndex;
  756. }
  757. return nextIndex;
  758. };
  759. Vertex.prototype.getKey = function () {
  760. if (this.isIndexed()) {
  761. return this.index;
  762. }
  763. return JSON.stringify({
  764. first: this.first.getKey(),
  765. second: this.second.getKey(),
  766. ratio: this.ratio,
  767. });
  768. };
  769. Vertex.prototype.isIndexed = function () {
  770. return defaultValue.defined(this.index);
  771. };
  772. Vertex.prototype.getH = function () {
  773. if (defaultValue.defined(this.index)) {
  774. return this.heightBuffer[this.index];
  775. }
  776. return ComponentDatatype.CesiumMath.lerp(this.first.getH(), this.second.getH(), this.ratio);
  777. };
  778. Vertex.prototype.getU = function () {
  779. if (defaultValue.defined(this.index)) {
  780. return this.uBuffer[this.index];
  781. }
  782. return ComponentDatatype.CesiumMath.lerp(this.first.getU(), this.second.getU(), this.ratio);
  783. };
  784. Vertex.prototype.getV = function () {
  785. if (defaultValue.defined(this.index)) {
  786. return this.vBuffer[this.index];
  787. }
  788. return ComponentDatatype.CesiumMath.lerp(this.first.getV(), this.second.getV(), this.ratio);
  789. };
  790. let encodedScratch = new Matrix2.Cartesian2();
  791. // An upsampled triangle may be clipped twice before it is assigned an index
  792. // In this case, we need a buffer to handle the recursion of getNormalX() and getNormalY().
  793. let depth = -1;
  794. const cartesianScratch1 = [new Matrix2.Cartesian3(), new Matrix2.Cartesian3()];
  795. const cartesianScratch2 = [new Matrix2.Cartesian3(), new Matrix2.Cartesian3()];
  796. function lerpOctEncodedNormal(vertex, result) {
  797. ++depth;
  798. let first = cartesianScratch1[depth];
  799. let second = cartesianScratch2[depth];
  800. first = AttributeCompression.AttributeCompression.octDecode(
  801. vertex.first.getNormalX(),
  802. vertex.first.getNormalY(),
  803. first
  804. );
  805. second = AttributeCompression.AttributeCompression.octDecode(
  806. vertex.second.getNormalX(),
  807. vertex.second.getNormalY(),
  808. second
  809. );
  810. cartesian3Scratch = Matrix2.Cartesian3.lerp(
  811. first,
  812. second,
  813. vertex.ratio,
  814. cartesian3Scratch
  815. );
  816. Matrix2.Cartesian3.normalize(cartesian3Scratch, cartesian3Scratch);
  817. AttributeCompression.AttributeCompression.octEncode(cartesian3Scratch, result);
  818. --depth;
  819. return result;
  820. }
  821. Vertex.prototype.getNormalX = function () {
  822. if (defaultValue.defined(this.index)) {
  823. return this.normalBuffer[this.index * 2];
  824. }
  825. encodedScratch = lerpOctEncodedNormal(this, encodedScratch);
  826. return encodedScratch.x;
  827. };
  828. Vertex.prototype.getNormalY = function () {
  829. if (defaultValue.defined(this.index)) {
  830. return this.normalBuffer[this.index * 2 + 1];
  831. }
  832. encodedScratch = lerpOctEncodedNormal(this, encodedScratch);
  833. return encodedScratch.y;
  834. };
  835. const polygonVertices = [];
  836. polygonVertices.push(new Vertex());
  837. polygonVertices.push(new Vertex());
  838. polygonVertices.push(new Vertex());
  839. polygonVertices.push(new Vertex());
  840. function addClippedPolygon(
  841. uBuffer,
  842. vBuffer,
  843. heightBuffer,
  844. normalBuffer,
  845. indices,
  846. vertexMap,
  847. clipped,
  848. triangleVertices,
  849. hasVertexNormals
  850. ) {
  851. if (clipped.length === 0) {
  852. return;
  853. }
  854. let numVertices = 0;
  855. let clippedIndex = 0;
  856. while (clippedIndex < clipped.length) {
  857. clippedIndex = polygonVertices[numVertices++].initializeFromClipResult(
  858. clipped,
  859. clippedIndex,
  860. triangleVertices
  861. );
  862. }
  863. for (let i = 0; i < numVertices; ++i) {
  864. const polygonVertex = polygonVertices[i];
  865. if (!polygonVertex.isIndexed()) {
  866. const key = polygonVertex.getKey();
  867. if (defaultValue.defined(vertexMap[key])) {
  868. polygonVertex.newIndex = vertexMap[key];
  869. } else {
  870. const newIndex = uBuffer.length;
  871. uBuffer.push(polygonVertex.getU());
  872. vBuffer.push(polygonVertex.getV());
  873. heightBuffer.push(polygonVertex.getH());
  874. if (hasVertexNormals) {
  875. normalBuffer.push(polygonVertex.getNormalX());
  876. normalBuffer.push(polygonVertex.getNormalY());
  877. }
  878. polygonVertex.newIndex = newIndex;
  879. vertexMap[key] = newIndex;
  880. }
  881. } else {
  882. polygonVertex.newIndex = vertexMap[polygonVertex.index];
  883. polygonVertex.uBuffer = uBuffer;
  884. polygonVertex.vBuffer = vBuffer;
  885. polygonVertex.heightBuffer = heightBuffer;
  886. if (hasVertexNormals) {
  887. polygonVertex.normalBuffer = normalBuffer;
  888. }
  889. }
  890. }
  891. if (numVertices === 3) {
  892. // A triangle.
  893. indices.push(polygonVertices[0].newIndex);
  894. indices.push(polygonVertices[1].newIndex);
  895. indices.push(polygonVertices[2].newIndex);
  896. } else if (numVertices === 4) {
  897. // A quad - two triangles.
  898. indices.push(polygonVertices[0].newIndex);
  899. indices.push(polygonVertices[1].newIndex);
  900. indices.push(polygonVertices[2].newIndex);
  901. indices.push(polygonVertices[0].newIndex);
  902. indices.push(polygonVertices[2].newIndex);
  903. indices.push(polygonVertices[3].newIndex);
  904. }
  905. }
  906. var upsampleQuantizedTerrainMesh$1 = createTaskProcessorWorker(upsampleQuantizedTerrainMesh);
  907. return upsampleQuantizedTerrainMesh$1;
  908. }));
  909. //# sourceMappingURL=upsampleQuantizedTerrainMesh.js.map