TerrainEncoding-51b8b33b.js 42 KB

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