babylon.rectPackingMap.ts 9.3 KB

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