babylon.octreeBlock.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. module BABYLON {
  2. /**
  3. * Class used to store a cell in an octree
  4. * @see http://doc.babylonjs.com/how_to/optimizing_your_scene_with_octrees
  5. */
  6. export class OctreeBlock<T> {
  7. /**
  8. * Gets the content of the current block
  9. */
  10. public entries = new Array<T>();
  11. /**
  12. * Gets the list of block children
  13. */
  14. public blocks: Array<OctreeBlock<T>>;
  15. private _depth: number;
  16. private _maxDepth: number;
  17. private _capacity: number;
  18. private _minPoint: Vector3;
  19. private _maxPoint: Vector3;
  20. private _boundingVectors = new Array<Vector3>();
  21. private _creationFunc: (entry: T, block: OctreeBlock<T>) => void;
  22. /**
  23. * Creates a new block
  24. * @param minPoint defines the minimum vector (in world space) of the block's bounding box
  25. * @param maxPoint defines the maximum vector (in world space) of the block's bounding box
  26. * @param capacity defines the maximum capacity of this block (if capacity is reached the block will be split into sub blocks)
  27. * @param depth defines the current depth of this block in the octree
  28. * @param maxDepth defines the maximal depth allowed (beyond this value, the capacity is ignored)
  29. * @param creationFunc defines a callback to call when an element is added to the block
  30. */
  31. constructor(minPoint: Vector3, maxPoint: Vector3, capacity: number, depth: number, maxDepth: number, creationFunc: (entry: T, block: OctreeBlock<T>) => void) {
  32. this._capacity = capacity;
  33. this._depth = depth;
  34. this._maxDepth = maxDepth;
  35. this._creationFunc = creationFunc;
  36. this._minPoint = minPoint;
  37. this._maxPoint = maxPoint;
  38. this._boundingVectors.push(minPoint.clone());
  39. this._boundingVectors.push(maxPoint.clone());
  40. this._boundingVectors.push(minPoint.clone());
  41. this._boundingVectors[2].x = maxPoint.x;
  42. this._boundingVectors.push(minPoint.clone());
  43. this._boundingVectors[3].y = maxPoint.y;
  44. this._boundingVectors.push(minPoint.clone());
  45. this._boundingVectors[4].z = maxPoint.z;
  46. this._boundingVectors.push(maxPoint.clone());
  47. this._boundingVectors[5].z = minPoint.z;
  48. this._boundingVectors.push(maxPoint.clone());
  49. this._boundingVectors[6].x = minPoint.x;
  50. this._boundingVectors.push(maxPoint.clone());
  51. this._boundingVectors[7].y = minPoint.y;
  52. }
  53. // Property
  54. /**
  55. * Gets the maximum capacity of this block (if capacity is reached the block will be split into sub blocks)
  56. */
  57. public get capacity(): number {
  58. return this._capacity;
  59. }
  60. /**
  61. * Gets the minimum vector (in world space) of the block's bounding box
  62. */
  63. public get minPoint(): Vector3 {
  64. return this._minPoint;
  65. }
  66. /**
  67. * Gets the maximum vector (in world space) of the block's bounding box
  68. */
  69. public get maxPoint(): Vector3 {
  70. return this._maxPoint;
  71. }
  72. // Methods
  73. /**
  74. * Add a new element to this block
  75. * @param entry defines the element to add
  76. */
  77. public addEntry(entry: T): void {
  78. if (this.blocks) {
  79. for (var index = 0; index < this.blocks.length; index++) {
  80. var block = this.blocks[index];
  81. block.addEntry(entry);
  82. }
  83. return;
  84. }
  85. this._creationFunc(entry, this);
  86. if (this.entries.length > this.capacity && this._depth < this._maxDepth) {
  87. this.createInnerBlocks();
  88. }
  89. }
  90. /**
  91. * Add an array of elements to this block
  92. * @param entries defines the array of elements to add
  93. */
  94. public addEntries(entries: T[]): void {
  95. for (var index = 0; index < entries.length; index++) {
  96. var mesh = entries[index];
  97. this.addEntry(mesh);
  98. }
  99. }
  100. /**
  101. * Test if the current block intersects the furstum planes and if yes, then add its content to the selection array
  102. * @param frustumPlanes defines the frustum planes to test
  103. * @param selection defines the array to store current content if selection is positive
  104. * @param allowDuplicate defines if the selection array can contains duplicated entries
  105. */
  106. public select(frustumPlanes: Plane[], selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
  107. if (BoundingBox.IsInFrustum(this._boundingVectors, frustumPlanes)) {
  108. if (this.blocks) {
  109. for (var index = 0; index < this.blocks.length; index++) {
  110. var block = this.blocks[index];
  111. block.select(frustumPlanes, selection, allowDuplicate);
  112. }
  113. return;
  114. }
  115. if (allowDuplicate) {
  116. selection.concat(this.entries);
  117. } else {
  118. selection.concatWithNoDuplicate(this.entries);
  119. }
  120. }
  121. }
  122. /**
  123. * Test if the current block intersect with the given bounding sphere and if yes, then add its content to the selection array
  124. * @param sphereCenter defines the bounding sphere center
  125. * @param sphereRadius defines the bounding sphere radius
  126. * @param selection defines the array to store current content if selection is positive
  127. * @param allowDuplicate defines if the selection array can contains duplicated entries
  128. */
  129. public intersects(sphereCenter: Vector3, sphereRadius: number, selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
  130. if (BoundingBox.IntersectsSphere(this._minPoint, this._maxPoint, sphereCenter, sphereRadius)) {
  131. if (this.blocks) {
  132. for (var index = 0; index < this.blocks.length; index++) {
  133. var block = this.blocks[index];
  134. block.intersects(sphereCenter, sphereRadius, selection, allowDuplicate);
  135. }
  136. return;
  137. }
  138. if (allowDuplicate) {
  139. selection.concat(this.entries);
  140. } else {
  141. selection.concatWithNoDuplicate(this.entries);
  142. }
  143. }
  144. }
  145. /**
  146. * Test if the current block intersect with the given ray and if yes, then add its content to the selection array
  147. * @param ray defines the ray to test with
  148. * @param selection defines the array to store current content if selection is positive
  149. */
  150. public intersectsRay(ray: Ray, selection: SmartArrayNoDuplicate<T>): void {
  151. if (ray.intersectsBoxMinMax(this._minPoint, this._maxPoint)) {
  152. if (this.blocks) {
  153. for (var index = 0; index < this.blocks.length; index++) {
  154. var block = this.blocks[index];
  155. block.intersectsRay(ray, selection);
  156. }
  157. return;
  158. }
  159. selection.concatWithNoDuplicate(this.entries);
  160. }
  161. }
  162. /**
  163. * Subdivide the content into child blocks (this block will then be empty)
  164. */
  165. public createInnerBlocks(): void {
  166. Octree._CreateBlocks(this._minPoint, this._maxPoint, this.entries, this._capacity, this._depth, this._maxDepth, this, this._creationFunc);
  167. }
  168. }
  169. }