CullingVolume.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartesian4 from './Cartesian4.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import DeveloperError from './DeveloperError.js';
  6. import Intersect from './Intersect.js';
  7. import Plane from './Plane.js';
  8. /**
  9. * The culling volume defined by planes.
  10. *
  11. * @alias CullingVolume
  12. * @constructor
  13. *
  14. * @param {Cartesian4[]} [planes] An array of clipping planes.
  15. */
  16. function CullingVolume(planes) {
  17. /**
  18. * Each plane is represented by a Cartesian4 object, where the x, y, and z components
  19. * define the unit vector normal to the plane, and the w component is the distance of the
  20. * plane from the origin.
  21. * @type {Cartesian4[]}
  22. * @default []
  23. */
  24. this.planes = defaultValue(planes, []);
  25. }
  26. var faces = [new Cartesian3(), new Cartesian3(), new Cartesian3()];
  27. Cartesian3.clone(Cartesian3.UNIT_X, faces[0]);
  28. Cartesian3.clone(Cartesian3.UNIT_Y, faces[1]);
  29. Cartesian3.clone(Cartesian3.UNIT_Z, faces[2]);
  30. var scratchPlaneCenter = new Cartesian3();
  31. var scratchPlaneNormal = new Cartesian3();
  32. var scratchPlane = new Plane(new Cartesian3(1.0, 0.0, 0.0), 0.0);
  33. /**
  34. * Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
  35. * The planes are aligned to the x, y, and z axes in world coordinates.
  36. *
  37. * @param {BoundingSphere} boundingSphere The bounding sphere used to create the culling volume.
  38. * @param {CullingVolume} [result] The object onto which to store the result.
  39. * @returns {CullingVolume} The culling volume created from the bounding sphere.
  40. */
  41. CullingVolume.fromBoundingSphere = function(boundingSphere, result) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (!defined(boundingSphere)) {
  44. throw new DeveloperError('boundingSphere is required.');
  45. }
  46. //>>includeEnd('debug');
  47. if (!defined(result)) {
  48. result = new CullingVolume();
  49. }
  50. var length = faces.length;
  51. var planes = result.planes;
  52. planes.length = 2 * length;
  53. var center = boundingSphere.center;
  54. var radius = boundingSphere.radius;
  55. var planeIndex = 0;
  56. for (var i = 0; i < length; ++i) {
  57. var faceNormal = faces[i];
  58. var plane0 = planes[planeIndex];
  59. var plane1 = planes[planeIndex + 1];
  60. if (!defined(plane0)) {
  61. plane0 = planes[planeIndex] = new Cartesian4();
  62. }
  63. if (!defined(plane1)) {
  64. plane1 = planes[planeIndex + 1] = new Cartesian4();
  65. }
  66. Cartesian3.multiplyByScalar(faceNormal, -radius, scratchPlaneCenter);
  67. Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
  68. plane0.x = faceNormal.x;
  69. plane0.y = faceNormal.y;
  70. plane0.z = faceNormal.z;
  71. plane0.w = -Cartesian3.dot(faceNormal, scratchPlaneCenter);
  72. Cartesian3.multiplyByScalar(faceNormal, radius, scratchPlaneCenter);
  73. Cartesian3.add(center, scratchPlaneCenter, scratchPlaneCenter);
  74. plane1.x = -faceNormal.x;
  75. plane1.y = -faceNormal.y;
  76. plane1.z = -faceNormal.z;
  77. plane1.w = -Cartesian3.dot(Cartesian3.negate(faceNormal, scratchPlaneNormal), scratchPlaneCenter);
  78. planeIndex += 2;
  79. }
  80. return result;
  81. };
  82. /**
  83. * Determines whether a bounding volume intersects the culling volume.
  84. *
  85. * @param {Object} boundingVolume The bounding volume whose intersection with the culling volume is to be tested.
  86. * @returns {Intersect} Intersect.OUTSIDE, Intersect.INTERSECTING, or Intersect.INSIDE.
  87. */
  88. CullingVolume.prototype.computeVisibility = function(boundingVolume) {
  89. //>>includeStart('debug', pragmas.debug);
  90. if (!defined(boundingVolume)) {
  91. throw new DeveloperError('boundingVolume is required.');
  92. }
  93. //>>includeEnd('debug');
  94. var planes = this.planes;
  95. var intersecting = false;
  96. for (var k = 0, len = planes.length; k < len; ++k) {
  97. var result = boundingVolume.intersectPlane(Plane.fromCartesian4(planes[k], scratchPlane));
  98. if (result === Intersect.OUTSIDE) {
  99. return Intersect.OUTSIDE;
  100. } else if (result === Intersect.INTERSECTING) {
  101. intersecting = true;
  102. }
  103. }
  104. return intersecting ? Intersect.INTERSECTING : Intersect.INSIDE;
  105. };
  106. /**
  107. * Determines whether a bounding volume intersects the culling volume.
  108. *
  109. * @param {Object} boundingVolume The bounding volume whose intersection with the culling volume is to be tested.
  110. * @param {Number} parentPlaneMask A bit mask from the boundingVolume's parent's check against the same culling
  111. * volume, such that if (planeMask & (1 << planeIndex) === 0), for k < 31, then
  112. * the parent (and therefore this) volume is completely inside plane[planeIndex]
  113. * and that plane check can be skipped.
  114. * @returns {Number} A plane mask as described above (which can be applied to this boundingVolume's children).
  115. *
  116. * @private
  117. */
  118. CullingVolume.prototype.computeVisibilityWithPlaneMask = function(boundingVolume, parentPlaneMask) {
  119. //>>includeStart('debug', pragmas.debug);
  120. if (!defined(boundingVolume)) {
  121. throw new DeveloperError('boundingVolume is required.');
  122. }
  123. if (!defined(parentPlaneMask)) {
  124. throw new DeveloperError('parentPlaneMask is required.');
  125. }
  126. //>>includeEnd('debug');
  127. if (parentPlaneMask === CullingVolume.MASK_OUTSIDE || parentPlaneMask === CullingVolume.MASK_INSIDE) {
  128. // parent is completely outside or completely inside, so this child is as well.
  129. return parentPlaneMask;
  130. }
  131. // Start with MASK_INSIDE (all zeros) so that after the loop, the return value can be compared with MASK_INSIDE.
  132. // (Because if there are fewer than 31 planes, the upper bits wont be changed.)
  133. var mask = CullingVolume.MASK_INSIDE;
  134. var planes = this.planes;
  135. for (var k = 0, len = planes.length; k < len; ++k) {
  136. // For k greater than 31 (since 31 is the maximum number of INSIDE/INTERSECTING bits we can store), skip the optimization.
  137. var flag = (k < 31) ? (1 << k) : 0;
  138. if (k < 31 && (parentPlaneMask & flag) === 0) {
  139. // boundingVolume is known to be INSIDE this plane.
  140. continue;
  141. }
  142. var result = boundingVolume.intersectPlane(Plane.fromCartesian4(planes[k], scratchPlane));
  143. if (result === Intersect.OUTSIDE) {
  144. return CullingVolume.MASK_OUTSIDE;
  145. } else if (result === Intersect.INTERSECTING) {
  146. mask |= flag;
  147. }
  148. }
  149. return mask;
  150. };
  151. /**
  152. * For plane masks (as used in {@link CullingVolume#computeVisibilityWithPlaneMask}), this special value
  153. * represents the case where the object bounding volume is entirely outside the culling volume.
  154. *
  155. * @type {Number}
  156. * @private
  157. */
  158. CullingVolume.MASK_OUTSIDE = 0xffffffff;
  159. /**
  160. * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
  161. * represents the case where the object bounding volume is entirely inside the culling volume.
  162. *
  163. * @type {Number}
  164. * @private
  165. */
  166. CullingVolume.MASK_INSIDE = 0x00000000;
  167. /**
  168. * For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
  169. * represents the case where the object bounding volume (may) intersect all planes of the culling volume.
  170. *
  171. * @type {Number}
  172. * @private
  173. */
  174. CullingVolume.MASK_INDETERMINATE = 0x7fffffff;
  175. export default CullingVolume;