babylon.rectPackingMap.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. module BABYLON {
  2. /**
  3. * This class describe a rectangle that were added to the map.
  4. * You have access to its coordinates either in pixel or normalized (UV)
  5. */
  6. export class PackedRect {
  7. constructor(root: PackedRect, parent: PackedRect, pos: Vector2, size: Size) {
  8. this._pos = pos;
  9. this._size = size;
  10. this._root = root;
  11. this._parent = parent;
  12. }
  13. /**
  14. * @returns the position of this node into the map
  15. */
  16. public get pos() {
  17. return this._pos;
  18. }
  19. /**
  20. * @returns the size of the rectangle this node handles
  21. */
  22. public get contentSize(): Size {
  23. return this._contentSize;
  24. }
  25. /**
  26. * Compute the UV of the top/left, top/right, bottom/right, bottom/left points of the rectangle this node handles into the map
  27. * @returns And array of 4 Vector2, containing UV coordinates for the four corners of the Rectangle into the map
  28. */
  29. public get UVs(): Vector2[] {
  30. var mainWidth = this._root._size.width;
  31. var mainHeight = this._root._size.height;
  32. var topLeft = new Vector2(this._pos.x / mainWidth, this._pos.y / mainHeight);
  33. var rightBottom = new Vector2((this._pos.x + this._contentSize.width - 1) / mainWidth, (this._pos.y + this._contentSize.height - 1) / mainHeight);
  34. var uvs = new Array<Vector2>();
  35. uvs.push(topLeft);
  36. uvs.push(new Vector2(rightBottom.x, topLeft.y));
  37. uvs.push(rightBottom);
  38. uvs.push(new Vector2(topLeft.x, rightBottom.y));
  39. return uvs;
  40. }
  41. /**
  42. * Free this rectangle from the map.
  43. * Call this method when you no longer need the rectangle to be in the map.
  44. */
  45. public freeContent() {
  46. if (!this.contentSize) {
  47. return;
  48. }
  49. this._contentSize = null;
  50. // If everything below is also free, reset the whole node, and attempt to reset parents if they also become free
  51. this.attemptDefrag();
  52. }
  53. protected get isUsed(): boolean {
  54. return this._contentSize != null || this._leftNode != null;
  55. }
  56. protected findAndSplitNode(contentSize: Size): PackedRect {
  57. var node = this.findNode(contentSize);
  58. // Not enought space...
  59. if (!node) {
  60. return null;
  61. }
  62. node.splitNode(contentSize);
  63. return node;
  64. }
  65. private findNode(size: Size): PackedRect {
  66. var resNode: PackedRect = null;
  67. // If this node is used, recurse to each of his subNodes to find an available one in its branch
  68. if (this.isUsed) {
  69. if (this._leftNode) {
  70. resNode = this._leftNode.findNode(size);
  71. }
  72. if (!resNode && this._rightNode) {
  73. resNode = this._rightNode.findNode(size);
  74. }
  75. if (!resNode && this._bottomNode) {
  76. resNode = this._bottomNode.findNode(size);
  77. }
  78. }
  79. // The node is free, but was previously allocated (_initialSize is set), rely on initialSize to make the test as it's the space we have
  80. else if (this._initialSize && (size.width <= this._initialSize.width) && (size.height <= this._initialSize.height)) {
  81. resNode = this;
  82. }
  83. // The node is free and empty, rely on its size for the test
  84. else if ((size.width <= this._size.width) && (size.height <= this._size.height)) {
  85. resNode = this;
  86. }
  87. return resNode;
  88. }
  89. private splitNode(contentSize: Size): PackedRect {
  90. // If there's no contentSize but an initialSize it means this node were previously allocated, but freed, we need to create a _leftNode as subNode and use to allocate the space we need (and this node will have a right/bottom subNode for the space left as this._initialSize may be greater than contentSize)
  91. if (!this._contentSize && this._initialSize) {
  92. this._leftNode = new PackedRect(this._root, this, new Vector2(this._pos.x, this._pos.y), new Size(this._initialSize.width, this._initialSize.height));
  93. return this._leftNode.splitNode(contentSize);
  94. } else {
  95. this._contentSize = contentSize.clone();
  96. this._initialSize = contentSize.clone();
  97. if (contentSize.width !== this._size.width) {
  98. this._rightNode = new PackedRect(this._root, this, new Vector2(this._pos.x + contentSize.width, this._pos.y), new Size(this._size.width - contentSize.width, contentSize.height));
  99. }
  100. if (contentSize.height !== this._size.height) {
  101. this._bottomNode = new PackedRect(this._root, this, new Vector2(this._pos.x, this._pos.y + contentSize.height), new Size(this._size.width, this._size.height - contentSize.height));
  102. }
  103. return this;
  104. }
  105. }
  106. private attemptDefrag() {
  107. if (!this.isUsed && this.isRecursiveFree) {
  108. this.clearNode();
  109. if (this._parent) {
  110. this._parent.attemptDefrag();
  111. }
  112. }
  113. }
  114. private clearNode() {
  115. this._initialSize = null;
  116. this._rightNode = null;
  117. this._bottomNode = null;
  118. }
  119. private get isRecursiveFree() {
  120. return !this.contentSize && (!this._leftNode || this._leftNode.isRecursiveFree) && (!this._rightNode || this._rightNode.isRecursiveFree) && (!this._bottomNode || this._bottomNode.isRecursiveFree);
  121. }
  122. protected evalFreeSize(size: number): number {
  123. var levelSize = 0;
  124. if (!this.isUsed) {
  125. if (this._initialSize) {
  126. levelSize = this._initialSize.surface;
  127. } else {
  128. levelSize = this._size.surface;
  129. }
  130. }
  131. if (this._rightNode) {
  132. levelSize += this._rightNode.evalFreeSize(0);
  133. }
  134. if (this._bottomNode) {
  135. levelSize += this._bottomNode.evalFreeSize(0);
  136. }
  137. return levelSize + size;
  138. }
  139. protected _root: PackedRect;
  140. protected _parent: PackedRect;
  141. private _contentSize: Size;
  142. private _initialSize: Size;
  143. private _leftNode: PackedRect;
  144. private _rightNode: PackedRect;
  145. private _bottomNode: PackedRect;
  146. private _pos: Vector2;
  147. protected _size: Size;
  148. }
  149. /**
  150. * The purpose of this class is to pack several Rectangles into a big map, while trying to fit everything as optimaly as possible.
  151. * This class is typically used to build lightmaps, sprite map or to pack several little textures into a big one.
  152. * Note that this class allows allocated Rectangles to be freed: that is the map is dynamically maintained so you can add/remove rectangle based on their lifecycle.
  153. */
  154. export class RectPackingMap extends PackedRect {
  155. /**
  156. * Create an instance of the object with a dimension using the given size
  157. * @param size The dimension of the rectangle that will contain all the sub ones.
  158. */
  159. constructor(size: Size) {
  160. super(null, null, Vector2.Zero(), size);
  161. this._root = this;
  162. }
  163. /**
  164. * Add a rectangle, finding the best location to store it into the map
  165. * @param size the dimension of the rectangle to store
  166. * @return the Node containing the rectangle information, or null if we couldn't find a free spot
  167. */
  168. public addRect(size: Size): PackedRect {
  169. var node = this.findAndSplitNode(size);
  170. return node;
  171. }
  172. /**
  173. * Return the current space free normalized between [0;1]
  174. * @returns {}
  175. */
  176. public get freeSpace(): number {
  177. var freeSize = 0;
  178. freeSize = this.evalFreeSize(freeSize);
  179. return freeSize / (this._size.width * this._size.height);
  180. }
  181. }
  182. }