babylon.octreeBlock.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.OctreeBlock = function (minPoint, maxPoint, capacity) {
  4. this.subMeshes = [];
  5. this.meshes = [];
  6. this._capacity = capacity;
  7. this._minPoint = minPoint;
  8. this._maxPoint = maxPoint;
  9. this._boundingVectors = [];
  10. this._boundingVectors.push(minPoint.clone());
  11. this._boundingVectors.push(maxPoint.clone());
  12. this._boundingVectors.push(minPoint.clone());
  13. this._boundingVectors[2].x = maxPoint.x;
  14. this._boundingVectors.push(minPoint.clone());
  15. this._boundingVectors[3].y = maxPoint.y;
  16. this._boundingVectors.push(minPoint.clone());
  17. this._boundingVectors[4].z = maxPoint.z;
  18. this._boundingVectors.push(maxPoint.clone());
  19. this._boundingVectors[5].z = minPoint.z;
  20. this._boundingVectors.push(maxPoint.clone());
  21. this._boundingVectors[6].x = minPoint.x;
  22. this._boundingVectors.push(maxPoint.clone());
  23. this._boundingVectors[7].y = minPoint.y;
  24. };
  25. // Methods
  26. BABYLON.OctreeBlock.prototype.addMesh = function (mesh) {
  27. if (this.blocks) {
  28. for (var index = 0; index < this.blocks.length; index++) {
  29. var block = this.blocks[index];
  30. block.addMesh(mesh);
  31. }
  32. return;
  33. }
  34. if (mesh.getBoundingInfo().boundingBox.intersectsMinMax(this._minPoint, this._maxPoint)) {
  35. var localMeshIndex = this.meshes.length;
  36. this.meshes.push(mesh);
  37. this.subMeshes[localMeshIndex] = [];
  38. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  39. var subMesh = mesh.subMeshes[subIndex];
  40. if (mesh.subMeshes.length === 1 || subMesh.getBoundingInfo().boundingBox.intersectsMinMax(this._minPoint, this._maxPoint)) {
  41. this.subMeshes[localMeshIndex].push(subMesh);
  42. }
  43. }
  44. }
  45. if (this.subMeshes.length > this._capacity) {
  46. BABYLON.Octree._CreateBlocks(this._minPoint, this._maxPoint, this.meshes, this._capacity, this);
  47. }
  48. };
  49. BABYLON.OctreeBlock.prototype.addEntries = function (meshes) {
  50. for (var index = 0; index < meshes.length; index++) {
  51. var mesh = meshes[index];
  52. this.addMesh(mesh);
  53. }
  54. };
  55. BABYLON.OctreeBlock.prototype.select = function (frustumPlanes, selection) {
  56. if (this.blocks) {
  57. for (var index = 0; index < this.blocks.length; index++) {
  58. var block = this.blocks[index];
  59. block.select(frustumPlanes, selection);
  60. }
  61. return;
  62. }
  63. if (BABYLON.BoundingBox.IsInFrustrum(this._boundingVectors, frustumPlanes)) {
  64. selection.push(this);
  65. }
  66. };
  67. })();