babylon.renderTargetTexture.js 3.6 KB

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