babylon.renderTargetTexture.js 6.6 KB

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