babylon.octreeBlock.ts 3.1 KB

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