babylon.octreeBlock.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. module BABYLON {
  2. export class OctreeBlock {
  3. public meshes = new Array<Mesh>();
  4. public subMeshes = new Array <Array<SubMesh>>();
  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: 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. if (mesh.subMeshes) {
  43. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  44. var subMesh = mesh.subMeshes[subIndex];
  45. if (mesh.subMeshes.length === 1 || subMesh.getBoundingInfo().boundingBox.intersectsMinMax(this._minPoint, this._maxPoint)) {
  46. this.subMeshes[localMeshIndex].push(subMesh);
  47. }
  48. }
  49. }
  50. }
  51. if (this.subMeshes.length > this._capacity) {
  52. Octree._CreateBlocks(this._minPoint, this._maxPoint, this.meshes, this._capacity, this);
  53. }
  54. }
  55. public addEntries(meshes: Mesh[]): void {
  56. for (var index = 0; index < meshes.length; index++) {
  57. var mesh = meshes[index];
  58. this.addMesh(mesh);
  59. }
  60. }
  61. public select(frustumPlanes: Plane[], selection: OctreeBlock[]): void {
  62. if (this.blocks) {
  63. for (var index = 0; index < this.blocks.length; index++) {
  64. var block = this.blocks[index];
  65. block.select(frustumPlanes, selection);
  66. }
  67. return;
  68. }
  69. if (BABYLON.BoundingBox.IsInFrustum(this._boundingVectors, frustumPlanes)) {
  70. selection.push(this);
  71. }
  72. }
  73. }
  74. }