octree.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Contains an array of blocks representing the octree
  3. */
  4. export interface IOctreeContainer<T> {
  5. /**
  6. * Blocks within the octree
  7. */
  8. blocks: Array<OctreeBlock<T>>;
  9. }
  10. /**
  11. * Octrees are a really powerful data structure that can quickly select entities based on space coordinates.
  12. * @see https://doc.babylonjs.com/how_to/optimizing_your_scene_with_octrees
  13. */
  14. export class Octree<T> {
  15. /**
  16. * Blocks within the octree containing objects
  17. */
  18. public blocks: Array<OctreeBlock<T>>;
  19. /**
  20. * Content stored in the octree
  21. */
  22. public dynamicContent = new Array<T>();
  23. private _maxBlockCapacity: number;
  24. private _selectionContent: SmartArrayNoDuplicate<T>;
  25. private _creationFunc: (entry: T, block: OctreeBlock<T>) => void;
  26. /**
  27. * Creates a octree
  28. * @see https://doc.babylonjs.com/how_to/optimizing_your_scene_with_octrees
  29. * @param creationFunc function to be used to instatiate the octree
  30. * @param maxBlockCapacity defines the maximum number of meshes you want on your octree's leaves (default: 64)
  31. * @param maxDepth defines the maximum depth (sub-levels) for your octree. Default value is 2, which means 8 8 8 = 512 blocks :) (This parameter takes precedence over capacity.)
  32. */
  33. constructor(creationFunc: (
  34. entry: T,
  35. block: OctreeBlock<T>) => void,
  36. maxBlockCapacity?: number,
  37. /** Defines the maximum depth (sub-levels) for your octree. Default value is 2, which means 8 8 8 = 512 blocks :) (This parameter takes precedence over capacity.) */
  38. public maxDepth = 2
  39. ) {
  40. this._maxBlockCapacity = maxBlockCapacity || 64;
  41. this._selectionContent = new SmartArrayNoDuplicate<T>(1024);
  42. this._creationFunc = creationFunc;
  43. }
  44. // Methods
  45. /**
  46. * Updates the octree by adding blocks for the passed in meshes within the min and max world parameters
  47. * @param worldMin worldMin for the octree blocks var blockSize = new Vector3((worldMax.x - worldMin.x) / 2, (worldMax.y - worldMin.y) / 2, (worldMax.z - worldMin.z) / 2);
  48. * @param worldMax worldMax for the octree blocks var blockSize = new Vector3((worldMax.x - worldMin.x) / 2, (worldMax.y - worldMin.y) / 2, (worldMax.z - worldMin.z) / 2);
  49. * @param entries meshes to be added to the octree blocks
  50. */
  51. public update(worldMin: Vector3, worldMax: Vector3, entries: T[]): void {
  52. Octree._CreateBlocks(worldMin, worldMax, entries, this._maxBlockCapacity, 0, this.maxDepth, this, this._creationFunc);
  53. }
  54. /**
  55. * Adds a mesh to the octree
  56. * @param entry Mesh to add to the octree
  57. */
  58. public addMesh(entry: T): void {
  59. for (var index = 0; index < this.blocks.length; index++) {
  60. var block = this.blocks[index];
  61. block.addEntry(entry);
  62. }
  63. }
  64. /**
  65. * Selects an array of meshes within the frustum
  66. * @param frustumPlanes The frustum planes to use which will select all meshes within it
  67. * @param allowDuplicate If duplicate objects are allowed in the resulting object array
  68. * @returns array of meshes within the frustum
  69. */
  70. public select(frustumPlanes: Plane[], allowDuplicate?: boolean): SmartArray<T> {
  71. this._selectionContent.reset();
  72. for (var index = 0; index < this.blocks.length; index++) {
  73. var block = this.blocks[index];
  74. block.select(frustumPlanes, this._selectionContent, allowDuplicate);
  75. }
  76. if (allowDuplicate) {
  77. this._selectionContent.concat(this.dynamicContent);
  78. } else {
  79. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  80. }
  81. return this._selectionContent;
  82. }
  83. /**
  84. * Test if the octree intersect with the given bounding sphere and if yes, then add its content to the selection array
  85. * @param sphereCenter defines the bounding sphere center
  86. * @param sphereRadius defines the bounding sphere radius
  87. * @param allowDuplicate defines if the selection array can contains duplicated entries
  88. * @returns an array of objects that intersect the sphere
  89. */
  90. public intersects(sphereCenter: Vector3, sphereRadius: number, allowDuplicate?: boolean): SmartArray<T> {
  91. this._selectionContent.reset();
  92. for (var index = 0; index < this.blocks.length; index++) {
  93. var block = this.blocks[index];
  94. block.intersects(sphereCenter, sphereRadius, this._selectionContent, allowDuplicate);
  95. }
  96. if (allowDuplicate) {
  97. this._selectionContent.concat(this.dynamicContent);
  98. } else {
  99. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  100. }
  101. return this._selectionContent;
  102. }
  103. /**
  104. * Test if the octree intersect with the given ray and if yes, then add its content to resulting array
  105. * @param ray defines the ray to test with
  106. * @returns array of intersected objects
  107. */
  108. public intersectsRay(ray: Ray): SmartArray<T> {
  109. this._selectionContent.reset();
  110. for (var index = 0; index < this.blocks.length; index++) {
  111. var block = this.blocks[index];
  112. block.intersectsRay(ray, this._selectionContent);
  113. }
  114. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  115. return this._selectionContent;
  116. }
  117. /**
  118. * @hidden
  119. */
  120. public static _CreateBlocks<T>(worldMin: Vector3, worldMax: Vector3, entries: T[], maxBlockCapacity: number, currentDepth: number, maxDepth: number, target: IOctreeContainer<T>, creationFunc: (entry: T, block: OctreeBlock<T>) => void): void {
  121. target.blocks = new Array<OctreeBlock<T>>();
  122. var blockSize = new Vector3((worldMax.x - worldMin.x) / 2, (worldMax.y - worldMin.y) / 2, (worldMax.z - worldMin.z) / 2);
  123. // Segmenting space
  124. for (var x = 0; x < 2; x++) {
  125. for (var y = 0; y < 2; y++) {
  126. for (var z = 0; z < 2; z++) {
  127. var localMin = worldMin.add(blockSize.multiplyByFloats(x, y, z));
  128. var localMax = worldMin.add(blockSize.multiplyByFloats(x + 1, y + 1, z + 1));
  129. var block = new OctreeBlock<T>(localMin, localMax, maxBlockCapacity, currentDepth + 1, maxDepth, creationFunc);
  130. block.addEntries(entries);
  131. target.blocks.push(block);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Adds a mesh into the octree block if it intersects the block
  138. */
  139. public static CreationFuncForMeshes = (entry: AbstractMesh, block: OctreeBlock<AbstractMesh>): void => {
  140. let boundingInfo = entry.getBoundingInfo();
  141. if (!entry.isBlocked && boundingInfo.boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  142. block.entries.push(entry);
  143. }
  144. }
  145. /**
  146. * Adds a submesh into the octree block if it intersects the block
  147. */
  148. public static CreationFuncForSubMeshes = (entry: SubMesh, block: OctreeBlock<SubMesh>): void => {
  149. let boundingInfo = entry.getBoundingInfo();
  150. if (boundingInfo.boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  151. block.entries.push(entry);
  152. }
  153. }
  154. }