babylon.boundingSphere.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module BABYLON {
  2. // This matrix is used as a value to reset the bounding box.
  3. const _identityMatrix = Matrix.Identity();
  4. /**
  5. * Class used to store bounding sphere information
  6. */
  7. export class BoundingSphere {
  8. /**
  9. * Gets the center of the bounding sphere in local space
  10. */
  11. public center = Vector3.Zero();
  12. /**
  13. * Radius of the bounding sphere in local space
  14. */
  15. public radius: number;
  16. /**
  17. * Gets the center of the bounding sphere in world space
  18. */
  19. public centerWorld = Vector3.Zero();
  20. /**
  21. * Radius of the bounding sphere in world space
  22. */
  23. public radiusWorld: number;
  24. /**
  25. * Gets the minimum vector in local space
  26. */
  27. public minimum = Vector3.Zero();
  28. /**
  29. * Gets the maximum vector in local space
  30. */
  31. public maximum = Vector3.Zero();
  32. /**
  33. * Creates a new bounding sphere
  34. * @param min defines the minimum vector (in local space)
  35. * @param max defines the maximum vector (in local space)
  36. */
  37. constructor(min: Vector3, max: Vector3) {
  38. this.reConstruct(min, max);
  39. }
  40. /**
  41. * Recreates the entire bounding sphere from scratch
  42. * @param min defines the new minimum vector (in local space)
  43. * @param max defines the new maximum vector (in local space)
  44. */
  45. public reConstruct(min: Vector3, max: Vector3) {
  46. this.minimum.copyFrom(min);
  47. this.maximum.copyFrom(max);
  48. var distance = Vector3.Distance(min, max);
  49. Vector3.LerpToRef(min, max, 0.5, this.center);
  50. this.radius = distance * 0.5;
  51. this.centerWorld.set(0, 0, 0);
  52. this._update(_identityMatrix);
  53. }
  54. /**
  55. * Scale the current bounding sphere by applying a scale factor
  56. * @param factor defines the scale factor to apply
  57. * @returns the current bounding box
  58. */
  59. public scale(factor: number): BoundingSphere {
  60. let newRadius = this.radius * factor;
  61. const tempRadiusVector = Tmp.Vector3[0].set(newRadius, newRadius, newRadius);
  62. let min = Tmp.Vector3[1].copyFrom(this.center).subtractInPlace(tempRadiusVector);
  63. let max = Tmp.Vector3[2].copyFrom(this.center).addInPlace(tempRadiusVector);
  64. this.reConstruct(min, max);
  65. return this;
  66. }
  67. // Methods
  68. /** @hidden */
  69. public _update(world: Matrix): void {
  70. Vector3.TransformCoordinatesToRef(this.center, world, this.centerWorld);
  71. const tempVector = Tmp.Vector3[0];
  72. Vector3.TransformNormalFromFloatsToRef(1.0, 1.0, 1.0, world, tempVector);
  73. this.radiusWorld = Math.max(Math.abs(tempVector.x), Math.abs(tempVector.y), Math.abs(tempVector.z)) * this.radius;
  74. }
  75. /**
  76. * Tests if the bounding sphere is intersecting the frustum planes
  77. * @param frustumPlanes defines the frustum planes to test
  78. * @returns true if there is an intersection
  79. */
  80. public isInFrustum(frustumPlanes: Plane[]): boolean {
  81. for (var i = 0; i < 6; i++) {
  82. if (frustumPlanes[i].dotCoordinate(this.centerWorld) <= -this.radiusWorld)
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * Tests if a point is inside the bounding sphere
  89. * @param point defines the point to test
  90. * @returns true if the point is inside the bounding sphere
  91. */
  92. public intersectsPoint(point: Vector3): boolean {
  93. var x = this.centerWorld.x - point.x;
  94. var y = this.centerWorld.y - point.y;
  95. var z = this.centerWorld.z - point.z;
  96. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  97. if (this.radiusWorld < distance)
  98. return false;
  99. return true;
  100. }
  101. // Statics
  102. /**
  103. * Checks if two sphere intersct
  104. * @param sphere0 sphere 0
  105. * @param sphere1 sphere 1
  106. * @returns true if the speres intersect
  107. */
  108. public static Intersects(sphere0: BoundingSphere, sphere1: BoundingSphere): boolean {
  109. var x = sphere0.centerWorld.x - sphere1.centerWorld.x;
  110. var y = sphere0.centerWorld.y - sphere1.centerWorld.y;
  111. var z = sphere0.centerWorld.z - sphere1.centerWorld.z;
  112. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  113. if (sphere0.radiusWorld + sphere1.radiusWorld < distance)
  114. return false;
  115. return true;
  116. }
  117. }
  118. }