babylon.rectPackingMap.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. return this.getUVsForCustomSize(this._root._size);
  31. }
  32. /**
  33. * You may have allocated the PackedRect using over-provisioning (you allocated more than you need in order to prevent frequent deallocations/reallocations) and then using only a part of the PackRect.
  34. * This method will return the UVs for this part by given the custom size of what you really use
  35. * @param customSize must be less/equal to the allocated size, UV will be compute from this
  36. */
  37. public getUVsForCustomSize(customSize: Size): Vector2[] {
  38. var mainWidth = this._root._size.width;
  39. var mainHeight = this._root._size.height;
  40. var topLeft = new Vector2(this._pos.x / mainWidth, this._pos.y / mainHeight);
  41. var rightBottom = new Vector2((this._pos.x + customSize.width - 1) / mainWidth, (this._pos.y + customSize.height - 1) / mainHeight);
  42. var uvs = new Array<Vector2>();
  43. uvs.push(topLeft);
  44. uvs.push(new Vector2(rightBottom.x, topLeft.y));
  45. uvs.push(rightBottom);
  46. uvs.push(new Vector2(topLeft.x, rightBottom.y));
  47. return uvs;
  48. }
  49. /**
  50. * Free this rectangle from the map.
  51. * Call this method when you no longer need the rectangle to be in the map.
  52. */
  53. public freeContent() {
  54. if (!this.contentSize) {
  55. return;
  56. }
  57. this._contentSize = null;
  58. // If everything below is also free, reset the whole node, and attempt to reset parents if they also become free
  59. this.attemptDefrag();
  60. }
  61. protected get isUsed(): boolean {
  62. return this._contentSize != null || this._leftNode != null;
  63. }
  64. protected findAndSplitNode(contentSize: Size): PackedRect {
  65. var node = this.findNode(contentSize);
  66. // Not enough space...
  67. if (!node) {
  68. return null;
  69. }
  70. node.splitNode(contentSize);
  71. return node;
  72. }
  73. private findNode(size: Size): PackedRect {
  74. var resNode: PackedRect = null;
  75. // If this node is used, recurse to each of his subNodes to find an available one in its branch
  76. if (this.isUsed) {
  77. if (this._leftNode) {
  78. resNode = this._leftNode.findNode(size);
  79. }
  80. if (!resNode && this._rightNode) {
  81. resNode = this._rightNode.findNode(size);
  82. }
  83. if (!resNode && this._bottomNode) {
  84. resNode = this._bottomNode.findNode(size);
  85. }
  86. }
  87. // 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
  88. else if (this._initialSize && (size.width <= this._initialSize.width) && (size.height <= this._initialSize.height)) {
  89. resNode = this;
  90. }
  91. // The node is free and empty, rely on its size for the test
  92. else if ((size.width <= this._size.width) && (size.height <= this._size.height)) {
  93. resNode = this;
  94. }
  95. return resNode;
  96. }
  97. private splitNode(contentSize: Size): PackedRect {
  98. // 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)
  99. if (!this._contentSize && this._initialSize) {
  100. this._leftNode = new PackedRect(this._root, this, new Vector2(this._pos.x, this._pos.y), new Size(this._initialSize.width, this._initialSize.height));
  101. return this._leftNode.splitNode(contentSize);
  102. } else {
  103. this._contentSize = contentSize.clone();
  104. this._initialSize = contentSize.clone();
  105. if (contentSize.width !== this._size.width) {
  106. 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));
  107. }
  108. if (contentSize.height !== this._size.height) {
  109. 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));
  110. }
  111. return this;
  112. }
  113. }
  114. private attemptDefrag() {
  115. if (!this.isUsed && this.isRecursiveFree) {
  116. this.clearNode();
  117. if (this._parent) {
  118. this._parent.attemptDefrag();
  119. }
  120. }
  121. }
  122. private clearNode() {
  123. this._initialSize = null;
  124. this._rightNode = null;
  125. this._bottomNode = null;
  126. }
  127. private get isRecursiveFree() {
  128. return !this.contentSize && (!this._leftNode || this._leftNode.isRecursiveFree) && (!this._rightNode || this._rightNode.isRecursiveFree) && (!this._bottomNode || this._bottomNode.isRecursiveFree);
  129. }
  130. protected evalFreeSize(size: number): number {
  131. var levelSize = 0;
  132. if (!this.isUsed) {
  133. if (this._initialSize) {
  134. levelSize = this._initialSize.surface;
  135. } else {
  136. levelSize = this._size.surface;
  137. }
  138. }
  139. if (this._rightNode) {
  140. levelSize += this._rightNode.evalFreeSize(0);
  141. }
  142. if (this._bottomNode) {
  143. levelSize += this._bottomNode.evalFreeSize(0);
  144. }
  145. return levelSize + size;
  146. }
  147. protected _root: PackedRect;
  148. protected _parent: PackedRect;
  149. private _contentSize: Size;
  150. private _initialSize: Size;
  151. private _leftNode: PackedRect;
  152. private _rightNode: PackedRect;
  153. private _bottomNode: PackedRect;
  154. private _pos: Vector2;
  155. protected _size: Size;
  156. }
  157. /**
  158. * The purpose of this class is to pack several Rectangles into a big map, while trying to fit everything as optimally as possible.
  159. * This class is typically used to build lightmaps, sprite map or to pack several little textures into a big one.
  160. * 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 life-cycle.
  161. */
  162. export class RectPackingMap extends PackedRect {
  163. /**
  164. * Create an instance of the object with a dimension using the given size
  165. * @param size The dimension of the rectangle that will contain all the sub ones.
  166. */
  167. constructor(size: Size) {
  168. super(null, null, Vector2.Zero(), size);
  169. this._root = this;
  170. }
  171. /**
  172. * Add a rectangle, finding the best location to store it into the map
  173. * @param size the dimension of the rectangle to store
  174. * @return the Node containing the rectangle information, or null if we couldn't find a free spot
  175. */
  176. public addRect(size: Size): PackedRect {
  177. var node = this.findAndSplitNode(size);
  178. return node;
  179. }
  180. /**
  181. * Return the current space free normalized between [0;1]
  182. * @returns {}
  183. */
  184. public get freeSpace(): number {
  185. var freeSize = 0;
  186. freeSize = this.evalFreeSize(freeSize);
  187. return freeSize / (this._size.width * this._size.height);
  188. }
  189. }
  190. }