babylon.octreeBlock.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 (!mesh.subMeshes) {
  32. return;
  33. }
  34. if (this.blocks) {
  35. for (var index = 0; index < this.blocks.length; index++) {
  36. var block = this.blocks[index];
  37. block.addMesh(mesh);
  38. }
  39. return;
  40. }
  41. if (mesh.getBoundingInfo().boundingBox.intersectsMinMax(this._minPoint, this._maxPoint)) {
  42. var localMeshIndex = this.meshes.length;
  43. this.meshes.push(mesh);
  44. this.subMeshes[localMeshIndex] = [];
  45. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  46. var subMesh = mesh.subMeshes[subIndex];
  47. if (mesh.subMeshes.length === 1 || subMesh.getBoundingInfo().boundingBox.intersectsMinMax(this._minPoint, this._maxPoint)) {
  48. this.subMeshes[localMeshIndex].push(subMesh);
  49. }
  50. }
  51. }
  52. if (this.subMeshes.length > this._capacity) {
  53. Octree._CreateBlocks(this._minPoint, this._maxPoint, this.meshes, this._capacity, this);
  54. }
  55. }
  56. public addEntries(meshes: Mesh[]): void {
  57. for (var index = 0; index < meshes.length; index++) {
  58. var mesh = meshes[index];
  59. this.addMesh(mesh);
  60. }
  61. }
  62. public select(frustumPlanes: Plane[], selection: OctreeBlock[]): void {
  63. if (this.blocks) {
  64. for (var index = 0; index < this.blocks.length; index++) {
  65. var block = this.blocks[index];
  66. block.select(frustumPlanes, selection);
  67. }
  68. return;
  69. }
  70. if (BABYLON.BoundingBox.IsInFrustum(this._boundingVectors, frustumPlanes)) {
  71. selection.push(this);
  72. }
  73. }
  74. }
  75. }