babylon.renderTargetTexture.js 3.5 KB

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