TileBoundingSphere.js 5.4 KB

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