babylon.boundingSphere.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.BoundingSphere = function (vertices, stride, start, count) {
  4. var minimum = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  5. var maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  6. for (var index = start; index < start + count; index += stride) {
  7. var current = new BABYLON.Vector3(vertices[index], vertices[index + 1], vertices[index + 2]);
  8. minimum = BABYLON.Vector3.Minimize(current, minimum);
  9. maximum = BABYLON.Vector3.Maximize(current, maximum);
  10. }
  11. var distance = BABYLON.Vector3.Distance(minimum, maximum);
  12. this.center = BABYLON.Vector3.Lerp(minimum, maximum, 0.5);;
  13. this.radius = distance * 0.5;
  14. };
  15. // Methods
  16. BABYLON.BoundingSphere.prototype._update = function (world, scale) {
  17. this.centerWorld = BABYLON.Vector3.TransformCoordinates(this.center, world);
  18. this.radiusWorld = this.radius * scale;
  19. };
  20. BABYLON.BoundingSphere.prototype.isInFrustrum = function (frustumPlanes) {
  21. for (var i = 0; i < 6; i++) {
  22. if (frustumPlanes[i].dotCoordinate(this.centerWorld) <= -this.radiusWorld)
  23. return false;
  24. }
  25. return true;
  26. };
  27. BABYLON.BoundingSphere.prototype.intersectsPoint = function(point) {
  28. var x = this.centerWorld.x - point.x;
  29. var y = this.centerWorld.y - point.y;
  30. var z = this.centerWorld.z - point.z;
  31. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  32. if (this.radiusWorld < distance)
  33. return false;
  34. return true;
  35. };
  36. // Statics
  37. BABYLON.BoundingSphere.intersects = function (sphere0, sphere1) {
  38. var x = sphere0.centerWorld.x - sphere1.centerWorld.x;
  39. var y = sphere0.centerWorld.y - sphere1.centerWorld.y;
  40. var z = sphere0.centerWorld.z - sphere1.centerWorld.z;
  41. var distance = Math.sqrt((x * x) + (y * y) + (z * z));
  42. if (sphere0.radiusWorld + sphere1.radiusWorld < distance)
  43. return false;
  44. return true;
  45. };
  46. })();