babylon.octree.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Octree = (function () {
  4. function Octree(creationFunc, maxBlockCapacity, maxDepth) {
  5. if (maxDepth === void 0) { maxDepth = 2; }
  6. this.maxDepth = maxDepth;
  7. this.dynamicContent = new Array();
  8. this._maxBlockCapacity = maxBlockCapacity || 64;
  9. this._selectionContent = new BABYLON.SmartArray(1024);
  10. this._creationFunc = creationFunc;
  11. }
  12. // Methods
  13. Octree.prototype.update = function (worldMin, worldMax, entries) {
  14. Octree._CreateBlocks(worldMin, worldMax, entries, this._maxBlockCapacity, 0, this.maxDepth, this, this._creationFunc);
  15. };
  16. Octree.prototype.addMesh = function (entry) {
  17. for (var index = 0; index < this.blocks.length; index++) {
  18. var block = this.blocks[index];
  19. block.addEntry(entry);
  20. }
  21. };
  22. Octree.prototype.select = function (frustumPlanes, allowDuplicate) {
  23. this._selectionContent.reset();
  24. for (var index = 0; index < this.blocks.length; index++) {
  25. var block = this.blocks[index];
  26. block.select(frustumPlanes, this._selectionContent, allowDuplicate);
  27. }
  28. if (allowDuplicate) {
  29. this._selectionContent.concat(this.dynamicContent);
  30. }
  31. else {
  32. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  33. }
  34. return this._selectionContent;
  35. };
  36. Octree.prototype.intersects = function (sphereCenter, sphereRadius, allowDuplicate) {
  37. this._selectionContent.reset();
  38. for (var index = 0; index < this.blocks.length; index++) {
  39. var block = this.blocks[index];
  40. block.intersects(sphereCenter, sphereRadius, this._selectionContent, allowDuplicate);
  41. }
  42. if (allowDuplicate) {
  43. this._selectionContent.concat(this.dynamicContent);
  44. }
  45. else {
  46. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  47. }
  48. return this._selectionContent;
  49. };
  50. Octree.prototype.intersectsRay = function (ray) {
  51. this._selectionContent.reset();
  52. for (var index = 0; index < this.blocks.length; index++) {
  53. var block = this.blocks[index];
  54. block.intersectsRay(ray, this._selectionContent);
  55. }
  56. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  57. return this._selectionContent;
  58. };
  59. Octree._CreateBlocks = function (worldMin, worldMax, entries, maxBlockCapacity, currentDepth, maxDepth, target, creationFunc) {
  60. target.blocks = new Array();
  61. var blockSize = new BABYLON.Vector3((worldMax.x - worldMin.x) / 2, (worldMax.y - worldMin.y) / 2, (worldMax.z - worldMin.z) / 2);
  62. // Segmenting space
  63. for (var x = 0; x < 2; x++) {
  64. for (var y = 0; y < 2; y++) {
  65. for (var z = 0; z < 2; z++) {
  66. var localMin = worldMin.add(blockSize.multiplyByFloats(x, y, z));
  67. var localMax = worldMin.add(blockSize.multiplyByFloats(x + 1, y + 1, z + 1));
  68. var block = new BABYLON.OctreeBlock(localMin, localMax, maxBlockCapacity, currentDepth + 1, maxDepth, creationFunc);
  69. block.addEntries(entries);
  70. target.blocks.push(block);
  71. }
  72. }
  73. }
  74. };
  75. Octree.CreationFuncForMeshes = function (entry, block) {
  76. if (!entry.isBlocked && entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  77. block.entries.push(entry);
  78. }
  79. };
  80. Octree.CreationFuncForSubMeshes = function (entry, block) {
  81. if (entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  82. block.entries.push(entry);
  83. }
  84. };
  85. return Octree;
  86. }());
  87. BABYLON.Octree = Octree;
  88. })(BABYLON || (BABYLON = {}));