babylon.mapTexture.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var MapTexture = (function (_super) {
  9. __extends(MapTexture, _super);
  10. function MapTexture(name, scene, size, samplingMode) {
  11. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  12. _super.call(this, null, scene, true, false, samplingMode);
  13. this.name = name;
  14. this._size = size;
  15. this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  16. this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  17. // Create the rectPackMap that will allocate portion of the texture
  18. this._rectPackingMap = new BABYLON.RectPackingMap(new BABYLON.Size(size.width, size.height));
  19. // Create the texture that will store the content
  20. this._texture = scene.getEngine().createRenderTargetTexture(size, { generateMipMaps: !this.noMipmap, type: BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT });
  21. }
  22. /**
  23. * Allocate a rectangle of a given size in the texture map
  24. * @param size the size of the rectangle to allocation
  25. * @return the PackedRect instance corresponding to the allocated rect or null is there was not enough space to allocate it.
  26. */
  27. MapTexture.prototype.allocateRect = function (size) {
  28. return this._rectPackingMap.addRect(size);
  29. };
  30. /**
  31. * Free a given rectangle from the texture map
  32. * @param rectInfo the instance corresponding to the rect to free.
  33. */
  34. MapTexture.prototype.freeRect = function (rectInfo) {
  35. if (rectInfo) {
  36. rectInfo.freeContent();
  37. }
  38. };
  39. Object.defineProperty(MapTexture.prototype, "freeSpace", {
  40. /**
  41. * Return the available space in the range of [O;1]. 0 being not space left at all, 1 being an empty texture map.
  42. * This is the cumulated space, not the biggest available surface. Due to fragmentation you may not allocate a rect corresponding to this surface.
  43. * @returns {}
  44. */
  45. get: function () {
  46. return this._rectPackingMap.freeSpace;
  47. },
  48. enumerable: true,
  49. configurable: true
  50. });
  51. /**
  52. * Bind the texture to the rendering engine to render in the zone of a given rectangle.
  53. * Use this method when you want to render into the texture map with a clipspace set to the location and size of the given rect.
  54. * Don't forget to call unbindTexture when you're done rendering
  55. * @param rect the zone to render to
  56. * @param clear true to clear the portion's color/depth data
  57. */
  58. MapTexture.prototype.bindTextureForRect = function (rect, clear) {
  59. return this.bindTextureForPosSize(rect.pos, rect.contentSize, clear);
  60. };
  61. /**
  62. * Bind the texture to the rendering engine to render in the zone of the given size at the given position.
  63. * Use this method when you want to render into the texture map with a clipspace set to the location and size of the given rect.
  64. * Don't forget to call unbindTexture when you're done rendering
  65. * @param pos the position into the texture
  66. * @param size the portion to fit the clip space to
  67. * @param clear true to clear the portion's color/depth data
  68. */
  69. MapTexture.prototype.bindTextureForPosSize = function (pos, size, clear) {
  70. var engine = this.getScene().getEngine();
  71. engine.bindFramebuffer(this._texture);
  72. this._replacedViewport = engine.setDirectViewport(pos.x, pos.y, size.width, size.height);
  73. if (clear) {
  74. // We only want to clear the part of the texture we're binding to, only the scissor can help us to achieve that
  75. engine.scissorClear(pos.x, pos.y, size.width, size.height, new BABYLON.Color4(0, 0, 0, 0));
  76. }
  77. };
  78. /**
  79. * Unbind the texture map from the rendering engine.
  80. * Call this method when you're done rendering. A previous call to bindTextureForRect has to be made.
  81. * @param dumpForDebug if set to true the content of the texture map will be dumped to a picture file that will be sent to the internet browser.
  82. */
  83. MapTexture.prototype.unbindTexture = function (dumpForDebug) {
  84. // Dump ?
  85. if (dumpForDebug) {
  86. BABYLON.Tools.DumpFramebuffer(this._size.width, this._size.height, this.getScene().getEngine());
  87. }
  88. var engine = this.getScene().getEngine();
  89. if (this._replacedViewport) {
  90. engine.setViewport(this._replacedViewport);
  91. this._replacedViewport = null;
  92. }
  93. engine.unBindFramebuffer(this._texture);
  94. };
  95. Object.defineProperty(MapTexture.prototype, "canRescale", {
  96. get: function () {
  97. return false;
  98. },
  99. enumerable: true,
  100. configurable: true
  101. });
  102. // Note, I don't know what behavior this method should have: clone the underlying texture/rectPackingMap or just reference them?
  103. // Anyway, there's not much point to use this method for this kind of texture I guess
  104. MapTexture.prototype.clone = function () {
  105. return null;
  106. };
  107. return MapTexture;
  108. })(BABYLON.Texture);
  109. BABYLON.MapTexture = MapTexture;
  110. })(BABYLON || (BABYLON = {}));