babylon.octreeBlock.js 2.8 KB

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