babylon.renderTargetTexture.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. RenderTargetTexture.prototype.resize = function (size, generateMipMaps) {
  60. this.releaseInternalTexture();
  61. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  62. };
  63. RenderTargetTexture.prototype.render = function (useCameraPostProcess) {
  64. var scene = this.getScene();
  65. var engine = scene.getEngine();
  66. if (this._waitingRenderList) {
  67. this.renderList = [];
  68. for (var index = 0; index < this._waitingRenderList.length; index++) {
  69. var id = this._waitingRenderList[index];
  70. this.renderList.push(scene.getMeshByID(id));
  71. }
  72. delete this._waitingRenderList;
  73. }
  74. if (!this.renderList || this.renderList.length == 0) {
  75. return;
  76. }
  77. // Bind
  78. if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
  79. engine.bindFramebuffer(this._texture);
  80. }
  81. // Clear
  82. engine.clear(scene.clearColor, true, true);
  83. this._renderingManager.reset();
  84. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  85. var mesh = this.renderList[meshIndex];
  86. if (mesh) {
  87. if (!mesh.isReady() || (mesh.material && !mesh.material.isReady())) {
  88. // Reset _currentRefreshId
  89. this.resetRefreshCounter();
  90. continue;
  91. }
  92. if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) != 0)) {
  93. mesh._activate(scene.getRenderId());
  94. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  95. var subMesh = mesh.subMeshes[subIndex];
  96. scene._activeVertices += subMesh.verticesCount;
  97. this._renderingManager.dispatch(subMesh);
  98. }
  99. }
  100. }
  101. }
  102. if (!this._doNotChangeAspectRatio) {
  103. scene.updateTransformMatrix(true);
  104. }
  105. if (this.onBeforeRender) {
  106. this.onBeforeRender();
  107. }
  108. // Render
  109. this._renderingManager.render(this.customRenderFunction, this.renderList, this.renderParticles, this.renderSprites);
  110. if (useCameraPostProcess) {
  111. scene.postProcessManager._finalizeFrame(false, this._texture);
  112. }
  113. if (this.onAfterRender) {
  114. this.onAfterRender();
  115. }
  116. // Unbind
  117. engine.unBindFramebuffer(this._texture);
  118. if (!this._doNotChangeAspectRatio) {
  119. scene.updateTransformMatrix(true);
  120. }
  121. };
  122. RenderTargetTexture.prototype.clone = function () {
  123. var textureSize = this.getSize();
  124. var newTexture = new BABYLON.RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  125. // Base texture
  126. newTexture.hasAlpha = this.hasAlpha;
  127. newTexture.level = this.level;
  128. // RenderTarget Texture
  129. newTexture.coordinatesMode = this.coordinatesMode;
  130. newTexture.renderList = this.renderList.slice(0);
  131. return newTexture;
  132. };
  133. return RenderTargetTexture;
  134. })(BABYLON.Texture);
  135. BABYLON.RenderTargetTexture = RenderTargetTexture;
  136. })(BABYLON || (BABYLON = {}));
  137. //# sourceMappingURL=babylon.renderTargetTexture.js.map