babylon.boundingInfo.js 4.3 KB

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