babylon.rectPackingMap.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: RectPackingMap, parent: PackedRect, pos: Vector2, size: Size) {
  8. this._pos = pos;
  9. this._size = size;
  10. this._root = root;
  11. this._parent = parent;
  12. this._contentSize = null;
  13. this._bottomNode = null;
  14. this._leftNode = null;
  15. this._initialSize = null;
  16. this._rightNode = null;
  17. }
  18. /**
  19. * @returns the position of this node into the map
  20. */
  21. public get pos(): Vector2 {
  22. return this._pos;
  23. }
  24. /**
  25. * @returns the size of the rectangle this node handles
  26. */
  27. public get contentSize(): Size {
  28. return this._contentSize;
  29. }
  30. /**
  31. * Retrieve the inner position (considering the margin) and stores it into the res object
  32. * @param res must be a valid Vector2 that will contain the inner position after this call
  33. */
  34. public getInnerPosToRef(res: Vector2) {
  35. let m = this._root._margin;
  36. res.x = this._pos.x + m;
  37. res.y = this._pos.y + m;
  38. }
  39. /**
  40. * Retrieve the inner size (considering the margin) and stores it into the res object
  41. * @param res must be a valid Size that will contain the inner size after this call
  42. */
  43. public getInnerSizeToRef(res: Size) {
  44. let m = this._root._margin;
  45. res.width = this._contentSize.width - (m*2);
  46. res.height = this._contentSize.height - (m*2);
  47. }
  48. /**
  49. * Compute the UV of the top/left, top/right, bottom/right, bottom/left points of the rectangle this node handles into the map
  50. * @returns And array of 4 Vector2, containing UV coordinates for the four corners of the Rectangle into the map
  51. */
  52. public get UVs(): Vector2[] {
  53. if (!this._contentSize) {
  54. throw new Error("Can't compute UVs for this object because it's nor allocated");
  55. }
  56. return this.getUVsForCustomSize(this._contentSize);
  57. }
  58. /**
  59. * You may have allocated the PackedRect using over-provisioning (you allocated more than you need in order to prevent frequent deallocations/reallocations)
  60. * and then using only a part of the PackRect.
  61. * This method will return the UVs for this part by given the custom size of what you really use
  62. * @param customSize must be less/equal to the allocated size, UV will be compute from this
  63. */
  64. public getUVsForCustomSize(customSize: Size): Vector2[] {
  65. var mainWidth = this._root._size.width;
  66. var mainHeight = this._root._size.height;
  67. let margin = this._root._margin;
  68. var topLeft = new Vector2((this._pos.x+margin) / mainWidth, (this._pos.y+margin) / mainHeight);
  69. var rightBottom = new Vector2((this._pos.x + customSize.width + margin - 1) / mainWidth, (this._pos.y + customSize.height + margin - 1) / mainHeight);
  70. var uvs = new Array<Vector2>();
  71. uvs.push(topLeft);
  72. uvs.push(new Vector2(rightBottom.x, topLeft.y));
  73. uvs.push(rightBottom);
  74. uvs.push(new Vector2(topLeft.x, rightBottom.y));
  75. return uvs;
  76. }
  77. /**
  78. * Free this rectangle from the map.
  79. * Call this method when you no longer need the rectangle to be in the map.
  80. */
  81. public freeContent() {
  82. if (!this.contentSize) {
  83. return;
  84. }
  85. this._contentSize = null;
  86. // If everything below is also free, reset the whole node, and attempt to reset parents if they also become free
  87. this.attemptDefrag();
  88. }
  89. protected get isUsed(): boolean {
  90. return this._contentSize != null || this._leftNode != null;
  91. }
  92. protected findAndSplitNode(contentSize: Size): PackedRect {
  93. var node = this.findNode(contentSize);
  94. // Not enough space...
  95. if (!node) {
  96. return null;
  97. }
  98. node.splitNode(contentSize);
  99. return node;
  100. }
  101. private findNode(size: Size): PackedRect {
  102. var resNode: PackedRect = null;
  103. let margin = this._root._margin * 2;
  104. // If this node is used, recurse to each of his subNodes to find an available one in its branch
  105. if (this.isUsed) {
  106. if (this._leftNode) {
  107. resNode = this._leftNode.findNode(size);
  108. }
  109. if (!resNode && this._rightNode) {
  110. resNode = this._rightNode.findNode(size);
  111. }
  112. if (!resNode && this._bottomNode) {
  113. resNode = this._bottomNode.findNode(size);
  114. }
  115. }
  116. // 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
  117. else if (this._initialSize) {
  118. if (((size.width+margin) <= this._initialSize.width) && ((size.height+margin) <= this._initialSize.height))
  119. {
  120. resNode = this;
  121. } else {
  122. return null;
  123. }
  124. }
  125. // The node is free and empty, rely on its size for the test
  126. else if (((size.width+margin) <= this._size.width) && ((size.height+margin) <= this._size.height)) {
  127. resNode = this;
  128. }
  129. return resNode;
  130. }
  131. private static TpsSize = Size.Zero();
  132. private splitNode(contentSize: Size): PackedRect {
  133. let cs = PackedRect.TpsSize;
  134. let margin = this._root._margin*2;
  135. cs.copyFrom(contentSize);
  136. cs.width += margin;
  137. cs.height += margin;
  138. // 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)
  139. if (!this._contentSize && this._initialSize) {
  140. this._contentSize = cs.clone();
  141. this._leftNode = new PackedRect(this._root, this, new Vector2(this._pos.x, this._pos.y), new Size(this._initialSize.width, this._initialSize.height));
  142. return this._leftNode.splitNode(contentSize);
  143. } else {
  144. this._contentSize = cs.clone();
  145. this._initialSize = cs.clone();
  146. if (cs.width !== this._size.width) {
  147. this._rightNode = new PackedRect(this._root, this, new Vector2(this._pos.x + cs.width, this._pos.y), new Size(this._size.width - cs.width, cs.height));
  148. }
  149. if (cs.height !== this._size.height) {
  150. this._bottomNode = new PackedRect(this._root, this, new Vector2(this._pos.x, this._pos.y + cs.height), new Size(this._size.width, this._size.height - cs.height));
  151. }
  152. return this;
  153. }
  154. }
  155. private attemptDefrag() {
  156. if (!this.isUsed && this.isRecursiveFree) {
  157. this.clearNode();
  158. if (this._parent) {
  159. this._parent.attemptDefrag();
  160. }
  161. }
  162. }
  163. private clearNode() {
  164. this._initialSize = null;
  165. this._rightNode = null;
  166. this._bottomNode = null;
  167. }
  168. private get isRecursiveFree() {
  169. return !this.contentSize && (!this._leftNode || this._leftNode.isRecursiveFree) && (!this._rightNode || this._rightNode.isRecursiveFree) && (!this._bottomNode || this._bottomNode.isRecursiveFree);
  170. }
  171. protected evalFreeSize(size: number): number {
  172. var levelSize = 0;
  173. if (!this.isUsed) {
  174. let margin = this._root._margin;
  175. let is = this._initialSize;
  176. if (is) {
  177. levelSize = is.surface - (is.width*margin) - (is.height*margin);
  178. } else {
  179. let size = this._size;
  180. levelSize = size.surface - (size.width*margin) - (size.height*margin);
  181. }
  182. }
  183. if (this._rightNode) {
  184. levelSize += this._rightNode.evalFreeSize(0);
  185. }
  186. if (this._bottomNode) {
  187. levelSize += this._bottomNode.evalFreeSize(0);
  188. }
  189. return levelSize + size;
  190. }
  191. protected _root: RectPackingMap;
  192. protected _parent: PackedRect;
  193. private _contentSize: Size;
  194. private _initialSize: Size;
  195. private _leftNode: PackedRect;
  196. private _rightNode: PackedRect;
  197. private _bottomNode: PackedRect;
  198. private _pos: Vector2;
  199. protected _size: Size;
  200. }
  201. /**
  202. * The purpose of this class is to pack several Rectangles into a big map, while trying to fit everything as optimally as possible.
  203. * This class is typically used to build lightmaps, sprite map or to pack several little textures into a big one.
  204. * 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.
  205. * In case you need a margin around the allocated rect, specify the amount in the margin argument during construction.
  206. * In such case you will have to rely on innerPositionToRef and innerSizeToRef calls to get the proper size
  207. */
  208. export class RectPackingMap extends PackedRect {
  209. /**
  210. * Create an instance of the object with a dimension using the given size
  211. * @param size The dimension of the rectangle that will contain all the sub ones.
  212. * @param margin The margin (empty space) created (in pixels) around the allocated Rectangles
  213. */
  214. constructor(size: Size, margin=0) {
  215. super(null, null, Vector2.Zero(), size);
  216. this._margin = margin;
  217. this._root = this;
  218. }
  219. /**
  220. * Add a rectangle, finding the best location to store it into the map
  221. * @param size the dimension of the rectangle to store
  222. * @return the Node containing the rectangle information, or null if we couldn't find a free spot
  223. */
  224. public addRect(size: Size): PackedRect {
  225. var node = this.findAndSplitNode(size);
  226. return node;
  227. }
  228. /**
  229. * Return the current space free normalized between [0;1]
  230. * @returns {}
  231. */
  232. public get freeSpace(): number {
  233. var freeSize = 0;
  234. freeSize = this.evalFreeSize(freeSize);
  235. return freeSize / (this._size.width * this._size.height);
  236. }
  237. public _margin: number;
  238. }
  239. }