babylon.boundingInfo.js 5.1 KB

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