TerrainEncoding-74a00123.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './defaultValue-a6eb9f34', './AttributeCompression-8033f934', './ComponentDatatype-e06f4e16'], (function (exports, Transforms, Matrix2, RuntimeError, defaultValue, AttributeCompression, ComponentDatatype) { 'use strict';
  26. /**
  27. * Determine whether or not other objects are visible or hidden behind the visible horizon defined by
  28. * an {@link Ellipsoid} and a camera position. The ellipsoid is assumed to be located at the
  29. * origin of the coordinate system. This class uses the algorithm described in the
  30. * {@link https://cesium.com/blog/2013/04/25/Horizon-culling/|Horizon Culling} blog post.
  31. *
  32. * @alias EllipsoidalOccluder
  33. *
  34. * @param {Ellipsoid} ellipsoid The ellipsoid to use as an occluder.
  35. * @param {Cartesian3} [cameraPosition] The coordinate of the viewer/camera. If this parameter is not
  36. * specified, {@link EllipsoidalOccluder#cameraPosition} must be called before
  37. * testing visibility.
  38. *
  39. * @constructor
  40. *
  41. * @example
  42. * // Construct an ellipsoidal occluder with radii 1.0, 1.1, and 0.9.
  43. * const cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  44. * const occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  45. * const occluder = new Cesium.EllipsoidalOccluder(occluderEllipsoid, cameraPosition);
  46. *
  47. * @private
  48. */
  49. function EllipsoidalOccluder(ellipsoid, cameraPosition) {
  50. //>>includeStart('debug', pragmas.debug);
  51. RuntimeError.Check.typeOf.object("ellipsoid", ellipsoid);
  52. //>>includeEnd('debug');
  53. this._ellipsoid = ellipsoid;
  54. this._cameraPosition = new Matrix2.Cartesian3();
  55. this._cameraPositionInScaledSpace = new Matrix2.Cartesian3();
  56. this._distanceToLimbInScaledSpaceSquared = 0.0;
  57. // cameraPosition fills in the above values
  58. if (defaultValue.defined(cameraPosition)) {
  59. this.cameraPosition = cameraPosition;
  60. }
  61. }
  62. Object.defineProperties(EllipsoidalOccluder.prototype, {
  63. /**
  64. * Gets the occluding ellipsoid.
  65. * @memberof EllipsoidalOccluder.prototype
  66. * @type {Ellipsoid}
  67. */
  68. ellipsoid: {
  69. get: function () {
  70. return this._ellipsoid;
  71. },
  72. },
  73. /**
  74. * Gets or sets the position of the camera.
  75. * @memberof EllipsoidalOccluder.prototype
  76. * @type {Cartesian3}
  77. */
  78. cameraPosition: {
  79. get: function () {
  80. return this._cameraPosition;
  81. },
  82. set: function (cameraPosition) {
  83. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  84. const ellipsoid = this._ellipsoid;
  85. const cv = ellipsoid.transformPositionToScaledSpace(
  86. cameraPosition,
  87. this._cameraPositionInScaledSpace
  88. );
  89. const vhMagnitudeSquared = Matrix2.Cartesian3.magnitudeSquared(cv) - 1.0;
  90. Matrix2.Cartesian3.clone(cameraPosition, this._cameraPosition);
  91. this._cameraPositionInScaledSpace = cv;
  92. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  93. },
  94. },
  95. });
  96. const scratchCartesian = new Matrix2.Cartesian3();
  97. /**
  98. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  99. *
  100. * @param {Cartesian3} occludee The point to test for visibility.
  101. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  102. *
  103. * @example
  104. * const cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  105. * const ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  106. * const occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  107. * const point = new Cesium.Cartesian3(0, -3, -3);
  108. * occluder.isPointVisible(point); //returns true
  109. */
  110. EllipsoidalOccluder.prototype.isPointVisible = function (occludee) {
  111. const ellipsoid = this._ellipsoid;
  112. const occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(
  113. occludee,
  114. scratchCartesian
  115. );
  116. return isScaledSpacePointVisible(
  117. occludeeScaledSpacePosition,
  118. this._cameraPositionInScaledSpace,
  119. this._distanceToLimbInScaledSpaceSquared
  120. );
  121. };
  122. /**
  123. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  124. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  125. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  126. *
  127. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  128. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  129. *
  130. * @example
  131. * const cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  132. * const ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  133. * const occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  134. * const point = new Cesium.Cartesian3(0, -3, -3);
  135. * const scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  136. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  137. */
  138. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function (
  139. occludeeScaledSpacePosition
  140. ) {
  141. return isScaledSpacePointVisible(
  142. occludeeScaledSpacePosition,
  143. this._cameraPositionInScaledSpace,
  144. this._distanceToLimbInScaledSpaceSquared
  145. );
  146. };
  147. const scratchCameraPositionInScaledSpaceShrunk = new Matrix2.Cartesian3();
  148. /**
  149. * Similar to {@link EllipsoidalOccluder#isScaledSpacePointVisible} except tests against an
  150. * ellipsoid that has been shrunk by the minimum height when the minimum height is below
  151. * the ellipsoid. This is intended to be used with points generated by
  152. * {@link EllipsoidalOccluder#computeHorizonCullingPointPossiblyUnderEllipsoid} or
  153. * {@link EllipsoidalOccluder#computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid}.
  154. *
  155. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space of the possibly-shrunk ellipsoid.
  156. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  157. */
  158. EllipsoidalOccluder.prototype.isScaledSpacePointVisiblePossiblyUnderEllipsoid = function (
  159. occludeeScaledSpacePosition,
  160. minimumHeight
  161. ) {
  162. const ellipsoid = this._ellipsoid;
  163. let vhMagnitudeSquared;
  164. let cv;
  165. if (
  166. defaultValue.defined(minimumHeight) &&
  167. minimumHeight < 0.0 &&
  168. ellipsoid.minimumRadius > -minimumHeight
  169. ) {
  170. // This code is similar to the cameraPosition setter, but unrolled for performance because it will be called a lot.
  171. cv = scratchCameraPositionInScaledSpaceShrunk;
  172. cv.x = this._cameraPosition.x / (ellipsoid.radii.x + minimumHeight);
  173. cv.y = this._cameraPosition.y / (ellipsoid.radii.y + minimumHeight);
  174. cv.z = this._cameraPosition.z / (ellipsoid.radii.z + minimumHeight);
  175. vhMagnitudeSquared = cv.x * cv.x + cv.y * cv.y + cv.z * cv.z - 1.0;
  176. } else {
  177. cv = this._cameraPositionInScaledSpace;
  178. vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  179. }
  180. return isScaledSpacePointVisible(
  181. occludeeScaledSpacePosition,
  182. cv,
  183. vhMagnitudeSquared
  184. );
  185. };
  186. /**
  187. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  188. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  189. * is expressed in the ellipsoid-scaled space and is suitable for use with
  190. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  191. *
  192. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  193. * A reasonable direction to use is the direction from the center of the ellipsoid to
  194. * the center of the bounding sphere computed from the positions. The direction need not
  195. * be normalized.
  196. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  197. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  198. * ellipsoid's axes.
  199. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  200. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  201. */
  202. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function (
  203. directionToPoint,
  204. positions,
  205. result
  206. ) {
  207. return computeHorizonCullingPointFromPositions(
  208. this._ellipsoid,
  209. directionToPoint,
  210. positions,
  211. result
  212. );
  213. };
  214. const scratchEllipsoidShrunk = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  215. /**
  216. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPoint} except computes the culling
  217. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  218. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  219. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  220. *
  221. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  222. * A reasonable direction to use is the direction from the center of the ellipsoid to
  223. * the center of the bounding sphere computed from the positions. The direction need not
  224. * be normalized.
  225. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  226. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  227. * ellipsoid's axes.
  228. * @param {Number} [minimumHeight] The minimum height of all positions. If this value is undefined, all positions are assumed to be above the ellipsoid.
  229. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  230. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  231. */
  232. EllipsoidalOccluder.prototype.computeHorizonCullingPointPossiblyUnderEllipsoid = function (
  233. directionToPoint,
  234. positions,
  235. minimumHeight,
  236. result
  237. ) {
  238. const possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(
  239. this._ellipsoid,
  240. minimumHeight,
  241. scratchEllipsoidShrunk
  242. );
  243. return computeHorizonCullingPointFromPositions(
  244. possiblyShrunkEllipsoid,
  245. directionToPoint,
  246. positions,
  247. result
  248. );
  249. };
  250. /**
  251. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  252. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  253. * is expressed in the ellipsoid-scaled space and is suitable for use with
  254. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  255. *
  256. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  257. * A reasonable direction to use is the direction from the center of the ellipsoid to
  258. * the center of the bounding sphere computed from the positions. The direction need not
  259. * be normalized.
  260. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  261. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  262. * ellipsoid's axes.
  263. * @param {Number} [stride=3]
  264. * @param {Cartesian3} [center=Cartesian3.ZERO]
  265. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  266. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  267. */
  268. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function (
  269. directionToPoint,
  270. vertices,
  271. stride,
  272. center,
  273. result
  274. ) {
  275. return computeHorizonCullingPointFromVertices(
  276. this._ellipsoid,
  277. directionToPoint,
  278. vertices,
  279. stride,
  280. center,
  281. result
  282. );
  283. };
  284. /**
  285. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPointFromVertices} except computes the culling
  286. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  287. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  288. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  289. *
  290. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  291. * A reasonable direction to use is the direction from the center of the ellipsoid to
  292. * the center of the bounding sphere computed from the positions. The direction need not
  293. * be normalized.
  294. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  295. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  296. * ellipsoid's axes.
  297. * @param {Number} [stride=3]
  298. * @param {Cartesian3} [center=Cartesian3.ZERO]
  299. * @param {Number} [minimumHeight] The minimum height of all vertices. If this value is undefined, all vertices are assumed to be above the ellipsoid.
  300. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  301. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  302. */
  303. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid = function (
  304. directionToPoint,
  305. vertices,
  306. stride,
  307. center,
  308. minimumHeight,
  309. result
  310. ) {
  311. const possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(
  312. this._ellipsoid,
  313. minimumHeight,
  314. scratchEllipsoidShrunk
  315. );
  316. return computeHorizonCullingPointFromVertices(
  317. possiblyShrunkEllipsoid,
  318. directionToPoint,
  319. vertices,
  320. stride,
  321. center,
  322. result
  323. );
  324. };
  325. const subsampleScratch = [];
  326. /**
  327. * Computes a point that can be used for horizon culling of a rectangle. If the point is below
  328. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  329. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  330. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  331. *
  332. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  333. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  334. * the ellipsoid used by this instance for occlusion testing.
  335. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  336. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  337. */
  338. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function (
  339. rectangle,
  340. ellipsoid,
  341. result
  342. ) {
  343. //>>includeStart('debug', pragmas.debug);
  344. RuntimeError.Check.typeOf.object("rectangle", rectangle);
  345. //>>includeEnd('debug');
  346. const positions = Matrix2.Rectangle.subsample(
  347. rectangle,
  348. ellipsoid,
  349. 0.0,
  350. subsampleScratch
  351. );
  352. const bs = Transforms.BoundingSphere.fromPoints(positions);
  353. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  354. // sense to try to horizon cull it.
  355. if (Matrix2.Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  356. return undefined;
  357. }
  358. return this.computeHorizonCullingPoint(bs.center, positions, result);
  359. };
  360. const scratchEllipsoidShrunkRadii = new Matrix2.Cartesian3();
  361. function getPossiblyShrunkEllipsoid(ellipsoid, minimumHeight, result) {
  362. if (
  363. defaultValue.defined(minimumHeight) &&
  364. minimumHeight < 0.0 &&
  365. ellipsoid.minimumRadius > -minimumHeight
  366. ) {
  367. const ellipsoidShrunkRadii = Matrix2.Cartesian3.fromElements(
  368. ellipsoid.radii.x + minimumHeight,
  369. ellipsoid.radii.y + minimumHeight,
  370. ellipsoid.radii.z + minimumHeight,
  371. scratchEllipsoidShrunkRadii
  372. );
  373. ellipsoid = Matrix2.Ellipsoid.fromCartesian3(ellipsoidShrunkRadii, result);
  374. }
  375. return ellipsoid;
  376. }
  377. function computeHorizonCullingPointFromPositions(
  378. ellipsoid,
  379. directionToPoint,
  380. positions,
  381. result
  382. ) {
  383. //>>includeStart('debug', pragmas.debug);
  384. RuntimeError.Check.typeOf.object("directionToPoint", directionToPoint);
  385. RuntimeError.Check.defined("positions", positions);
  386. //>>includeEnd('debug');
  387. if (!defaultValue.defined(result)) {
  388. result = new Matrix2.Cartesian3();
  389. }
  390. const scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(
  391. ellipsoid,
  392. directionToPoint
  393. );
  394. let resultMagnitude = 0.0;
  395. for (let i = 0, len = positions.length; i < len; ++i) {
  396. const position = positions[i];
  397. const candidateMagnitude = computeMagnitude(
  398. ellipsoid,
  399. position,
  400. scaledSpaceDirectionToPoint
  401. );
  402. if (candidateMagnitude < 0.0) {
  403. // all points should face the same direction, but this one doesn't, so return undefined
  404. return undefined;
  405. }
  406. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  407. }
  408. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  409. }
  410. const positionScratch = new Matrix2.Cartesian3();
  411. function computeHorizonCullingPointFromVertices(
  412. ellipsoid,
  413. directionToPoint,
  414. vertices,
  415. stride,
  416. center,
  417. result
  418. ) {
  419. //>>includeStart('debug', pragmas.debug);
  420. RuntimeError.Check.typeOf.object("directionToPoint", directionToPoint);
  421. RuntimeError.Check.defined("vertices", vertices);
  422. RuntimeError.Check.typeOf.number("stride", stride);
  423. //>>includeEnd('debug');
  424. if (!defaultValue.defined(result)) {
  425. result = new Matrix2.Cartesian3();
  426. }
  427. stride = defaultValue.defaultValue(stride, 3);
  428. center = defaultValue.defaultValue(center, Matrix2.Cartesian3.ZERO);
  429. const scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(
  430. ellipsoid,
  431. directionToPoint
  432. );
  433. let resultMagnitude = 0.0;
  434. for (let i = 0, len = vertices.length; i < len; i += stride) {
  435. positionScratch.x = vertices[i] + center.x;
  436. positionScratch.y = vertices[i + 1] + center.y;
  437. positionScratch.z = vertices[i + 2] + center.z;
  438. const candidateMagnitude = computeMagnitude(
  439. ellipsoid,
  440. positionScratch,
  441. scaledSpaceDirectionToPoint
  442. );
  443. if (candidateMagnitude < 0.0) {
  444. // all points should face the same direction, but this one doesn't, so return undefined
  445. return undefined;
  446. }
  447. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  448. }
  449. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  450. }
  451. function isScaledSpacePointVisible(
  452. occludeeScaledSpacePosition,
  453. cameraPositionInScaledSpace,
  454. distanceToLimbInScaledSpaceSquared
  455. ) {
  456. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  457. const cv = cameraPositionInScaledSpace;
  458. const vhMagnitudeSquared = distanceToLimbInScaledSpaceSquared;
  459. const vt = Matrix2.Cartesian3.subtract(
  460. occludeeScaledSpacePosition,
  461. cv,
  462. scratchCartesian
  463. );
  464. const vtDotVc = -Matrix2.Cartesian3.dot(vt, cv);
  465. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  466. // in this case, set the culling plane to be on V.
  467. const isOccluded =
  468. vhMagnitudeSquared < 0
  469. ? vtDotVc > 0
  470. : vtDotVc > vhMagnitudeSquared &&
  471. (vtDotVc * vtDotVc) / Matrix2.Cartesian3.magnitudeSquared(vt) >
  472. vhMagnitudeSquared;
  473. return !isOccluded;
  474. }
  475. const scaledSpaceScratch = new Matrix2.Cartesian3();
  476. const directionScratch = new Matrix2.Cartesian3();
  477. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  478. const scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(
  479. position,
  480. scaledSpaceScratch
  481. );
  482. let magnitudeSquared = Matrix2.Cartesian3.magnitudeSquared(scaledSpacePosition);
  483. let magnitude = Math.sqrt(magnitudeSquared);
  484. const direction = Matrix2.Cartesian3.divideByScalar(
  485. scaledSpacePosition,
  486. magnitude,
  487. directionScratch
  488. );
  489. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  490. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  491. magnitude = Math.max(1.0, magnitude);
  492. const cosAlpha = Matrix2.Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  493. const sinAlpha = Matrix2.Cartesian3.magnitude(
  494. Matrix2.Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction)
  495. );
  496. const cosBeta = 1.0 / magnitude;
  497. const sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  498. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  499. }
  500. function magnitudeToPoint(
  501. scaledSpaceDirectionToPoint,
  502. resultMagnitude,
  503. result
  504. ) {
  505. // The horizon culling point is undefined if there were no positions from which to compute it,
  506. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  507. if (
  508. resultMagnitude <= 0.0 ||
  509. resultMagnitude === 1.0 / 0.0 ||
  510. resultMagnitude !== resultMagnitude
  511. ) {
  512. return undefined;
  513. }
  514. return Matrix2.Cartesian3.multiplyByScalar(
  515. scaledSpaceDirectionToPoint,
  516. resultMagnitude,
  517. result
  518. );
  519. }
  520. const directionToPointScratch = new Matrix2.Cartesian3();
  521. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  522. if (Matrix2.Cartesian3.equals(directionToPoint, Matrix2.Cartesian3.ZERO)) {
  523. return directionToPoint;
  524. }
  525. ellipsoid.transformPositionToScaledSpace(
  526. directionToPoint,
  527. directionToPointScratch
  528. );
  529. return Matrix2.Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  530. }
  531. /**
  532. * @private
  533. */
  534. const TerrainExaggeration = {};
  535. /**
  536. * Scales a height relative to an offset.
  537. *
  538. * @param {Number} height The height.
  539. * @param {Number} scale A scalar used to exaggerate the terrain. If the value is 1.0 there will be no effect.
  540. * @param {Number} relativeHeight The height relative to which terrain is exaggerated. If the value is 0.0 terrain will be exaggerated relative to the ellipsoid surface.
  541. */
  542. TerrainExaggeration.getHeight = function (height, scale, relativeHeight) {
  543. return (height - relativeHeight) * scale + relativeHeight;
  544. };
  545. const scratchCartographic = new Matrix2.Cartesian3();
  546. /**
  547. * Scales a position by exaggeration.
  548. */
  549. TerrainExaggeration.getPosition = function (
  550. position,
  551. ellipsoid,
  552. terrainExaggeration,
  553. terrainExaggerationRelativeHeight,
  554. result
  555. ) {
  556. const cartographic = ellipsoid.cartesianToCartographic(
  557. position,
  558. scratchCartographic
  559. );
  560. const newHeight = TerrainExaggeration.getHeight(
  561. cartographic.height,
  562. terrainExaggeration,
  563. terrainExaggerationRelativeHeight
  564. );
  565. return Matrix2.Cartesian3.fromRadians(
  566. cartographic.longitude,
  567. cartographic.latitude,
  568. newHeight,
  569. ellipsoid,
  570. result
  571. );
  572. };
  573. var TerrainExaggeration$1 = TerrainExaggeration;
  574. /**
  575. * This enumerated type is used to determine how the vertices of the terrain mesh are compressed.
  576. *
  577. * @enum {Number}
  578. *
  579. * @private
  580. */
  581. const TerrainQuantization = {
  582. /**
  583. * The vertices are not compressed.
  584. *
  585. * @type {Number}
  586. * @constant
  587. */
  588. NONE: 0,
  589. /**
  590. * The vertices are compressed to 12 bits.
  591. *
  592. * @type {Number}
  593. * @constant
  594. */
  595. BITS12: 1,
  596. };
  597. var TerrainQuantization$1 = Object.freeze(TerrainQuantization);
  598. const cartesian3Scratch = new Matrix2.Cartesian3();
  599. const cartesian3DimScratch = new Matrix2.Cartesian3();
  600. const cartesian2Scratch = new Matrix2.Cartesian2();
  601. const matrix4Scratch = new Matrix2.Matrix4();
  602. const matrix4Scratch2 = new Matrix2.Matrix4();
  603. const SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
  604. /**
  605. * Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
  606. * are unpacked in the vertex shader.
  607. *
  608. * @alias TerrainEncoding
  609. * @constructor
  610. *
  611. * @param {Cartesian3} center The center point of the vertices.
  612. * @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
  613. * @param {Number} minimumHeight The minimum height.
  614. * @param {Number} maximumHeight The maximum height.
  615. * @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
  616. * @param {Boolean} hasVertexNormals If the mesh has vertex normals.
  617. * @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
  618. * @param {Boolean} [hasGeodeticSurfaceNormals=false] true if the terrain data includes geodetic surface normals; otherwise, false.
  619. * @param {Number} [exaggeration=1.0] A scalar used to exaggerate terrain.
  620. * @param {Number} [exaggerationRelativeHeight=0.0] The relative height from which terrain is exaggerated.
  621. *
  622. * @private
  623. */
  624. function TerrainEncoding(
  625. center,
  626. axisAlignedBoundingBox,
  627. minimumHeight,
  628. maximumHeight,
  629. fromENU,
  630. hasVertexNormals,
  631. hasWebMercatorT,
  632. hasGeodeticSurfaceNormals,
  633. exaggeration,
  634. exaggerationRelativeHeight
  635. ) {
  636. let quantization = TerrainQuantization$1.NONE;
  637. let toENU;
  638. let matrix;
  639. if (
  640. defaultValue.defined(axisAlignedBoundingBox) &&
  641. defaultValue.defined(minimumHeight) &&
  642. defaultValue.defined(maximumHeight) &&
  643. defaultValue.defined(fromENU)
  644. ) {
  645. const minimum = axisAlignedBoundingBox.minimum;
  646. const maximum = axisAlignedBoundingBox.maximum;
  647. const dimensions = Matrix2.Cartesian3.subtract(
  648. maximum,
  649. minimum,
  650. cartesian3DimScratch
  651. );
  652. const hDim = maximumHeight - minimumHeight;
  653. const maxDim = Math.max(Matrix2.Cartesian3.maximumComponent(dimensions), hDim);
  654. if (maxDim < SHIFT_LEFT_12 - 1.0) {
  655. quantization = TerrainQuantization$1.BITS12;
  656. } else {
  657. quantization = TerrainQuantization$1.NONE;
  658. }
  659. toENU = Matrix2.Matrix4.inverseTransformation(fromENU, new Matrix2.Matrix4());
  660. const translation = Matrix2.Cartesian3.negate(minimum, cartesian3Scratch);
  661. Matrix2.Matrix4.multiply(
  662. Matrix2.Matrix4.fromTranslation(translation, matrix4Scratch),
  663. toENU,
  664. toENU
  665. );
  666. const scale = cartesian3Scratch;
  667. scale.x = 1.0 / dimensions.x;
  668. scale.y = 1.0 / dimensions.y;
  669. scale.z = 1.0 / dimensions.z;
  670. Matrix2.Matrix4.multiply(Matrix2.Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
  671. matrix = Matrix2.Matrix4.clone(fromENU);
  672. Matrix2.Matrix4.setTranslation(matrix, Matrix2.Cartesian3.ZERO, matrix);
  673. fromENU = Matrix2.Matrix4.clone(fromENU, new Matrix2.Matrix4());
  674. const translationMatrix = Matrix2.Matrix4.fromTranslation(minimum, matrix4Scratch);
  675. const scaleMatrix = Matrix2.Matrix4.fromScale(dimensions, matrix4Scratch2);
  676. const st = Matrix2.Matrix4.multiply(translationMatrix, scaleMatrix, matrix4Scratch);
  677. Matrix2.Matrix4.multiply(fromENU, st, fromENU);
  678. Matrix2.Matrix4.multiply(matrix, st, matrix);
  679. }
  680. /**
  681. * How the vertices of the mesh were compressed.
  682. * @type {TerrainQuantization}
  683. */
  684. this.quantization = quantization;
  685. /**
  686. * The minimum height of the tile including the skirts.
  687. * @type {Number}
  688. */
  689. this.minimumHeight = minimumHeight;
  690. /**
  691. * The maximum height of the tile.
  692. * @type {Number}
  693. */
  694. this.maximumHeight = maximumHeight;
  695. /**
  696. * The center of the tile.
  697. * @type {Cartesian3}
  698. */
  699. this.center = Matrix2.Cartesian3.clone(center);
  700. /**
  701. * A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
  702. * it so each component is in the [0, 1] range.
  703. * @type {Matrix4}
  704. */
  705. this.toScaledENU = toENU;
  706. /**
  707. * A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
  708. * @type {Matrix4}
  709. */
  710. this.fromScaledENU = fromENU;
  711. /**
  712. * The matrix used to decompress the terrain vertices in the shader for RTE rendering.
  713. * @type {Matrix4}
  714. */
  715. this.matrix = matrix;
  716. /**
  717. * The terrain mesh contains normals.
  718. * @type {Boolean}
  719. */
  720. this.hasVertexNormals = hasVertexNormals;
  721. /**
  722. * The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
  723. * @type {Boolean}
  724. */
  725. this.hasWebMercatorT = defaultValue.defaultValue(hasWebMercatorT, false);
  726. /**
  727. * The terrain mesh contains geodetic surface normals, used for terrain exaggeration.
  728. * @type {Boolean}
  729. */
  730. this.hasGeodeticSurfaceNormals = defaultValue.defaultValue(
  731. hasGeodeticSurfaceNormals,
  732. false
  733. );
  734. /**
  735. * A scalar used to exaggerate terrain.
  736. * @type {Number}
  737. */
  738. this.exaggeration = defaultValue.defaultValue(exaggeration, 1.0);
  739. /**
  740. * The relative height from which terrain is exaggerated.
  741. */
  742. this.exaggerationRelativeHeight = defaultValue.defaultValue(
  743. exaggerationRelativeHeight,
  744. 0.0
  745. );
  746. /**
  747. * The number of components in each vertex. This value can differ with different quantizations.
  748. * @type {Number}
  749. */
  750. this.stride = 0;
  751. this._offsetGeodeticSurfaceNormal = 0;
  752. this._offsetVertexNormal = 0;
  753. // Calculate the stride and offsets declared above
  754. this._calculateStrideAndOffsets();
  755. }
  756. TerrainEncoding.prototype.encode = function (
  757. vertexBuffer,
  758. bufferIndex,
  759. position,
  760. uv,
  761. height,
  762. normalToPack,
  763. webMercatorT,
  764. geodeticSurfaceNormal
  765. ) {
  766. const u = uv.x;
  767. const v = uv.y;
  768. if (this.quantization === TerrainQuantization$1.BITS12) {
  769. position = Matrix2.Matrix4.multiplyByPoint(
  770. this.toScaledENU,
  771. position,
  772. cartesian3Scratch
  773. );
  774. position.x = ComponentDatatype.CesiumMath.clamp(position.x, 0.0, 1.0);
  775. position.y = ComponentDatatype.CesiumMath.clamp(position.y, 0.0, 1.0);
  776. position.z = ComponentDatatype.CesiumMath.clamp(position.z, 0.0, 1.0);
  777. const hDim = this.maximumHeight - this.minimumHeight;
  778. const h = ComponentDatatype.CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
  779. Matrix2.Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
  780. const compressed0 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  781. cartesian2Scratch
  782. );
  783. Matrix2.Cartesian2.fromElements(position.z, h, cartesian2Scratch);
  784. const compressed1 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  785. cartesian2Scratch
  786. );
  787. Matrix2.Cartesian2.fromElements(u, v, cartesian2Scratch);
  788. const compressed2 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  789. cartesian2Scratch
  790. );
  791. vertexBuffer[bufferIndex++] = compressed0;
  792. vertexBuffer[bufferIndex++] = compressed1;
  793. vertexBuffer[bufferIndex++] = compressed2;
  794. if (this.hasWebMercatorT) {
  795. Matrix2.Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
  796. const compressed3 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  797. cartesian2Scratch
  798. );
  799. vertexBuffer[bufferIndex++] = compressed3;
  800. }
  801. } else {
  802. Matrix2.Cartesian3.subtract(position, this.center, cartesian3Scratch);
  803. vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
  804. vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
  805. vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
  806. vertexBuffer[bufferIndex++] = height;
  807. vertexBuffer[bufferIndex++] = u;
  808. vertexBuffer[bufferIndex++] = v;
  809. if (this.hasWebMercatorT) {
  810. vertexBuffer[bufferIndex++] = webMercatorT;
  811. }
  812. }
  813. if (this.hasVertexNormals) {
  814. vertexBuffer[bufferIndex++] = AttributeCompression.AttributeCompression.octPackFloat(
  815. normalToPack
  816. );
  817. }
  818. if (this.hasGeodeticSurfaceNormals) {
  819. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.x;
  820. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.y;
  821. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.z;
  822. }
  823. return bufferIndex;
  824. };
  825. const scratchPosition = new Matrix2.Cartesian3();
  826. const scratchGeodeticSurfaceNormal = new Matrix2.Cartesian3();
  827. TerrainEncoding.prototype.addGeodeticSurfaceNormals = function (
  828. oldBuffer,
  829. newBuffer,
  830. ellipsoid
  831. ) {
  832. if (this.hasGeodeticSurfaceNormals) {
  833. return;
  834. }
  835. const oldStride = this.stride;
  836. const vertexCount = oldBuffer.length / oldStride;
  837. this.hasGeodeticSurfaceNormals = true;
  838. this._calculateStrideAndOffsets();
  839. const newStride = this.stride;
  840. for (let index = 0; index < vertexCount; index++) {
  841. for (let offset = 0; offset < oldStride; offset++) {
  842. const oldIndex = index * oldStride + offset;
  843. const newIndex = index * newStride + offset;
  844. newBuffer[newIndex] = oldBuffer[oldIndex];
  845. }
  846. const position = this.decodePosition(newBuffer, index, scratchPosition);
  847. const geodeticSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  848. position,
  849. scratchGeodeticSurfaceNormal
  850. );
  851. const bufferIndex = index * newStride + this._offsetGeodeticSurfaceNormal;
  852. newBuffer[bufferIndex] = geodeticSurfaceNormal.x;
  853. newBuffer[bufferIndex + 1] = geodeticSurfaceNormal.y;
  854. newBuffer[bufferIndex + 2] = geodeticSurfaceNormal.z;
  855. }
  856. };
  857. TerrainEncoding.prototype.removeGeodeticSurfaceNormals = function (
  858. oldBuffer,
  859. newBuffer
  860. ) {
  861. if (!this.hasGeodeticSurfaceNormals) {
  862. return;
  863. }
  864. const oldStride = this.stride;
  865. const vertexCount = oldBuffer.length / oldStride;
  866. this.hasGeodeticSurfaceNormals = false;
  867. this._calculateStrideAndOffsets();
  868. const newStride = this.stride;
  869. for (let index = 0; index < vertexCount; index++) {
  870. for (let offset = 0; offset < newStride; offset++) {
  871. const oldIndex = index * oldStride + offset;
  872. const newIndex = index * newStride + offset;
  873. newBuffer[newIndex] = oldBuffer[oldIndex];
  874. }
  875. }
  876. };
  877. TerrainEncoding.prototype.decodePosition = function (buffer, index, result) {
  878. if (!defaultValue.defined(result)) {
  879. result = new Matrix2.Cartesian3();
  880. }
  881. index *= this.stride;
  882. if (this.quantization === TerrainQuantization$1.BITS12) {
  883. const xy = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  884. buffer[index],
  885. cartesian2Scratch
  886. );
  887. result.x = xy.x;
  888. result.y = xy.y;
  889. const zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  890. buffer[index + 1],
  891. cartesian2Scratch
  892. );
  893. result.z = zh.x;
  894. return Matrix2.Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
  895. }
  896. result.x = buffer[index];
  897. result.y = buffer[index + 1];
  898. result.z = buffer[index + 2];
  899. return Matrix2.Cartesian3.add(result, this.center, result);
  900. };
  901. TerrainEncoding.prototype.getExaggeratedPosition = function (
  902. buffer,
  903. index,
  904. result
  905. ) {
  906. result = this.decodePosition(buffer, index, result);
  907. const exaggeration = this.exaggeration;
  908. const exaggerationRelativeHeight = this.exaggerationRelativeHeight;
  909. const hasExaggeration = exaggeration !== 1.0;
  910. if (hasExaggeration && this.hasGeodeticSurfaceNormals) {
  911. const geodeticSurfaceNormal = this.decodeGeodeticSurfaceNormal(
  912. buffer,
  913. index,
  914. scratchGeodeticSurfaceNormal
  915. );
  916. const rawHeight = this.decodeHeight(buffer, index);
  917. const heightDifference =
  918. TerrainExaggeration$1.getHeight(
  919. rawHeight,
  920. exaggeration,
  921. exaggerationRelativeHeight
  922. ) - rawHeight;
  923. // some math is unrolled for better performance
  924. result.x += geodeticSurfaceNormal.x * heightDifference;
  925. result.y += geodeticSurfaceNormal.y * heightDifference;
  926. result.z += geodeticSurfaceNormal.z * heightDifference;
  927. }
  928. return result;
  929. };
  930. TerrainEncoding.prototype.decodeTextureCoordinates = function (
  931. buffer,
  932. index,
  933. result
  934. ) {
  935. if (!defaultValue.defined(result)) {
  936. result = new Matrix2.Cartesian2();
  937. }
  938. index *= this.stride;
  939. if (this.quantization === TerrainQuantization$1.BITS12) {
  940. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  941. buffer[index + 2],
  942. result
  943. );
  944. }
  945. return Matrix2.Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
  946. };
  947. TerrainEncoding.prototype.decodeHeight = function (buffer, index) {
  948. index *= this.stride;
  949. if (this.quantization === TerrainQuantization$1.BITS12) {
  950. const zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  951. buffer[index + 1],
  952. cartesian2Scratch
  953. );
  954. return (
  955. zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight
  956. );
  957. }
  958. return buffer[index + 3];
  959. };
  960. TerrainEncoding.prototype.decodeWebMercatorT = function (buffer, index) {
  961. index *= this.stride;
  962. if (this.quantization === TerrainQuantization$1.BITS12) {
  963. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  964. buffer[index + 3],
  965. cartesian2Scratch
  966. ).x;
  967. }
  968. return buffer[index + 6];
  969. };
  970. TerrainEncoding.prototype.getOctEncodedNormal = function (
  971. buffer,
  972. index,
  973. result
  974. ) {
  975. index = index * this.stride + this._offsetVertexNormal;
  976. const temp = buffer[index] / 256.0;
  977. const x = Math.floor(temp);
  978. const y = (temp - x) * 256.0;
  979. return Matrix2.Cartesian2.fromElements(x, y, result);
  980. };
  981. TerrainEncoding.prototype.decodeGeodeticSurfaceNormal = function (
  982. buffer,
  983. index,
  984. result
  985. ) {
  986. index = index * this.stride + this._offsetGeodeticSurfaceNormal;
  987. result.x = buffer[index];
  988. result.y = buffer[index + 1];
  989. result.z = buffer[index + 2];
  990. return result;
  991. };
  992. TerrainEncoding.prototype._calculateStrideAndOffsets = function () {
  993. let vertexStride = 0;
  994. switch (this.quantization) {
  995. case TerrainQuantization$1.BITS12:
  996. vertexStride += 3;
  997. break;
  998. default:
  999. vertexStride += 6;
  1000. }
  1001. if (this.hasWebMercatorT) {
  1002. vertexStride += 1;
  1003. }
  1004. if (this.hasVertexNormals) {
  1005. this._offsetVertexNormal = vertexStride;
  1006. vertexStride += 1;
  1007. }
  1008. if (this.hasGeodeticSurfaceNormals) {
  1009. this._offsetGeodeticSurfaceNormal = vertexStride;
  1010. vertexStride += 3;
  1011. }
  1012. this.stride = vertexStride;
  1013. };
  1014. const attributesIndicesNone = {
  1015. position3DAndHeight: 0,
  1016. textureCoordAndEncodedNormals: 1,
  1017. geodeticSurfaceNormal: 2,
  1018. };
  1019. const attributesIndicesBits12 = {
  1020. compressed0: 0,
  1021. compressed1: 1,
  1022. geodeticSurfaceNormal: 2,
  1023. };
  1024. TerrainEncoding.prototype.getAttributes = function (buffer) {
  1025. const datatype = ComponentDatatype.ComponentDatatype.FLOAT;
  1026. const sizeInBytes = ComponentDatatype.ComponentDatatype.getSizeInBytes(datatype);
  1027. const strideInBytes = this.stride * sizeInBytes;
  1028. let offsetInBytes = 0;
  1029. const attributes = [];
  1030. function addAttribute(index, componentsPerAttribute) {
  1031. attributes.push({
  1032. index: index,
  1033. vertexBuffer: buffer,
  1034. componentDatatype: datatype,
  1035. componentsPerAttribute: componentsPerAttribute,
  1036. offsetInBytes: offsetInBytes,
  1037. strideInBytes: strideInBytes,
  1038. });
  1039. offsetInBytes += componentsPerAttribute * sizeInBytes;
  1040. }
  1041. if (this.quantization === TerrainQuantization$1.NONE) {
  1042. addAttribute(attributesIndicesNone.position3DAndHeight, 4);
  1043. let componentsTexCoordAndNormals = 2;
  1044. componentsTexCoordAndNormals += this.hasWebMercatorT ? 1 : 0;
  1045. componentsTexCoordAndNormals += this.hasVertexNormals ? 1 : 0;
  1046. addAttribute(
  1047. attributesIndicesNone.textureCoordAndEncodedNormals,
  1048. componentsTexCoordAndNormals
  1049. );
  1050. if (this.hasGeodeticSurfaceNormals) {
  1051. addAttribute(attributesIndicesNone.geodeticSurfaceNormal, 3);
  1052. }
  1053. } else {
  1054. // When there is no webMercatorT or vertex normals, the attribute only needs 3 components: x/y, z/h, u/v.
  1055. // WebMercatorT and vertex normals each take up one component, so if only one of them is present the first
  1056. // attribute gets a 4th component. If both are present, we need an additional attribute that has 1 component.
  1057. const usingAttribute0Component4 =
  1058. this.hasWebMercatorT || this.hasVertexNormals;
  1059. const usingAttribute1Component1 =
  1060. this.hasWebMercatorT && this.hasVertexNormals;
  1061. addAttribute(
  1062. attributesIndicesBits12.compressed0,
  1063. usingAttribute0Component4 ? 4 : 3
  1064. );
  1065. if (usingAttribute1Component1) {
  1066. addAttribute(attributesIndicesBits12.compressed1, 1);
  1067. }
  1068. if (this.hasGeodeticSurfaceNormals) {
  1069. addAttribute(attributesIndicesBits12.geodeticSurfaceNormal, 3);
  1070. }
  1071. }
  1072. return attributes;
  1073. };
  1074. TerrainEncoding.prototype.getAttributeLocations = function () {
  1075. if (this.quantization === TerrainQuantization$1.NONE) {
  1076. return attributesIndicesNone;
  1077. }
  1078. return attributesIndicesBits12;
  1079. };
  1080. TerrainEncoding.clone = function (encoding, result) {
  1081. if (!defaultValue.defined(encoding)) {
  1082. return undefined;
  1083. }
  1084. if (!defaultValue.defined(result)) {
  1085. result = new TerrainEncoding();
  1086. }
  1087. result.quantization = encoding.quantization;
  1088. result.minimumHeight = encoding.minimumHeight;
  1089. result.maximumHeight = encoding.maximumHeight;
  1090. result.center = Matrix2.Cartesian3.clone(encoding.center);
  1091. result.toScaledENU = Matrix2.Matrix4.clone(encoding.toScaledENU);
  1092. result.fromScaledENU = Matrix2.Matrix4.clone(encoding.fromScaledENU);
  1093. result.matrix = Matrix2.Matrix4.clone(encoding.matrix);
  1094. result.hasVertexNormals = encoding.hasVertexNormals;
  1095. result.hasWebMercatorT = encoding.hasWebMercatorT;
  1096. result.hasGeodeticSurfaceNormals = encoding.hasGeodeticSurfaceNormals;
  1097. result.exaggeration = encoding.exaggeration;
  1098. result.exaggerationRelativeHeight = encoding.exaggerationRelativeHeight;
  1099. result._calculateStrideAndOffsets();
  1100. return result;
  1101. };
  1102. exports.EllipsoidalOccluder = EllipsoidalOccluder;
  1103. exports.TerrainEncoding = TerrainEncoding;
  1104. }));