babylon.boundingInfo.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var computeBoxExtents = function (axis, box) {
  4. var p = BABYLON.Vector3.Dot(box.center, axis);
  5. var r0 = Math.abs(BABYLON.Vector3.Dot(box.directions[0], axis)) * box.extendSize.x;
  6. var r1 = Math.abs(BABYLON.Vector3.Dot(box.directions[1], axis)) * box.extendSize.y;
  7. var r2 = Math.abs(BABYLON.Vector3.Dot(box.directions[2], axis)) * box.extendSize.z;
  8. var r = r0 + r1 + r2;
  9. return {
  10. min: p - r,
  11. max: p + r
  12. };
  13. };
  14. var extentsOverlap = function (min0, max0, min1, max1) {
  15. return !(min0 > max1 || min1 > max0);
  16. };
  17. var axisOverlap = function (axis, box0, box1) {
  18. var result0 = computeBoxExtents(axis, box0);
  19. var result1 = computeBoxExtents(axis, box1);
  20. return extentsOverlap(result0.min, result0.max, result1.min, result1.max);
  21. };
  22. var BoundingInfo = (function () {
  23. function BoundingInfo(minimum, maximum) {
  24. this.minimum = minimum;
  25. this.maximum = maximum;
  26. this.boundingBox = new BABYLON.BoundingBox(minimum, maximum);
  27. this.boundingSphere = new BABYLON.BoundingSphere(minimum, maximum);
  28. }
  29. // Methods
  30. BoundingInfo.prototype._update = function (world) {
  31. this.boundingBox._update(world);
  32. this.boundingSphere._update(world);
  33. };
  34. BoundingInfo.prototype.isInFrustum = function (frustumPlanes) {
  35. if (!this.boundingSphere.isInFrustum(frustumPlanes))
  36. return false;
  37. return this.boundingBox.isInFrustum(frustumPlanes);
  38. };
  39. BoundingInfo.prototype.isCompletelyInFrustum = function (frustumPlanes) {
  40. return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
  41. };
  42. BoundingInfo.prototype._checkCollision = function (collider) {
  43. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  44. };
  45. BoundingInfo.prototype.intersectsPoint = function (point) {
  46. if (!this.boundingSphere.centerWorld) {
  47. return false;
  48. }
  49. if (!this.boundingSphere.intersectsPoint(point)) {
  50. return false;
  51. }
  52. if (!this.boundingBox.intersectsPoint(point)) {
  53. return false;
  54. }
  55. return true;
  56. };
  57. BoundingInfo.prototype.intersects = function (boundingInfo, precise) {
  58. if (!this.boundingSphere.centerWorld || !boundingInfo.boundingSphere.centerWorld) {
  59. return false;
  60. }
  61. if (!BABYLON.BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
  62. return false;
  63. }
  64. if (!BABYLON.BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
  65. return false;
  66. }
  67. if (!precise) {
  68. return true;
  69. }
  70. var box0 = this.boundingBox;
  71. var box1 = boundingInfo.boundingBox;
  72. if (!axisOverlap(box0.directions[0], box0, box1))
  73. return false;
  74. if (!axisOverlap(box0.directions[1], box0, box1))
  75. return false;
  76. if (!axisOverlap(box0.directions[2], box0, box1))
  77. return false;
  78. if (!axisOverlap(box1.directions[0], box0, box1))
  79. return false;
  80. if (!axisOverlap(box1.directions[1], box0, box1))
  81. return false;
  82. if (!axisOverlap(box1.directions[2], box0, box1))
  83. return false;
  84. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1))
  85. return false;
  86. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1))
  87. return false;
  88. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1))
  89. return false;
  90. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1))
  91. return false;
  92. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1))
  93. return false;
  94. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1))
  95. return false;
  96. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1))
  97. return false;
  98. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1))
  99. return false;
  100. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1))
  101. return false;
  102. return true;
  103. };
  104. return BoundingInfo;
  105. })();
  106. BABYLON.BoundingInfo = BoundingInfo;
  107. })(BABYLON || (BABYLON = {}));
  108. //# sourceMappingURL=babylon.boundingInfo.js.map