babylon.renderTargetTexture.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. module BABYLON {
  2. export class RenderTargetTexture extends Texture {
  3. public renderList = new Array<AbstractMesh>();
  4. public renderParticles = true;
  5. public renderSprites = false;
  6. public coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  7. public onBeforeRender: () => void;
  8. public onAfterRender: () => void;
  9. public activeCamera: Camera;
  10. public customRenderFunction: (opaqueSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, beforeTransparents?: () => void) => void;
  11. private _size: number;
  12. public _generateMipMaps: boolean;
  13. private _renderingManager: RenderingManager;
  14. public _waitingRenderList: string[];
  15. private _doNotChangeAspectRatio: boolean;
  16. private _currentRefreshId = -1;
  17. private _refreshRate = 1;
  18. constructor(name: string, size: any, scene: Scene, generateMipMaps?: boolean, doNotChangeAspectRatio: boolean = true) {
  19. super(null, scene, !generateMipMaps);
  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. public resetRefreshCounter(): void {
  30. this._currentRefreshId = -1;
  31. }
  32. public get refreshRate(): number {
  33. return this._refreshRate;
  34. }
  35. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  36. public set refreshRate(value: number) {
  37. this._refreshRate = value;
  38. this.resetRefreshCounter();
  39. }
  40. public _shouldRender(): boolean {
  41. if (this._currentRefreshId === -1) { // At least render once
  42. this._currentRefreshId = 1;
  43. return true;
  44. }
  45. if (this.refreshRate == this._currentRefreshId) {
  46. this._currentRefreshId = 1;
  47. return true;
  48. }
  49. this._currentRefreshId++;
  50. return false;
  51. }
  52. public isReady(): boolean {
  53. if (!this.getScene().renderTargetsEnabled) {
  54. return false;
  55. }
  56. return super.isReady();
  57. }
  58. public getRenderSize(): number {
  59. return this._size;
  60. }
  61. public get canRescale(): boolean {
  62. return true;
  63. }
  64. public scale(ratio: number): void {
  65. var newSize = this._size * ratio;
  66. this.resize(newSize, this._generateMipMaps);
  67. }
  68. public resize(size:any, generateMipMaps?: boolean) {
  69. this.releaseInternalTexture();
  70. this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
  71. }
  72. public render(useCameraPostProcess?: boolean) {
  73. var scene = this.getScene();
  74. var engine = scene.getEngine();
  75. if (this._waitingRenderList) {
  76. this.renderList = [];
  77. for (var index = 0; index < this._waitingRenderList.length; index++) {
  78. var id = this._waitingRenderList[index];
  79. this.renderList.push(scene.getMeshByID(id));
  80. }
  81. delete this._waitingRenderList;
  82. }
  83. if (!this.renderList) {
  84. return;
  85. }
  86. // Bind
  87. if (!useCameraPostProcess || !scene.postProcessManager._prepareFrame(this._texture)) {
  88. engine.bindFramebuffer(this._texture);
  89. }
  90. // Clear
  91. engine.clear(scene.clearColor, true, true);
  92. this._renderingManager.reset();
  93. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  94. var mesh = this.renderList[meshIndex];
  95. if (mesh) {
  96. if (!mesh.isReady() || (mesh.material && !mesh.material.isReady())) {
  97. // Reset _currentRefreshId
  98. this.resetRefreshCounter();
  99. continue;
  100. }
  101. if (mesh.isEnabled() && mesh.isVisible && mesh.subMeshes && ((mesh.layerMask & scene.activeCamera.layerMask) != 0)) {
  102. mesh._activate(scene.getRenderId());
  103. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  104. var subMesh = mesh.subMeshes[subIndex];
  105. scene._activeVertices += subMesh.verticesCount;
  106. this._renderingManager.dispatch(subMesh);
  107. }
  108. }
  109. }
  110. }
  111. if (!this._doNotChangeAspectRatio) {
  112. scene.updateTransformMatrix(true);
  113. }
  114. if (this.onBeforeRender) {
  115. this.onBeforeRender();
  116. }
  117. // Render
  118. this._renderingManager.render(this.customRenderFunction, this.renderList, this.renderParticles, this.renderSprites);
  119. if (useCameraPostProcess) {
  120. scene.postProcessManager._finalizeFrame(false, this._texture);
  121. }
  122. if (this.onAfterRender) {
  123. this.onAfterRender();
  124. }
  125. // Unbind
  126. engine.unBindFramebuffer(this._texture);
  127. if (!this._doNotChangeAspectRatio) {
  128. scene.updateTransformMatrix(true);
  129. }
  130. }
  131. public clone(): RenderTargetTexture {
  132. var textureSize = this.getSize();
  133. var newTexture = new BABYLON.RenderTargetTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  134. // Base texture
  135. newTexture.hasAlpha = this.hasAlpha;
  136. newTexture.level = this.level;
  137. // RenderTarget Texture
  138. newTexture.coordinatesMode = this.coordinatesMode;
  139. newTexture.renderList = this.renderList.slice(0);
  140. return newTexture;
  141. }
  142. }
  143. }