babylon.octree.js 4.0 KB

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