babylon.renderTargetTexture.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.RenderTargetTexture = function (name, size, scene, generateMipMaps) {
  5. this._scene = scene;
  6. this._scene.textures.push(this);
  7. this.name = name;
  8. this._generateMipMaps = generateMipMaps;
  9. this._texture = scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  10. // Render list
  11. this.renderList = [];
  12. // Rendering groups
  13. this._renderingManager = new BABYLON.RenderingManager(scene);
  14. };
  15. BABYLON.RenderTargetTexture.prototype = Object.create(BABYLON.Texture.prototype);
  16. // Members
  17. BABYLON.RenderTargetTexture.prototype.renderParticles = true;
  18. BABYLON.RenderTargetTexture.prototype.renderSprites = false;
  19. BABYLON.RenderTargetTexture.prototype.isRenderTarget = true;
  20. BABYLON.RenderTargetTexture.prototype.coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  21. // Methods
  22. BABYLON.RenderTargetTexture.prototype.onBeforeRender = null;
  23. BABYLON.RenderTargetTexture.prototype.onAfterRender = null;
  24. BABYLON.RenderTargetTexture.prototype.resize = function (size, generateMipMaps) {
  25. this.releaseInternalTexture();
  26. this._texture = this._scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  27. };
  28. BABYLON.RenderTargetTexture.prototype.render = function () {
  29. if (this.onBeforeRender) {
  30. this.onBeforeRender();
  31. }
  32. var scene = this._scene;
  33. var engine = scene.getEngine();
  34. if (this._waitingRenderList) {
  35. this.renderList = [];
  36. for (var index = 0; index < this._waitingRenderList.length; index++) {
  37. var id = this._waitingRenderList[index];
  38. this.renderList.push(this._scene.getMeshByID(id));
  39. }
  40. delete this._waitingRenderList;
  41. }
  42. if (!this.renderList || this.renderList.length == 0) {
  43. if (this.onAfterRender) {
  44. this.onAfterRender();
  45. }
  46. return;
  47. }
  48. // Bind
  49. engine.bindFramebuffer(this._texture);
  50. // Clear
  51. engine.clear(scene.clearColor, true, true);
  52. this._renderingManager.reset();
  53. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  54. var mesh = this.renderList[meshIndex];
  55. if (mesh && mesh.isEnabled() && mesh.isVisible) {
  56. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  57. var subMesh = mesh.subMeshes[subIndex];
  58. scene._activeVertices += subMesh.verticesCount;
  59. this._renderingManager.dispatch(subMesh);
  60. }
  61. }
  62. }
  63. // Render
  64. this._renderingManager.render(this.customRenderFunction, this.renderList, this.renderParticles, this.renderSprites);
  65. // Unbind
  66. engine.unBindFramebuffer(this._texture);
  67. if (this.onAfterRender) {
  68. this.onAfterRender();
  69. }
  70. };
  71. BABYLON.RenderTargetTexture.prototype.clone = function () {
  72. var textureSize = this.getSize();
  73. var newTexture = new BABYLON.RenderTargetTexture(this.name, textureSize.width, this._scene, this._generateMipMaps);
  74. // Base texture
  75. newTexture.hasAlpha = this.hasAlpha;
  76. newTexture.level = this.level;
  77. // RenderTarget Texture
  78. newTexture.coordinatesMode = this.coordinatesMode;
  79. newTexture.renderList = this.renderList.slice(0);
  80. return newTexture;
  81. };
  82. })();