TileBoundingSphere.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Check from "../Core/Check.js";
  4. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  5. import GeometryInstance from "../Core/GeometryInstance.js";
  6. import CesiumMath from "../Core/Math.js";
  7. import Matrix4 from "../Core/Matrix4.js";
  8. import SphereOutlineGeometry from "../Core/SphereOutlineGeometry.js";
  9. import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
  10. import Primitive from "./Primitive.js";
  11. /**
  12. * A tile bounding volume specified as a sphere.
  13. * @alias TileBoundingSphere
  14. * @constructor
  15. *
  16. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  17. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  18. *
  19. * @private
  20. */
  21. function TileBoundingSphere(center, radius) {
  22. if (radius === 0) {
  23. radius = CesiumMath.EPSILON7;
  24. }
  25. this._boundingSphere = new BoundingSphere(center, radius);
  26. }
  27. Object.defineProperties(TileBoundingSphere.prototype, {
  28. /**
  29. * The center of the bounding sphere
  30. *
  31. * @memberof TileBoundingSphere.prototype
  32. *
  33. * @type {Cartesian3}
  34. * @readonly
  35. */
  36. center: {
  37. get: function () {
  38. return this._boundingSphere.center;
  39. },
  40. },
  41. /**
  42. * The radius of the bounding sphere
  43. *
  44. * @memberof TileBoundingSphere.prototype
  45. *
  46. * @type {Number}
  47. * @readonly
  48. */
  49. radius: {
  50. get: function () {
  51. return this._boundingSphere.radius;
  52. },
  53. },
  54. /**
  55. * The underlying bounding volume
  56. *
  57. * @memberof TileBoundingSphere.prototype
  58. *
  59. * @type {Object}
  60. * @readonly
  61. */
  62. boundingVolume: {
  63. get: function () {
  64. return this._boundingSphere;
  65. },
  66. },
  67. /**
  68. * The underlying bounding sphere
  69. *
  70. * @memberof TileBoundingSphere.prototype
  71. *
  72. * @type {BoundingSphere}
  73. * @readonly
  74. */
  75. boundingSphere: {
  76. get: function () {
  77. return this._boundingSphere;
  78. },
  79. },
  80. });
  81. /**
  82. * Computes the distance between this bounding sphere and the camera attached to frameState.
  83. *
  84. * @param {FrameState} frameState The frameState to which the camera is attached.
  85. * @returns {Number} The distance between the camera and the bounding sphere in meters. Returns 0 if the camera is inside the bounding volume.
  86. *
  87. */
  88. TileBoundingSphere.prototype.distanceToCamera = function (frameState) {
  89. //>>includeStart('debug', pragmas.debug);
  90. Check.defined("frameState", frameState);
  91. //>>includeEnd('debug');
  92. const boundingSphere = this._boundingSphere;
  93. return Math.max(
  94. 0.0,
  95. Cartesian3.distance(boundingSphere.center, frameState.camera.positionWC) -
  96. boundingSphere.radius
  97. );
  98. };
  99. /**
  100. * Determines which side of a plane this sphere is located.
  101. *
  102. * @param {Plane} plane The plane to test against.
  103. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  104. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  105. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  106. * intersects the plane.
  107. */
  108. TileBoundingSphere.prototype.intersectPlane = function (plane) {
  109. //>>includeStart('debug', pragmas.debug);
  110. Check.defined("plane", plane);
  111. //>>includeEnd('debug');
  112. return BoundingSphere.intersectPlane(this._boundingSphere, plane);
  113. };
  114. /**
  115. * Update the bounding sphere after the tile is transformed.
  116. *
  117. * @param {Cartesian3} center The center of the bounding sphere.
  118. * @param {Number} radius The radius of the bounding sphere.
  119. */
  120. TileBoundingSphere.prototype.update = function (center, radius) {
  121. Cartesian3.clone(center, this._boundingSphere.center);
  122. this._boundingSphere.radius = radius;
  123. };
  124. /**
  125. * Creates a debug primitive that shows the outline of the sphere.
  126. *
  127. * @param {Color} color The desired color of the primitive's mesh
  128. * @return {Primitive}
  129. */
  130. TileBoundingSphere.prototype.createDebugVolume = function (color) {
  131. //>>includeStart('debug', pragmas.debug);
  132. Check.defined("color", color);
  133. //>>includeEnd('debug');
  134. const geometry = new SphereOutlineGeometry({
  135. radius: this.radius,
  136. });
  137. const modelMatrix = Matrix4.fromTranslation(
  138. this.center,
  139. new Matrix4.clone(Matrix4.IDENTITY)
  140. );
  141. const instance = new GeometryInstance({
  142. geometry: geometry,
  143. id: "outline",
  144. modelMatrix: modelMatrix,
  145. attributes: {
  146. color: ColorGeometryInstanceAttribute.fromColor(color),
  147. },
  148. });
  149. return new Primitive({
  150. geometryInstances: instance,
  151. appearance: new PerInstanceColorAppearance({
  152. translucent: false,
  153. flat: true,
  154. }),
  155. asynchronous: false,
  156. });
  157. };
  158. export default TileBoundingSphere;