babylon.boundingInfo.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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._isLocked = false;
  25. this.boundingBox = new BABYLON.BoundingBox(minimum, maximum);
  26. this.boundingSphere = new BABYLON.BoundingSphere(minimum, maximum);
  27. }
  28. Object.defineProperty(BoundingInfo.prototype, "isLocked", {
  29. get: function () {
  30. return this._isLocked;
  31. },
  32. set: function (value) {
  33. this._isLocked = value;
  34. },
  35. enumerable: true,
  36. configurable: true
  37. });
  38. // Methods
  39. BoundingInfo.prototype.update = function (world) {
  40. if (this._isLocked) {
  41. return;
  42. }
  43. this.boundingBox._update(world);
  44. this.boundingSphere._update(world);
  45. };
  46. BoundingInfo.prototype.isInFrustum = function (frustumPlanes) {
  47. if (!this.boundingSphere.isInFrustum(frustumPlanes))
  48. return false;
  49. return this.boundingBox.isInFrustum(frustumPlanes);
  50. };
  51. BoundingInfo.prototype.isCompletelyInFrustum = function (frustumPlanes) {
  52. return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
  53. };
  54. BoundingInfo.prototype._checkCollision = function (collider) {
  55. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  56. };
  57. BoundingInfo.prototype.intersectsPoint = function (point) {
  58. if (!this.boundingSphere.centerWorld) {
  59. return false;
  60. }
  61. if (!this.boundingSphere.intersectsPoint(point)) {
  62. return false;
  63. }
  64. if (!this.boundingBox.intersectsPoint(point)) {
  65. return false;
  66. }
  67. return true;
  68. };
  69. BoundingInfo.prototype.intersects = function (boundingInfo, precise) {
  70. if (!this.boundingSphere.centerWorld || !boundingInfo.boundingSphere.centerWorld) {
  71. return false;
  72. }
  73. if (!BABYLON.BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
  74. return false;
  75. }
  76. if (!BABYLON.BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
  77. return false;
  78. }
  79. if (!precise) {
  80. return true;
  81. }
  82. var box0 = this.boundingBox;
  83. var box1 = boundingInfo.boundingBox;
  84. if (!axisOverlap(box0.directions[0], box0, box1))
  85. return false;
  86. if (!axisOverlap(box0.directions[1], box0, box1))
  87. return false;
  88. if (!axisOverlap(box0.directions[2], box0, box1))
  89. return false;
  90. if (!axisOverlap(box1.directions[0], box0, box1))
  91. return false;
  92. if (!axisOverlap(box1.directions[1], box0, box1))
  93. return false;
  94. if (!axisOverlap(box1.directions[2], box0, box1))
  95. return false;
  96. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1))
  97. return false;
  98. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1))
  99. return false;
  100. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1))
  101. return false;
  102. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1))
  103. return false;
  104. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1))
  105. return false;
  106. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1))
  107. return false;
  108. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1))
  109. return false;
  110. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1))
  111. return false;
  112. if (!axisOverlap(BABYLON.Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1))
  113. return false;
  114. return true;
  115. };
  116. return BoundingInfo;
  117. })();
  118. BABYLON.BoundingInfo = BoundingInfo;
  119. })(BABYLON || (BABYLON = {}));