TerrainEncoding-a807a704.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-8f8a682c', './ComponentDatatype-5862616f', './AttributeCompression-84a90a13'], function (exports, when, Check, _Math, Cartographic, Cartesian2, BoundingSphere, ComponentDatatype, AttributeCompression) { 'use strict';
  24. /**
  25. * Determine whether or not other objects are visible or hidden behind the visible horizon defined by
  26. * an {@link Ellipsoid} and a camera position. The ellipsoid is assumed to be located at the
  27. * origin of the coordinate system. This class uses the algorithm described in the
  28. * {@link https://cesium.com/blog/2013/04/25/Horizon-culling/|Horizon Culling} blog post.
  29. *
  30. * @alias EllipsoidalOccluder
  31. *
  32. * @param {Ellipsoid} ellipsoid The ellipsoid to use as an occluder.
  33. * @param {Cartesian3} [cameraPosition] The coordinate of the viewer/camera. If this parameter is not
  34. * specified, {@link EllipsoidalOccluder#cameraPosition} must be called before
  35. * testing visibility.
  36. *
  37. * @constructor
  38. *
  39. * @example
  40. * // Construct an ellipsoidal occluder with radii 1.0, 1.1, and 0.9.
  41. * var cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  42. * var occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  43. * var occluder = new Cesium.EllipsoidalOccluder(occluderEllipsoid, cameraPosition);
  44. *
  45. * @private
  46. */
  47. function EllipsoidalOccluder(ellipsoid, cameraPosition) {
  48. //>>includeStart('debug', pragmas.debug);
  49. Check.Check.typeOf.object('ellipsoid', ellipsoid);
  50. //>>includeEnd('debug');
  51. this._ellipsoid = ellipsoid;
  52. this._cameraPosition = new Cartographic.Cartesian3();
  53. this._cameraPositionInScaledSpace = new Cartographic.Cartesian3();
  54. this._distanceToLimbInScaledSpaceSquared = 0.0;
  55. // cameraPosition fills in the above values
  56. if (when.defined(cameraPosition)) {
  57. this.cameraPosition = cameraPosition;
  58. }
  59. }
  60. Object.defineProperties(EllipsoidalOccluder.prototype, {
  61. /**
  62. * Gets the occluding ellipsoid.
  63. * @memberof EllipsoidalOccluder.prototype
  64. * @type {Ellipsoid}
  65. */
  66. ellipsoid : {
  67. get: function() {
  68. return this._ellipsoid;
  69. }
  70. },
  71. /**
  72. * Gets or sets the position of the camera.
  73. * @memberof EllipsoidalOccluder.prototype
  74. * @type {Cartesian3}
  75. */
  76. cameraPosition : {
  77. get : function() {
  78. return this._cameraPosition;
  79. },
  80. set : function(cameraPosition) {
  81. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  82. var ellipsoid = this._ellipsoid;
  83. var cv = ellipsoid.transformPositionToScaledSpace(cameraPosition, this._cameraPositionInScaledSpace);
  84. var vhMagnitudeSquared = Cartographic.Cartesian3.magnitudeSquared(cv) - 1.0;
  85. Cartographic.Cartesian3.clone(cameraPosition, this._cameraPosition);
  86. this._cameraPositionInScaledSpace = cv;
  87. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  88. }
  89. }
  90. });
  91. var scratchCartesian = new Cartographic.Cartesian3();
  92. /**
  93. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  94. *
  95. * @param {Cartesian3} occludee The point to test for visibility.
  96. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  97. *
  98. * @example
  99. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  100. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  101. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  102. * var point = new Cesium.Cartesian3(0, -3, -3);
  103. * occluder.isPointVisible(point); //returns true
  104. */
  105. EllipsoidalOccluder.prototype.isPointVisible = function(occludee) {
  106. var ellipsoid = this._ellipsoid;
  107. var occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(occludee, scratchCartesian);
  108. return isScaledSpacePointVisible(occludeeScaledSpacePosition, this._cameraPositionInScaledSpace, this._distanceToLimbInScaledSpaceSquared);
  109. };
  110. /**
  111. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  112. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  113. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  114. *
  115. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  116. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  117. *
  118. * @example
  119. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  120. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  121. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  122. * var point = new Cesium.Cartesian3(0, -3, -3);
  123. * var scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  124. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  125. */
  126. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function(occludeeScaledSpacePosition) {
  127. return isScaledSpacePointVisible(occludeeScaledSpacePosition, this._cameraPositionInScaledSpace, this._distanceToLimbInScaledSpaceSquared);
  128. };
  129. var scratchCameraPositionInScaledSpaceShrunk = new Cartographic.Cartesian3();
  130. /**
  131. * Similar to {@link EllipsoidalOccluder#isScaledSpacePointVisible} except tests against an
  132. * ellipsoid that has been shrunk by the minimum height when the minimum height is below
  133. * the ellipsoid. This is intended to be used with points generated by
  134. * {@link EllipsoidalOccluder#computeHorizonCullingPointPossiblyUnderEllipsoid} or
  135. * {@link EllipsoidalOccluder#computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid}.
  136. *
  137. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space of the possibly-shrunk ellipsoid.
  138. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  139. */
  140. EllipsoidalOccluder.prototype.isScaledSpacePointVisiblePossiblyUnderEllipsoid = function(occludeeScaledSpacePosition, minimumHeight) {
  141. var ellipsoid = this._ellipsoid;
  142. var vhMagnitudeSquared;
  143. var cv;
  144. if (when.defined(minimumHeight) && minimumHeight < 0.0 && ellipsoid.minimumRadius > -minimumHeight) {
  145. // This code is similar to the cameraPosition setter, but unrolled for performance because it will be called a lot.
  146. cv = scratchCameraPositionInScaledSpaceShrunk;
  147. cv.x = this._cameraPosition.x / (ellipsoid.radii.x + minimumHeight);
  148. cv.y = this._cameraPosition.y / (ellipsoid.radii.y + minimumHeight);
  149. cv.z = this._cameraPosition.z / (ellipsoid.radii.z + minimumHeight);
  150. vhMagnitudeSquared = cv.x * cv.x + cv.y * cv.y + cv.z * cv.z - 1.0;
  151. } else {
  152. cv = this._cameraPositionInScaledSpace;
  153. vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  154. }
  155. return isScaledSpacePointVisible(occludeeScaledSpacePosition, cv, vhMagnitudeSquared);
  156. };
  157. /**
  158. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  159. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  160. * is expressed in the ellipsoid-scaled space and is suitable for use with
  161. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  162. *
  163. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  164. * A reasonable direction to use is the direction from the center of the ellipsoid to
  165. * the center of the bounding sphere computed from the positions. The direction need not
  166. * be normalized.
  167. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  168. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  169. * ellipsoid's axes.
  170. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  171. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  172. */
  173. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function(directionToPoint, positions, result) {
  174. return computeHorizonCullingPointFromPositions(this._ellipsoid, directionToPoint, positions, result);
  175. };
  176. var scratchEllipsoidShrunk = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  177. /**
  178. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPoint} except computes the culling
  179. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  180. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  181. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  182. *
  183. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  184. * A reasonable direction to use is the direction from the center of the ellipsoid to
  185. * the center of the bounding sphere computed from the positions. The direction need not
  186. * be normalized.
  187. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  188. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  189. * ellipsoid's axes.
  190. * @param {Number} [minimumHeight] The minimum height of all positions. If this value is undefined, all positions are assumed to be above the ellipsoid.
  191. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  192. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  193. */
  194. EllipsoidalOccluder.prototype.computeHorizonCullingPointPossiblyUnderEllipsoid = function(directionToPoint, positions, minimumHeight, result) {
  195. var possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(this._ellipsoid, minimumHeight, scratchEllipsoidShrunk);
  196. return computeHorizonCullingPointFromPositions(possiblyShrunkEllipsoid, directionToPoint, positions, result);
  197. };
  198. /**
  199. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  200. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  201. * is expressed in the ellipsoid-scaled space and is suitable for use with
  202. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  203. *
  204. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  205. * A reasonable direction to use is the direction from the center of the ellipsoid to
  206. * the center of the bounding sphere computed from the positions. The direction need not
  207. * be normalized.
  208. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  209. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  210. * ellipsoid's axes.
  211. * @param {Number} [stride=3]
  212. * @param {Cartesian3} [center=Cartesian3.ZERO]
  213. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  214. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  215. */
  216. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function(directionToPoint, vertices, stride, center, result) {
  217. return computeHorizonCullingPointFromVertices(this._ellipsoid, directionToPoint, vertices, stride, center, result);
  218. };
  219. /**
  220. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPointFromVertices} except computes the culling
  221. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  222. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  223. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  224. *
  225. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  226. * A reasonable direction to use is the direction from the center of the ellipsoid to
  227. * the center of the bounding sphere computed from the positions. The direction need not
  228. * be normalized.
  229. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  230. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  231. * ellipsoid's axes.
  232. * @param {Number} [stride=3]
  233. * @param {Cartesian3} [center=Cartesian3.ZERO]
  234. * @param {Number} [minimumHeight] The minimum height of all vertices. If this value is undefined, all vertices are assumed to be above the ellipsoid.
  235. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  236. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  237. */
  238. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid = function(directionToPoint, vertices, stride, center, minimumHeight, result) {
  239. var possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(this._ellipsoid, minimumHeight, scratchEllipsoidShrunk);
  240. return computeHorizonCullingPointFromVertices(possiblyShrunkEllipsoid, directionToPoint, vertices, stride, center, result);
  241. };
  242. var subsampleScratch = [];
  243. /**
  244. * Computes a point that can be used for horizon culling of a rectangle. If the point is below
  245. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  246. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  247. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  248. *
  249. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  250. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  251. * the ellipsoid used by this instance for occlusion testing.
  252. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  253. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  254. */
  255. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function(rectangle, ellipsoid, result) {
  256. //>>includeStart('debug', pragmas.debug);
  257. Check.Check.typeOf.object('rectangle', rectangle);
  258. //>>includeEnd('debug');
  259. var positions = Cartesian2.Rectangle.subsample(rectangle, ellipsoid, 0.0, subsampleScratch);
  260. var bs = BoundingSphere.BoundingSphere.fromPoints(positions);
  261. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  262. // sense to try to horizon cull it.
  263. if (Cartographic.Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  264. return undefined;
  265. }
  266. return this.computeHorizonCullingPoint(bs.center, positions, result);
  267. };
  268. var scratchEllipsoidShrunkRadii = new Cartographic.Cartesian3();
  269. function getPossiblyShrunkEllipsoid(ellipsoid, minimumHeight, result) {
  270. if (when.defined(minimumHeight) && minimumHeight < 0.0 && ellipsoid.minimumRadius > -minimumHeight) {
  271. var ellipsoidShrunkRadii = Cartographic.Cartesian3.fromElements(
  272. ellipsoid.radii.x + minimumHeight,
  273. ellipsoid.radii.y + minimumHeight,
  274. ellipsoid.radii.z + minimumHeight,
  275. scratchEllipsoidShrunkRadii
  276. );
  277. ellipsoid = Cartesian2.Ellipsoid.fromCartesian3(ellipsoidShrunkRadii, result);
  278. }
  279. return ellipsoid;
  280. }
  281. function computeHorizonCullingPointFromPositions(ellipsoid, directionToPoint, positions, result) {
  282. //>>includeStart('debug', pragmas.debug);
  283. Check.Check.typeOf.object('directionToPoint', directionToPoint);
  284. Check.Check.defined('positions', positions);
  285. //>>includeEnd('debug');
  286. if (!when.defined(result)) {
  287. result = new Cartographic.Cartesian3();
  288. }
  289. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  290. var resultMagnitude = 0.0;
  291. for (var i = 0, len = positions.length; i < len; ++i) {
  292. var position = positions[i];
  293. var candidateMagnitude = computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint);
  294. if (candidateMagnitude < 0.0) {
  295. // all points should face the same direction, but this one doesn't, so return undefined
  296. return undefined;
  297. }
  298. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  299. }
  300. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  301. }
  302. var positionScratch = new Cartographic.Cartesian3();
  303. function computeHorizonCullingPointFromVertices(ellipsoid, directionToPoint, vertices, stride, center, result) {
  304. //>>includeStart('debug', pragmas.debug);
  305. Check.Check.typeOf.object('directionToPoint', directionToPoint);
  306. Check.Check.defined('vertices', vertices);
  307. Check.Check.typeOf.number('stride', stride);
  308. //>>includeEnd('debug');
  309. if (!when.defined(result)) {
  310. result = new Cartographic.Cartesian3();
  311. }
  312. stride = when.defaultValue(stride, 3);
  313. center = when.defaultValue(center, Cartographic.Cartesian3.ZERO);
  314. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  315. var resultMagnitude = 0.0;
  316. for (var i = 0, len = vertices.length; i < len; i += stride) {
  317. positionScratch.x = vertices[i] + center.x;
  318. positionScratch.y = vertices[i + 1] + center.y;
  319. positionScratch.z = vertices[i + 2] + center.z;
  320. var candidateMagnitude = computeMagnitude(ellipsoid, positionScratch, scaledSpaceDirectionToPoint);
  321. if (candidateMagnitude < 0.0) {
  322. // all points should face the same direction, but this one doesn't, so return undefined
  323. return undefined;
  324. }
  325. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  326. }
  327. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  328. }
  329. function isScaledSpacePointVisible(occludeeScaledSpacePosition, cameraPositionInScaledSpace, distanceToLimbInScaledSpaceSquared) {
  330. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  331. var cv = cameraPositionInScaledSpace;
  332. var vhMagnitudeSquared = distanceToLimbInScaledSpaceSquared;
  333. var vt = Cartographic.Cartesian3.subtract(occludeeScaledSpacePosition, cv, scratchCartesian);
  334. var vtDotVc = -Cartographic.Cartesian3.dot(vt, cv);
  335. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  336. // in this case, set the culling plane to be on V.
  337. var isOccluded = vhMagnitudeSquared < 0 ? vtDotVc > 0 : (vtDotVc > vhMagnitudeSquared &&
  338. vtDotVc * vtDotVc / Cartographic.Cartesian3.magnitudeSquared(vt) > vhMagnitudeSquared);
  339. return !isOccluded;
  340. }
  341. var scaledSpaceScratch = new Cartographic.Cartesian3();
  342. var directionScratch = new Cartographic.Cartesian3();
  343. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  344. var scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(position, scaledSpaceScratch);
  345. var magnitudeSquared = Cartographic.Cartesian3.magnitudeSquared(scaledSpacePosition);
  346. var magnitude = Math.sqrt(magnitudeSquared);
  347. var direction = Cartographic.Cartesian3.divideByScalar(scaledSpacePosition, magnitude, directionScratch);
  348. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  349. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  350. magnitude = Math.max(1.0, magnitude);
  351. var cosAlpha = Cartographic.Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  352. var sinAlpha = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction));
  353. var cosBeta = 1.0 / magnitude;
  354. var sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  355. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  356. }
  357. function magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result) {
  358. // The horizon culling point is undefined if there were no positions from which to compute it,
  359. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  360. if (resultMagnitude <= 0.0 || resultMagnitude === 1.0 / 0.0 || resultMagnitude !== resultMagnitude) {
  361. return undefined;
  362. }
  363. return Cartographic.Cartesian3.multiplyByScalar(scaledSpaceDirectionToPoint, resultMagnitude, result);
  364. }
  365. var directionToPointScratch = new Cartographic.Cartesian3();
  366. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  367. if (Cartographic.Cartesian3.equals(directionToPoint, Cartographic.Cartesian3.ZERO)) {
  368. return directionToPoint;
  369. }
  370. ellipsoid.transformPositionToScaledSpace(directionToPoint, directionToPointScratch);
  371. return Cartographic.Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  372. }
  373. /**
  374. * This enumerated type is used to determine how the vertices of the terrain mesh are compressed.
  375. *
  376. * @exports TerrainQuantization
  377. *
  378. * @private
  379. */
  380. var TerrainQuantization = {
  381. /**
  382. * The vertices are not compressed.
  383. *
  384. * @type {Number}
  385. * @constant
  386. */
  387. NONE : 0,
  388. /**
  389. * The vertices are compressed to 12 bits.
  390. *
  391. * @type {Number}
  392. * @constant
  393. */
  394. BITS12 : 1
  395. };
  396. var TerrainQuantization$1 = Object.freeze(TerrainQuantization);
  397. var cartesian3Scratch = new Cartographic.Cartesian3();
  398. var cartesian3DimScratch = new Cartographic.Cartesian3();
  399. var cartesian2Scratch = new Cartesian2.Cartesian2();
  400. var matrix4Scratch = new BoundingSphere.Matrix4();
  401. var matrix4Scratch2 = new BoundingSphere.Matrix4();
  402. var SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
  403. /**
  404. * Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
  405. * are unpacked in the vertex shader.
  406. *
  407. * @alias TerrainEncoding
  408. * @constructor
  409. *
  410. * @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
  411. * @param {Number} minimumHeight The minimum height.
  412. * @param {Number} maximumHeight The maximum height.
  413. * @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
  414. * @param {Boolean} hasVertexNormals If the mesh has vertex normals.
  415. * @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
  416. *
  417. * @private
  418. */
  419. function TerrainEncoding(axisAlignedBoundingBox, minimumHeight, maximumHeight, fromENU, hasVertexNormals, hasWebMercatorT) {
  420. var quantization = TerrainQuantization$1.NONE;
  421. var center;
  422. var toENU;
  423. var matrix;
  424. if (when.defined(axisAlignedBoundingBox) && when.defined(minimumHeight) && when.defined(maximumHeight) && when.defined(fromENU)) {
  425. var minimum = axisAlignedBoundingBox.minimum;
  426. var maximum = axisAlignedBoundingBox.maximum;
  427. var dimensions = Cartographic.Cartesian3.subtract(maximum, minimum, cartesian3DimScratch);
  428. var hDim = maximumHeight - minimumHeight;
  429. var maxDim = Math.max(Cartographic.Cartesian3.maximumComponent(dimensions), hDim);
  430. if (maxDim < SHIFT_LEFT_12 - 1.0) {
  431. quantization = TerrainQuantization$1.BITS12;
  432. } else {
  433. quantization = TerrainQuantization$1.NONE;
  434. }
  435. quantization = TerrainQuantization$1.NONE;//防止精度损失,出现地形模型匹配不上,默认不压缩
  436. center = axisAlignedBoundingBox.center;
  437. toENU = BoundingSphere.Matrix4.inverseTransformation(fromENU, new BoundingSphere.Matrix4());
  438. var translation = Cartographic.Cartesian3.negate(minimum, cartesian3Scratch);
  439. BoundingSphere.Matrix4.multiply(BoundingSphere.Matrix4.fromTranslation(translation, matrix4Scratch), toENU, toENU);
  440. var scale = cartesian3Scratch;
  441. scale.x = 1.0 / dimensions.x;
  442. scale.y = 1.0 / dimensions.y;
  443. scale.z = 1.0 / dimensions.z;
  444. BoundingSphere.Matrix4.multiply(BoundingSphere.Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
  445. matrix = BoundingSphere.Matrix4.clone(fromENU);
  446. BoundingSphere.Matrix4.setTranslation(matrix, Cartographic.Cartesian3.ZERO, matrix);
  447. fromENU = BoundingSphere.Matrix4.clone(fromENU, new BoundingSphere.Matrix4());
  448. var translationMatrix = BoundingSphere.Matrix4.fromTranslation(minimum, matrix4Scratch);
  449. var scaleMatrix = BoundingSphere.Matrix4.fromScale(dimensions, matrix4Scratch2);
  450. var st = BoundingSphere.Matrix4.multiply(translationMatrix, scaleMatrix,matrix4Scratch);
  451. BoundingSphere.Matrix4.multiply(fromENU, st, fromENU);
  452. BoundingSphere.Matrix4.multiply(matrix, st, matrix);
  453. }
  454. /**
  455. * How the vertices of the mesh were compressed.
  456. * @type {TerrainQuantization}
  457. */
  458. this.quantization = quantization;
  459. /**
  460. * The minimum height of the tile including the skirts.
  461. * @type {Number}
  462. */
  463. this.minimumHeight = minimumHeight;
  464. /**
  465. * The maximum height of the tile.
  466. * @type {Number}
  467. */
  468. this.maximumHeight = maximumHeight;
  469. /**
  470. * The center of the tile.
  471. * @type {Cartesian3}
  472. */
  473. this.center = center;
  474. /**
  475. * A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
  476. * it so each component is in the [0, 1] range.
  477. * @type {Matrix4}
  478. */
  479. this.toScaledENU = toENU;
  480. /**
  481. * A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
  482. * @type {Matrix4}
  483. */
  484. this.fromScaledENU = fromENU;
  485. /**
  486. * The matrix used to decompress the terrain vertices in the shader for RTE rendering.
  487. * @type {Matrix4}
  488. */
  489. this.matrix = matrix;
  490. /**
  491. * The terrain mesh contains normals.
  492. * @type {Boolean}
  493. */
  494. this.hasVertexNormals = hasVertexNormals;
  495. /**
  496. * The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
  497. * @type {Boolean}
  498. */
  499. this.hasWebMercatorT = when.defaultValue(hasWebMercatorT, false);
  500. }
  501. TerrainEncoding.prototype.encode = function(vertexBuffer, bufferIndex, position, uv, height, normalToPack, webMercatorT) {
  502. var u = uv.x;
  503. var v = uv.y;
  504. if (this.quantization === TerrainQuantization$1.BITS12) {
  505. position = BoundingSphere.Matrix4.multiplyByPoint(this.toScaledENU, position, cartesian3Scratch);
  506. position.x = _Math.CesiumMath.clamp(position.x, 0.0, 1.0);
  507. position.y = _Math.CesiumMath.clamp(position.y, 0.0, 1.0);
  508. position.z = _Math.CesiumMath.clamp(position.z, 0.0, 1.0);
  509. var hDim = this.maximumHeight - this.minimumHeight;
  510. var h = _Math.CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
  511. Cartesian2.Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
  512. var compressed0 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  513. Cartesian2.Cartesian2.fromElements(position.z, h, cartesian2Scratch);
  514. var compressed1 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  515. Cartesian2.Cartesian2.fromElements(u, v, cartesian2Scratch);
  516. var compressed2 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  517. vertexBuffer[bufferIndex++] = compressed0;
  518. vertexBuffer[bufferIndex++] = compressed1;
  519. vertexBuffer[bufferIndex++] = compressed2;
  520. if (this.hasWebMercatorT) {
  521. Cartesian2.Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
  522. var compressed3 = AttributeCompression.AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
  523. vertexBuffer[bufferIndex++] = compressed3;
  524. }
  525. } else {
  526. Cartographic.Cartesian3.subtract(position, this.center, cartesian3Scratch);
  527. vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
  528. vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
  529. vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
  530. vertexBuffer[bufferIndex++] = height;
  531. vertexBuffer[bufferIndex++] = u;
  532. vertexBuffer[bufferIndex++] = v;
  533. if (this.hasWebMercatorT) {
  534. vertexBuffer[bufferIndex++] = webMercatorT;
  535. }
  536. }
  537. if (this.hasVertexNormals) {
  538. vertexBuffer[bufferIndex++] = AttributeCompression.AttributeCompression.octPackFloat(normalToPack);
  539. }
  540. return bufferIndex;
  541. };
  542. TerrainEncoding.prototype.decodePosition = function(buffer, index, result) {
  543. if (!when.defined(result)) {
  544. result = new Cartographic.Cartesian3();
  545. }
  546. index *= this.getStride();
  547. if (this.quantization === TerrainQuantization$1.BITS12) {
  548. var xy = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index], cartesian2Scratch);
  549. result.x = xy.x;
  550. result.y = xy.y;
  551. var zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  552. result.z = zh.x;
  553. return BoundingSphere.Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
  554. }
  555. result.x = buffer[index];
  556. result.y = buffer[index + 1];
  557. result.z = buffer[index + 2];
  558. return Cartographic.Cartesian3.add(result, this.center, result);
  559. };
  560. TerrainEncoding.prototype.decodeTextureCoordinates = function(buffer, index, result) {
  561. if (!when.defined(result)) {
  562. result = new Cartesian2.Cartesian2();
  563. }
  564. index *= this.getStride();
  565. if (this.quantization === TerrainQuantization$1.BITS12) {
  566. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 2], result);
  567. }
  568. return Cartesian2.Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
  569. };
  570. TerrainEncoding.prototype.decodeHeight = function(buffer, index) {
  571. index *= this.getStride();
  572. if (this.quantization === TerrainQuantization$1.BITS12) {
  573. var zh = AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
  574. return zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight;
  575. }
  576. return buffer[index + 3];
  577. };
  578. TerrainEncoding.prototype.decodeWebMercatorT = function(buffer, index) {
  579. index *= this.getStride();
  580. if (this.quantization === TerrainQuantization$1.BITS12) {
  581. return AttributeCompression.AttributeCompression.decompressTextureCoordinates(buffer[index + 3], cartesian2Scratch).x;
  582. }
  583. return buffer[index + 6];
  584. };
  585. TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
  586. var stride = this.getStride();
  587. index = (index + 1) * stride - 1;
  588. var temp = buffer[index] / 256.0;
  589. var x = Math.floor(temp);
  590. var y = (temp - x) * 256.0;
  591. return Cartesian2.Cartesian2.fromElements(x, y, result);
  592. };
  593. TerrainEncoding.prototype.getStride = function() {
  594. var vertexStride;
  595. switch (this.quantization) {
  596. case TerrainQuantization$1.BITS12:
  597. vertexStride = 3;
  598. break;
  599. default:
  600. vertexStride = 6;
  601. }
  602. if (this.hasWebMercatorT) {
  603. ++vertexStride;
  604. }
  605. if (this.hasVertexNormals) {
  606. ++vertexStride;
  607. }
  608. return vertexStride;
  609. };
  610. var attributesNone = {
  611. position3DAndHeight : 0,
  612. textureCoordAndEncodedNormals : 1
  613. };
  614. var attributes = {
  615. compressed0 : 0,
  616. compressed1 : 1
  617. };
  618. TerrainEncoding.prototype.getAttributes = function(buffer) {
  619. var datatype = ComponentDatatype.ComponentDatatype.FLOAT;
  620. var sizeInBytes = ComponentDatatype.ComponentDatatype.getSizeInBytes(datatype);
  621. var stride;
  622. if (this.quantization === TerrainQuantization$1.NONE) {
  623. var position3DAndHeightLength = 4;
  624. var numTexCoordComponents = 2;
  625. if (this.hasWebMercatorT) {
  626. ++numTexCoordComponents;
  627. }
  628. if (this.hasVertexNormals) {
  629. ++numTexCoordComponents;
  630. }
  631. stride = (position3DAndHeightLength + numTexCoordComponents) * sizeInBytes;
  632. return [{
  633. index : attributesNone.position3DAndHeight,
  634. vertexBuffer : buffer,
  635. componentDatatype : datatype,
  636. componentsPerAttribute : position3DAndHeightLength,
  637. offsetInBytes : 0,
  638. strideInBytes : stride
  639. }, {
  640. index : attributesNone.textureCoordAndEncodedNormals,
  641. vertexBuffer : buffer,
  642. componentDatatype : datatype,
  643. componentsPerAttribute : numTexCoordComponents,
  644. offsetInBytes : position3DAndHeightLength * sizeInBytes,
  645. strideInBytes : stride
  646. }];
  647. }
  648. var numCompressed0 = 3;
  649. var numCompressed1 = 0;
  650. if (this.hasWebMercatorT || this.hasVertexNormals) {
  651. ++numCompressed0;
  652. }
  653. if (this.hasWebMercatorT && this.hasVertexNormals) {
  654. ++numCompressed1;
  655. stride = (numCompressed0 + numCompressed1) * sizeInBytes;
  656. return [{
  657. index : attributes.compressed0,
  658. vertexBuffer : buffer,
  659. componentDatatype : datatype,
  660. componentsPerAttribute : numCompressed0,
  661. offsetInBytes : 0,
  662. strideInBytes : stride
  663. }, {
  664. index : attributes.compressed1,
  665. vertexBuffer : buffer,
  666. componentDatatype : datatype,
  667. componentsPerAttribute : numCompressed1,
  668. offsetInBytes : numCompressed0 * sizeInBytes,
  669. strideInBytes : stride
  670. }];
  671. }
  672. return [{
  673. index : attributes.compressed0,
  674. vertexBuffer : buffer,
  675. componentDatatype : datatype,
  676. componentsPerAttribute : numCompressed0
  677. }];
  678. };
  679. TerrainEncoding.prototype.getAttributeLocations = function() {
  680. if (this.quantization === TerrainQuantization$1.NONE) {
  681. return attributesNone;
  682. }
  683. return attributes;
  684. };
  685. TerrainEncoding.clone = function(encoding, result) {
  686. if (!when.defined(result)) {
  687. result = new TerrainEncoding();
  688. }
  689. result.quantization = encoding.quantization;
  690. result.minimumHeight = encoding.minimumHeight;
  691. result.maximumHeight = encoding.maximumHeight;
  692. result.center = Cartographic.Cartesian3.clone(encoding.center);
  693. result.toScaledENU = BoundingSphere.Matrix4.clone(encoding.toScaledENU);
  694. result.fromScaledENU = BoundingSphere.Matrix4.clone(encoding.fromScaledENU);
  695. result.matrix = BoundingSphere.Matrix4.clone(encoding.matrix);
  696. result.hasVertexNormals = encoding.hasVertexNormals;
  697. result.hasWebMercatorT = encoding.hasWebMercatorT;
  698. return result;
  699. };
  700. exports.EllipsoidalOccluder = EllipsoidalOccluder;
  701. exports.TerrainEncoding = TerrainEncoding;
  702. });