babylon.octree.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Octree = (function () {
  4. function Octree(creationFunc, maxBlockCapacity, maxDepth) {
  5. if (typeof maxDepth === "undefined") { 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. } else {
  31. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  32. }
  33. return this._selectionContent;
  34. };
  35. Octree.prototype.intersects = function (sphereCenter, sphereRadius, allowDuplicate) {
  36. this._selectionContent.reset();
  37. for (var index = 0; index < this.blocks.length; index++) {
  38. var block = this.blocks[index];
  39. block.intersects(sphereCenter, sphereRadius, this._selectionContent, allowDuplicate);
  40. }
  41. if (allowDuplicate) {
  42. this._selectionContent.concat(this.dynamicContent);
  43. } else {
  44. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  45. }
  46. return this._selectionContent;
  47. };
  48. Octree.prototype.intersectsRay = function (ray) {
  49. this._selectionContent.reset();
  50. for (var index = 0; index < this.blocks.length; index++) {
  51. var block = this.blocks[index];
  52. block.intersectsRay(ray, this._selectionContent);
  53. }
  54. this._selectionContent.concatWithNoDuplicate(this.dynamicContent);
  55. return this._selectionContent;
  56. };
  57. Octree._CreateBlocks = function (worldMin, worldMax, entries, maxBlockCapacity, currentDepth, maxDepth, target, creationFunc) {
  58. target.blocks = new Array();
  59. var blockSize = new BABYLON.Vector3((worldMax.x - worldMin.x) / 2, (worldMax.y - worldMin.y) / 2, (worldMax.z - worldMin.z) / 2);
  60. for (var x = 0; x < 2; x++) {
  61. for (var y = 0; y < 2; y++) {
  62. for (var z = 0; z < 2; z++) {
  63. var localMin = worldMin.add(blockSize.multiplyByFloats(x, y, z));
  64. var localMax = worldMin.add(blockSize.multiplyByFloats(x + 1, y + 1, z + 1));
  65. var block = new BABYLON.OctreeBlock(localMin, localMax, maxBlockCapacity, currentDepth + 1, maxDepth, creationFunc);
  66. block.addEntries(entries);
  67. target.blocks.push(block);
  68. }
  69. }
  70. }
  71. };
  72. Octree.CreationFuncForMeshes = function (entry, block) {
  73. if (!entry.isBlocked && entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  74. block.entries.push(entry);
  75. }
  76. };
  77. Octree.CreationFuncForSubMeshes = function (entry, block) {
  78. if (entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
  79. block.entries.push(entry);
  80. }
  81. };
  82. return Octree;
  83. })();
  84. BABYLON.Octree = Octree;
  85. })(BABYLON || (BABYLON = {}));
  86. //# sourceMappingURL=babylon.octree.js.map