babylon.renderTargetTexture.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var __extends = 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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var RenderTargetTexture = (function (_super) {
  10. __extends(RenderTargetTexture, _super);
  11. function RenderTargetTexture(name, size, scene, generateMipMaps, doNotChangeAspectRatio, type) {
  12. if (doNotChangeAspectRatio === void 0) { doNotChangeAspectRatio = true; }
  13. if (type === void 0) { type = BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT; }
  14. _super.call(this, null, scene, !generateMipMaps);
  15. this.renderList = new Array();
  16. this.renderParticles = true;
  17. this.renderSprites = false;
  18. this.coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  19. this._currentRefreshId = -1;
  20. this._refreshRate = 1;
  21. this.name = name;
  22. this.isRenderTarget = true;
  23. this._size = size;
  24. this._generateMipMaps = generateMipMaps;
  25. this._doNotChangeAspectRatio = doNotChangeAspectRatio;
  26. this._texture = scene.getEngine().createRenderTargetTexture(size, { generateMipMaps: generateMipMaps, type: type });
  27. // Rendering groups
  28. this._renderingManager = new BABYLON.RenderingManager(scene);
  29. }
  30. RenderTargetTexture.prototype.resetRefreshCounter = function () {
  31. this._currentRefreshId = -1;
  32. };
  33. Object.defineProperty(RenderTargetTexture.prototype, "refreshRate", {
  34. get: function () {
  35. return this._refreshRate;
  36. },
  37. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  38. set: function (value) {
  39. this._refreshRate = value;
  40. this.resetRefreshCounter();
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. RenderTargetTexture.prototype._shouldRender = function () {
  46. if (this._currentRefreshId === -1) {
  47. this._currentRefreshId = 1;
  48. return true;
  49. }
  50. if (this.refreshRate === this._currentRefreshId) {
  51. this._currentRefreshId = 1;
  52. return true;
  53. }
  54. this._currentRefreshId++;
  55. return false;
  56. };
  57. RenderTargetTexture.prototype.isReady = function () {
  58. if (!this.getScene().renderTargetsEnabled) {
  59. return false;
  60. }
  61. return _super.prototype.isReady.call(this);
  62. };
  63. RenderTargetTexture.prototype.getRenderSize = function () {
  64. return this._size;
  65. };
  66. Object.defineProperty(RenderTargetTexture.prototype, "canRescale", {
  67. get: function () {
  68. return true;
  69. },
  70. enumerable: true,
  71. configurable: true
  72. });
  73. RenderTargetTexture.prototype.scale = function (ratio) {
  74. var newSize = this._size * ratio;
  75. this.resize(newSize, this._generateMipMaps);
  76. };
  77. RenderTargetTexture.prototype.resize = function (size, generateMipMaps) {
  78. this.releaseInternalTexture();
  79. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  80. };
  81. RenderTargetTexture.prototype.render = function (useCameraPostProcess) {
  82. var scene = this.getScene();
  83. var engine = scene.getEngine();
  84. if (!this.activeCamera) {
  85. this.activeCamera = scene.activeCamera;
  86. }
  87. if (this._waitingRenderList) {
  88. this.renderList = [];
  89. for (var index = 0; index < this._waitingRenderList.length; index++) {
  90. var id = this._waitingRenderList[index];
  91. this.renderList.push(scene.getMeshByID(id));
  92. }
  93. delete this._waitingRenderList;
  94. }
  95. if (this.renderList && this.renderList.length === 0) {
  96. return;
  97. }
  98. // Bind
  99. if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
  100. engine.bindFramebuffer(this._texture);
  101. }
  102. // Clear
  103. engine.clear(scene.clearColor, true, true);
  104. this._renderingManager.reset();
  105. var currentRenderList = this.renderList ? this.renderList : scene.getActiveMeshes().data;
  106. for (var meshIndex = 0; meshIndex < currentRenderList.length; meshIndex++) {
  107. var mesh = currentRenderList[meshIndex];
  108. if (mesh) {
  109. if (!mesh.isReady()) {
  110. // Reset _currentRefreshId
  111. this.resetRefreshCounter();
  112. continue;
  113. }
  114. if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) !== 0)) {
  115. mesh._activate(scene.getRenderId());
  116. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  117. var subMesh = mesh.subMeshes[subIndex];
  118. scene._activeVertices += subMesh.indexCount;
  119. this._renderingManager.dispatch(subMesh);
  120. }
  121. }
  122. }
  123. }
  124. if (!this._doNotChangeAspectRatio) {
  125. scene.updateTransformMatrix(true);
  126. }
  127. if (this.onBeforeRender) {
  128. this.onBeforeRender();
  129. }
  130. // Render
  131. this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);
  132. if (useCameraPostProcess) {
  133. scene.postProcessManager._finalizeFrame(false, this._texture);
  134. }
  135. if (this.onAfterRender) {
  136. this.onAfterRender();
  137. }
  138. // Unbind
  139. engine.unBindFramebuffer(this._texture);
  140. if (!this._doNotChangeAspectRatio) {
  141. scene.updateTransformMatrix(true);
  142. }
  143. };
  144. RenderTargetTexture.prototype.clone = function () {
  145. var textureSize = this.getSize();
  146. var newTexture = new RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  147. // Base texture
  148. newTexture.hasAlpha = this.hasAlpha;
  149. newTexture.level = this.level;
  150. // RenderTarget Texture
  151. newTexture.coordinatesMode = this.coordinatesMode;
  152. newTexture.renderList = this.renderList.slice(0);
  153. return newTexture;
  154. };
  155. return RenderTargetTexture;
  156. })(BABYLON.Texture);
  157. BABYLON.RenderTargetTexture = RenderTargetTexture;
  158. })(BABYLON || (BABYLON = {}));
  159. //# sourceMappingURL=babylon.renderTargetTexture.js.map