babylon.boundingInfo.js 4.3 KB

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