TerrainEncoding-8fbe9124.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  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-273eeb44', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './AttributeCompression-f202be44', './ComponentDatatype-4eeb6d9b'], (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. /**
  574. * This enumerated type is used to determine how the vertices of the terrain mesh are compressed.
  575. *
  576. * @enum {Number}
  577. *
  578. * @private
  579. */
  580. const TerrainQuantization = {
  581. /**
  582. * The vertices are not compressed.
  583. *
  584. * @type {Number}
  585. * @constant
  586. */
  587. NONE: 0,
  588. /**
  589. * The vertices are compressed to 12 bits.
  590. *
  591. * @type {Number}
  592. * @constant
  593. */
  594. BITS12: 1,
  595. };
  596. var TerrainQuantization$1 = Object.freeze(TerrainQuantization);
  597. const cartesian3Scratch = new Matrix2.Cartesian3();
  598. const cartesian3DimScratch = new Matrix2.Cartesian3();
  599. const cartesian2Scratch = new Matrix2.Cartesian2();
  600. const matrix4Scratch = new Matrix2.Matrix4();
  601. const matrix4Scratch2 = new Matrix2.Matrix4();
  602. const SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
  603. /**
  604. * Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
  605. * are unpacked in the vertex shader.
  606. *
  607. * @alias TerrainEncoding
  608. * @constructor
  609. *
  610. * @param {Cartesian3} center The center point of the vertices.
  611. * @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
  612. * @param {Number} minimumHeight The minimum height.
  613. * @param {Number} maximumHeight The maximum height.
  614. * @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
  615. * @param {Boolean} hasVertexNormals If the mesh has vertex normals.
  616. * @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
  617. * @param {Boolean} [hasGeodeticSurfaceNormals=false] true if the terrain data includes geodetic surface normals; otherwise, false.
  618. * @param {Number} [exaggeration=1.0] A scalar used to exaggerate terrain.
  619. * @param {Number} [exaggerationRelativeHeight=0.0] The relative height from which terrain is exaggerated.
  620. *
  621. * @private
  622. */
  623. function TerrainEncoding(
  624. center,
  625. axisAlignedBoundingBox,
  626. minimumHeight,
  627. maximumHeight,
  628. fromENU,
  629. hasVertexNormals,
  630. hasWebMercatorT,
  631. hasGeodeticSurfaceNormals,
  632. exaggeration,
  633. exaggerationRelativeHeight
  634. ) {
  635. let quantization = TerrainQuantization$1.NONE;
  636. let toENU;
  637. let matrix;
  638. if (
  639. defaultValue.defined(axisAlignedBoundingBox) &&
  640. defaultValue.defined(minimumHeight) &&
  641. defaultValue.defined(maximumHeight) &&
  642. defaultValue.defined(fromENU)
  643. ) {
  644. const minimum = axisAlignedBoundingBox.minimum;
  645. const maximum = axisAlignedBoundingBox.maximum;
  646. const dimensions = Matrix2.Cartesian3.subtract(
  647. maximum,
  648. minimum,
  649. cartesian3DimScratch
  650. );
  651. const hDim = maximumHeight - minimumHeight;
  652. const maxDim = Math.max(Matrix2.Cartesian3.maximumComponent(dimensions), hDim);
  653. if (maxDim < SHIFT_LEFT_12 - 1.0) {
  654. quantization = TerrainQuantization$1.BITS12;
  655. } else {
  656. quantization = TerrainQuantization$1.NONE;
  657. }
  658. toENU = Matrix2.Matrix4.inverseTransformation(fromENU, new Matrix2.Matrix4());
  659. const translation = Matrix2.Cartesian3.negate(minimum, cartesian3Scratch);
  660. Matrix2.Matrix4.multiply(
  661. Matrix2.Matrix4.fromTranslation(translation, matrix4Scratch),
  662. toENU,
  663. toENU
  664. );
  665. const scale = cartesian3Scratch;
  666. scale.x = 1.0 / dimensions.x;
  667. scale.y = 1.0 / dimensions.y;
  668. scale.z = 1.0 / dimensions.z;
  669. Matrix2.Matrix4.multiply(Matrix2.Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
  670. matrix = Matrix2.Matrix4.clone(fromENU);
  671. Matrix2.Matrix4.setTranslation(matrix, Matrix2.Cartesian3.ZERO, matrix);
  672. fromENU = Matrix2.Matrix4.clone(fromENU, new Matrix2.Matrix4());
  673. const translationMatrix = Matrix2.Matrix4.fromTranslation(minimum, matrix4Scratch);
  674. const scaleMatrix = Matrix2.Matrix4.fromScale(dimensions, matrix4Scratch2);
  675. const st = Matrix2.Matrix4.multiply(translationMatrix, scaleMatrix, matrix4Scratch);
  676. Matrix2.Matrix4.multiply(fromENU, st, fromENU);
  677. Matrix2.Matrix4.multiply(matrix, st, matrix);
  678. }
  679. /**
  680. * How the vertices of the mesh were compressed.
  681. * @type {TerrainQuantization}
  682. */
  683. this.quantization = quantization;
  684. /**
  685. * The minimum height of the tile including the skirts.
  686. * @type {Number}
  687. */
  688. this.minimumHeight = minimumHeight;
  689. /**
  690. * The maximum height of the tile.
  691. * @type {Number}
  692. */
  693. this.maximumHeight = maximumHeight;
  694. /**
  695. * The center of the tile.
  696. * @type {Cartesian3}
  697. */
  698. this.center = Matrix2.Cartesian3.clone(center);
  699. /**
  700. * A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
  701. * it so each component is in the [0, 1] range.
  702. * @type {Matrix4}
  703. */
  704. this.toScaledENU = toENU;
  705. /**
  706. * A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
  707. * @type {Matrix4}
  708. */
  709. this.fromScaledENU = fromENU;
  710. /**
  711. * The matrix used to decompress the terrain vertices in the shader for RTE rendering.
  712. * @type {Matrix4}
  713. */
  714. this.matrix = matrix;
  715. /**
  716. * The terrain mesh contains normals.
  717. * @type {Boolean}
  718. */
  719. this.hasVertexNormals = hasVertexNormals;
  720. /**
  721. * The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
  722. * @type {Boolean}
  723. */
  724. this.hasWebMercatorT = defaultValue.defaultValue(hasWebMercatorT, false);
  725. /**
  726. * The terrain mesh contains geodetic surface normals, used for terrain exaggeration.
  727. * @type {Boolean}
  728. */
  729. this.hasGeodeticSurfaceNormals = defaultValue.defaultValue(
  730. hasGeodeticSurfaceNormals,
  731. false
  732. );
  733. /**
  734. * A scalar used to exaggerate terrain.
  735. * @type {Number}
  736. */
  737. this.exaggeration = defaultValue.defaultValue(exaggeration, 1.0);
  738. /**
  739. * The relative height from which terrain is exaggerated.
  740. */
  741. this.exaggerationRelativeHeight = defaultValue.defaultValue(
  742. exaggerationRelativeHeight,
  743. 0.0
  744. );
  745. /**
  746. * The number of components in each vertex. This value can differ with different quantizations.
  747. * @type {Number}
  748. */
  749. this.stride = 0;
  750. this._offsetGeodeticSurfaceNormal = 0;
  751. this._offsetVertexNormal = 0;
  752. // Calculate the stride and offsets declared above
  753. this._calculateStrideAndOffsets();
  754. }
  755. TerrainEncoding.prototype.encode = function (
  756. vertexBuffer,
  757. bufferIndex,
  758. position,
  759. uv,
  760. height,
  761. normalToPack,
  762. webMercatorT,
  763. geodeticSurfaceNormal
  764. ) {
  765. const u = uv.x;
  766. const v = uv.y;
  767. if (this.quantization === TerrainQuantization$1.BITS12) {
  768. position = Matrix2.Matrix4.multiplyByPoint(
  769. this.toScaledENU,
  770. position,
  771. cartesian3Scratch
  772. );
  773. position.x = ComponentDatatype.CesiumMath.clamp(position.x, 0.0, 1.0);
  774. position.y = ComponentDatatype.CesiumMath.clamp(position.y, 0.0, 1.0);
  775. position.z = ComponentDatatype.CesiumMath.clamp(position.z, 0.0, 1.0);
  776. const hDim = this.maximumHeight - this.minimumHeight;
  777. const h = ComponentDatatype.CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
  778. Matrix2.Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
  779. const compressed0 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  780. cartesian2Scratch
  781. );
  782. Matrix2.Cartesian2.fromElements(position.z, h, cartesian2Scratch);
  783. const compressed1 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  784. cartesian2Scratch
  785. );
  786. Matrix2.Cartesian2.fromElements(u, v, cartesian2Scratch);
  787. const compressed2 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  788. cartesian2Scratch
  789. );
  790. vertexBuffer[bufferIndex++] = compressed0;
  791. vertexBuffer[bufferIndex++] = compressed1;
  792. vertexBuffer[bufferIndex++] = compressed2;
  793. if (this.hasWebMercatorT) {
  794. Matrix2.Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
  795. const compressed3 = AttributeCompression.AttributeCompression.compressTextureCoordinates(
  796. cartesian2Scratch
  797. );
  798. vertexBuffer[bufferIndex++] = compressed3;
  799. }
  800. } else {
  801. Matrix2.Cartesian3.subtract(position, this.center, cartesian3Scratch);
  802. vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
  803. vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
  804. vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
  805. vertexBuffer[bufferIndex++] = height;
  806. vertexBuffer[bufferIndex++] = u;
  807. vertexBuffer[bufferIndex++] = v;
  808. if (this.hasWebMercatorT) {
  809. vertexBuffer[bufferIndex++] = webMercatorT;
  810. }
  811. }
  812. if (this.hasVertexNormals) {
  813. vertexBuffer[bufferIndex++] = AttributeCompression.AttributeCompression.octPackFloat(
  814. normalToPack
  815. );
  816. }
  817. if (this.hasGeodeticSurfaceNormals) {
  818. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.x;
  819. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.y;
  820. vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.z;
  821. }
  822. return bufferIndex;
  823. };
  824. const scratchPosition = new Matrix2.Cartesian3();
  825. const scratchGeodeticSurfaceNormal = new Matrix2.Cartesian3();
  826. TerrainEncoding.prototype.addGeodeticSurfaceNormals = function (
  827. oldBuffer,
  828. newBuffer,
  829. ellipsoid
  830. ) {
  831. if (this.hasGeodeticSurfaceNormals) {
  832. return;
  833. }
  834. const oldStride = this.stride;
  835. const vertexCount = oldBuffer.length / oldStride;
  836. this.hasGeodeticSurfaceNormals = true;
  837. this._calculateStrideAndOffsets();
  838. const newStride = this.stride;
  839. for (let index = 0; index < vertexCount; index++) {
  840. for (let offset = 0; offset < oldStride; offset++) {
  841. const oldIndex = index * oldStride + offset;
  842. const newIndex = index * newStride + offset;
  843. newBuffer[newIndex] = oldBuffer[oldIndex];
  844. }
  845. const position = this.decodePosition(newBuffer, index, scratchPosition);
  846. const geodeticSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  847. position,
  848. scratchGeodeticSurfaceNormal
  849. );
  850. const bufferIndex = index * newStride + this._offsetGeodeticSurfaceNormal;
  851. newBuffer[bufferIndex] = geodeticSurfaceNormal.x;
  852. newBuffer[bufferIndex + 1] = geodeticSurfaceNormal.y;
  853. newBuffer[bufferIndex + 2] = geodeticSurfaceNormal.z;
  854. }
  855. };
  856. TerrainEncoding.prototype.removeGeodeticSurfaceNormals = function (
  857. oldBuffer,
  858. newBuffer
  859. ) {
  860. if (!this.hasGeodeticSurfaceNormals) {
  861. return;
  862. }
  863. const oldStride = this.stride;
  864. const vertexCount = oldBuffer.length / oldStride;
  865. this.hasGeodeticSurfaceNormals = false;
  866. this._calculateStrideAndOffsets();
  867. const newStride = this.stride;
  868. for (let index = 0; index < vertexCount; index++) {
  869. for (let offset = 0; offset < newStride; offset++) {
  870. const oldIndex = index * oldStride + offset;
  871. const newIndex = index * newStride + offset;
  872. newBuffer[newIndex] = oldBuffer[oldIndex];
  873. }
  874. }
  875. };
  876. TerrainEncoding.prototype.decodePosition = function (buffer, index, result) {
  877. if (!defaultValue.defined(result)) {
  878. result = new Matrix2.Cartesian3();
  879. }
  880. index *= this.stride;
  881. if (this.quantization === TerrainQuantization$1.BITS12) {
  882. const xy = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  883. buffer[index],
  884. cartesian2Scratch
  885. );
  886. result.x = xy.x;
  887. result.y = xy.y;
  888. const zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  889. buffer[index + 1],
  890. cartesian2Scratch
  891. );
  892. result.z = zh.x;
  893. return Matrix2.Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
  894. }
  895. result.x = buffer[index];
  896. result.y = buffer[index + 1];
  897. result.z = buffer[index + 2];
  898. return Matrix2.Cartesian3.add(result, this.center, result);
  899. };
  900. TerrainEncoding.prototype.getExaggeratedPosition = function (
  901. buffer,
  902. index,
  903. result
  904. ) {
  905. result = this.decodePosition(buffer, index, result);
  906. const exaggeration = this.exaggeration;
  907. const exaggerationRelativeHeight = this.exaggerationRelativeHeight;
  908. const hasExaggeration = exaggeration !== 1.0;
  909. if (hasExaggeration && this.hasGeodeticSurfaceNormals) {
  910. const geodeticSurfaceNormal = this.decodeGeodeticSurfaceNormal(
  911. buffer,
  912. index,
  913. scratchGeodeticSurfaceNormal
  914. );
  915. const rawHeight = this.decodeHeight(buffer, index);
  916. const heightDifference =
  917. TerrainExaggeration.getHeight(
  918. rawHeight,
  919. exaggeration,
  920. exaggerationRelativeHeight
  921. ) - rawHeight;
  922. // some math is unrolled for better performance
  923. result.x += geodeticSurfaceNormal.x * heightDifference;
  924. result.y += geodeticSurfaceNormal.y * heightDifference;
  925. result.z += geodeticSurfaceNormal.z * heightDifference;
  926. }
  927. return result;
  928. };
  929. TerrainEncoding.prototype.decodeTextureCoordinates = function (
  930. buffer,
  931. index,
  932. result
  933. ) {
  934. if (!defaultValue.defined(result)) {
  935. result = new Matrix2.Cartesian2();
  936. }
  937. index *= this.stride;
  938. if (this.quantization === TerrainQuantization$1.BITS12) {
  939. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  940. buffer[index + 2],
  941. result
  942. );
  943. }
  944. return Matrix2.Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
  945. };
  946. TerrainEncoding.prototype.decodeHeight = function (buffer, index) {
  947. index *= this.stride;
  948. if (this.quantization === TerrainQuantization$1.BITS12) {
  949. const zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  950. buffer[index + 1],
  951. cartesian2Scratch
  952. );
  953. return (
  954. zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight
  955. );
  956. }
  957. return buffer[index + 3];
  958. };
  959. TerrainEncoding.prototype.decodeWebMercatorT = function (buffer, index) {
  960. index *= this.stride;
  961. if (this.quantization === TerrainQuantization$1.BITS12) {
  962. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(
  963. buffer[index + 3],
  964. cartesian2Scratch
  965. ).x;
  966. }
  967. return buffer[index + 6];
  968. };
  969. TerrainEncoding.prototype.getOctEncodedNormal = function (
  970. buffer,
  971. index,
  972. result
  973. ) {
  974. index = index * this.stride + this._offsetVertexNormal;
  975. const temp = buffer[index] / 256.0;
  976. const x = Math.floor(temp);
  977. const y = (temp - x) * 256.0;
  978. return Matrix2.Cartesian2.fromElements(x, y, result);
  979. };
  980. TerrainEncoding.prototype.decodeGeodeticSurfaceNormal = function (
  981. buffer,
  982. index,
  983. result
  984. ) {
  985. index = index * this.stride + this._offsetGeodeticSurfaceNormal;
  986. result.x = buffer[index];
  987. result.y = buffer[index + 1];
  988. result.z = buffer[index + 2];
  989. return result;
  990. };
  991. TerrainEncoding.prototype._calculateStrideAndOffsets = function () {
  992. let vertexStride = 0;
  993. switch (this.quantization) {
  994. case TerrainQuantization$1.BITS12:
  995. vertexStride += 3;
  996. break;
  997. default:
  998. vertexStride += 6;
  999. }
  1000. if (this.hasWebMercatorT) {
  1001. vertexStride += 1;
  1002. }
  1003. if (this.hasVertexNormals) {
  1004. this._offsetVertexNormal = vertexStride;
  1005. vertexStride += 1;
  1006. }
  1007. if (this.hasGeodeticSurfaceNormals) {
  1008. this._offsetGeodeticSurfaceNormal = vertexStride;
  1009. vertexStride += 3;
  1010. }
  1011. this.stride = vertexStride;
  1012. };
  1013. const attributesIndicesNone = {
  1014. position3DAndHeight: 0,
  1015. textureCoordAndEncodedNormals: 1,
  1016. geodeticSurfaceNormal: 2,
  1017. };
  1018. const attributesIndicesBits12 = {
  1019. compressed0: 0,
  1020. compressed1: 1,
  1021. geodeticSurfaceNormal: 2,
  1022. };
  1023. TerrainEncoding.prototype.getAttributes = function (buffer) {
  1024. const datatype = ComponentDatatype.ComponentDatatype.FLOAT;
  1025. const sizeInBytes = ComponentDatatype.ComponentDatatype.getSizeInBytes(datatype);
  1026. const strideInBytes = this.stride * sizeInBytes;
  1027. let offsetInBytes = 0;
  1028. const attributes = [];
  1029. function addAttribute(index, componentsPerAttribute) {
  1030. attributes.push({
  1031. index: index,
  1032. vertexBuffer: buffer,
  1033. componentDatatype: datatype,
  1034. componentsPerAttribute: componentsPerAttribute,
  1035. offsetInBytes: offsetInBytes,
  1036. strideInBytes: strideInBytes,
  1037. });
  1038. offsetInBytes += componentsPerAttribute * sizeInBytes;
  1039. }
  1040. if (this.quantization === TerrainQuantization$1.NONE) {
  1041. addAttribute(attributesIndicesNone.position3DAndHeight, 4);
  1042. let componentsTexCoordAndNormals = 2;
  1043. componentsTexCoordAndNormals += this.hasWebMercatorT ? 1 : 0;
  1044. componentsTexCoordAndNormals += this.hasVertexNormals ? 1 : 0;
  1045. addAttribute(
  1046. attributesIndicesNone.textureCoordAndEncodedNormals,
  1047. componentsTexCoordAndNormals
  1048. );
  1049. if (this.hasGeodeticSurfaceNormals) {
  1050. addAttribute(attributesIndicesNone.geodeticSurfaceNormal, 3);
  1051. }
  1052. } else {
  1053. // When there is no webMercatorT or vertex normals, the attribute only needs 3 components: x/y, z/h, u/v.
  1054. // WebMercatorT and vertex normals each take up one component, so if only one of them is present the first
  1055. // attribute gets a 4th component. If both are present, we need an additional attribute that has 1 component.
  1056. const usingAttribute0Component4 =
  1057. this.hasWebMercatorT || this.hasVertexNormals;
  1058. const usingAttribute1Component1 =
  1059. this.hasWebMercatorT && this.hasVertexNormals;
  1060. addAttribute(
  1061. attributesIndicesBits12.compressed0,
  1062. usingAttribute0Component4 ? 4 : 3
  1063. );
  1064. if (usingAttribute1Component1) {
  1065. addAttribute(attributesIndicesBits12.compressed1, 1);
  1066. }
  1067. if (this.hasGeodeticSurfaceNormals) {
  1068. addAttribute(attributesIndicesBits12.geodeticSurfaceNormal, 3);
  1069. }
  1070. }
  1071. return attributes;
  1072. };
  1073. TerrainEncoding.prototype.getAttributeLocations = function () {
  1074. if (this.quantization === TerrainQuantization$1.NONE) {
  1075. return attributesIndicesNone;
  1076. }
  1077. return attributesIndicesBits12;
  1078. };
  1079. TerrainEncoding.clone = function (encoding, result) {
  1080. if (!defaultValue.defined(encoding)) {
  1081. return undefined;
  1082. }
  1083. if (!defaultValue.defined(result)) {
  1084. result = new TerrainEncoding();
  1085. }
  1086. result.quantization = encoding.quantization;
  1087. result.minimumHeight = encoding.minimumHeight;
  1088. result.maximumHeight = encoding.maximumHeight;
  1089. result.center = Matrix2.Cartesian3.clone(encoding.center);
  1090. result.toScaledENU = Matrix2.Matrix4.clone(encoding.toScaledENU);
  1091. result.fromScaledENU = Matrix2.Matrix4.clone(encoding.fromScaledENU);
  1092. result.matrix = Matrix2.Matrix4.clone(encoding.matrix);
  1093. result.hasVertexNormals = encoding.hasVertexNormals;
  1094. result.hasWebMercatorT = encoding.hasWebMercatorT;
  1095. result.hasGeodeticSurfaceNormals = encoding.hasGeodeticSurfaceNormals;
  1096. result.exaggeration = encoding.exaggeration;
  1097. result.exaggerationRelativeHeight = encoding.exaggerationRelativeHeight;
  1098. result._calculateStrideAndOffsets();
  1099. return result;
  1100. };
  1101. exports.EllipsoidalOccluder = EllipsoidalOccluder;
  1102. exports.TerrainEncoding = TerrainEncoding;
  1103. }));
  1104. //# sourceMappingURL=TerrainEncoding-8fbe9124.js.map