babylon.renderTargetTexture.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. };
  11. BABYLON.RenderTargetTexture.prototype = Object.create(BABYLON.Texture.prototype);
  12. // Members
  13. BABYLON.RenderTargetTexture.prototype.isRenderTarget = true;
  14. BABYLON.RenderTargetTexture.prototype.coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  15. // Methods
  16. BABYLON.RenderTargetTexture.prototype.onBeforeRender = null;
  17. BABYLON.RenderTargetTexture.prototype.onAfterRender = null;
  18. BABYLON.RenderTargetTexture.prototype.resize = function(size, generateMipMaps) {
  19. this.releaseInternalTexture();
  20. this._texture = this._scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  21. };
  22. BABYLON.RenderTargetTexture.prototype.render = function () {
  23. if (this.onBeforeRender) {
  24. this.onBeforeRender();
  25. }
  26. var scene = this._scene;
  27. var engine = scene.getEngine();
  28. if (this._waitingRenderList) {
  29. this.renderList = [];
  30. for (var index = 0; index < this._waitingRenderList.length; index++) {
  31. var id = this._waitingRenderList[index];
  32. this.renderList.push(this._scene.getMeshByID(id));
  33. }
  34. delete this._waitingRenderList;
  35. }
  36. if (!this.renderList || this.renderList.length == 0) {
  37. return;
  38. }
  39. if (!this._opaqueSubMeshes) {
  40. // Smart arrays
  41. this._opaqueSubMeshes = new BABYLON.Tools.SmartArray(256);
  42. this._transparentSubMeshes = new BABYLON.Tools.SmartArray(256);
  43. this._alphaTestSubMeshes = new BABYLON.Tools.SmartArray(256);
  44. }
  45. // Bind
  46. engine.bindFramebuffer(this._texture);
  47. // Clear
  48. engine.clear(scene.clearColor, true, true);
  49. // Dispatch subMeshes
  50. this._opaqueSubMeshes.reset();
  51. this._transparentSubMeshes.reset();
  52. this._alphaTestSubMeshes.reset();
  53. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  54. var mesh = this.renderList[meshIndex];
  55. if (mesh.isEnabled() && mesh.isVisible) {
  56. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  57. var subMesh = mesh.subMeshes[subIndex];
  58. var material = subMesh.getMaterial();
  59. if (material.needAlphaTesting()) { // Alpha test
  60. this._alphaTestSubMeshes.push(subMesh);
  61. } else if (material.needAlphaBlending()) { // Transparent
  62. if (material.alpha > 0) {
  63. this._transparentSubMeshes.push(subMesh); // Opaque
  64. }
  65. } else {
  66. this._opaqueSubMeshes.push(subMesh);
  67. }
  68. }
  69. }
  70. }
  71. // Render
  72. if (this.customRenderFunction) {
  73. this.customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  74. } else {
  75. scene._localRender(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  76. }
  77. // Unbind
  78. engine.unBindFramebuffer(this._texture);
  79. if (this.onAfterRender) {
  80. this.onAfterRender();
  81. }
  82. };
  83. BABYLON.RenderTargetTexture.prototype.clone = function () {
  84. var textureSize = this.getSize();
  85. var newTexture = new BABYLON.DynamicTexture(this.name, textureSize.width, this._scene, this._generateMipMaps);
  86. // Base texture
  87. newTexture.hasAlpha = this.hasAlpha;
  88. newTexture.level = this.level;
  89. // RenderTarget Texture
  90. newTexture.coordinatesMode = this.coordinatesMode;
  91. newTexture.renderList = this.renderList.slice(0);
  92. return newTexture;
  93. };
  94. })();