babylon.renderTargetTexture.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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._waitingRenderList) {
  85. this.renderList = [];
  86. for (var index = 0; index < this._waitingRenderList.length; index++) {
  87. var id = this._waitingRenderList[index];
  88. this.renderList.push(scene.getMeshByID(id));
  89. }
  90. delete this._waitingRenderList;
  91. }
  92. if (this.renderList && this.renderList.length === 0) {
  93. return;
  94. }
  95. // Bind
  96. if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
  97. engine.bindFramebuffer(this._texture);
  98. }
  99. // Clear
  100. engine.clear(scene.clearColor, true, true);
  101. this._renderingManager.reset();
  102. var currentRenderList = this.renderList ? this.renderList : scene.getActiveMeshes().data;
  103. for (var meshIndex = 0; meshIndex < currentRenderList.length; meshIndex++) {
  104. var mesh = currentRenderList[meshIndex];
  105. if (mesh) {
  106. if (!mesh.isReady() || (mesh.material && !mesh.material.isReady())) {
  107. // Reset _currentRefreshId
  108. this.resetRefreshCounter();
  109. continue;
  110. }
  111. if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) !== 0)) {
  112. mesh._activate(scene.getRenderId());
  113. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  114. var subMesh = mesh.subMeshes[subIndex];
  115. scene._activeVertices += subMesh.indexCount;
  116. this._renderingManager.dispatch(subMesh);
  117. }
  118. }
  119. }
  120. }
  121. if (!this._doNotChangeAspectRatio) {
  122. scene.updateTransformMatrix(true);
  123. }
  124. if (this.onBeforeRender) {
  125. this.onBeforeRender();
  126. }
  127. // Render
  128. this._renderingManager.render(this.customRenderFunction, currentRenderList, this.renderParticles, this.renderSprites);
  129. if (useCameraPostProcess) {
  130. scene.postProcessManager._finalizeFrame(false, this._texture);
  131. }
  132. if (this.onAfterRender) {
  133. this.onAfterRender();
  134. }
  135. // Unbind
  136. engine.unBindFramebuffer(this._texture);
  137. if (!this._doNotChangeAspectRatio) {
  138. scene.updateTransformMatrix(true);
  139. }
  140. };
  141. RenderTargetTexture.prototype.clone = function () {
  142. var textureSize = this.getSize();
  143. var newTexture = new RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  144. // Base texture
  145. newTexture.hasAlpha = this.hasAlpha;
  146. newTexture.level = this.level;
  147. // RenderTarget Texture
  148. newTexture.coordinatesMode = this.coordinatesMode;
  149. newTexture.renderList = this.renderList.slice(0);
  150. return newTexture;
  151. };
  152. return RenderTargetTexture;
  153. })(BABYLON.Texture);
  154. BABYLON.RenderTargetTexture = RenderTargetTexture;
  155. })(BABYLON || (BABYLON = {}));
  156. //# sourceMappingURL=babylon.renderTargetTexture.js.map