babylon.renderTargetTexture.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Bind
  40. engine.bindFramebuffer(this._texture);
  41. // Clear
  42. engine.clear(scene.clearColor, true, true);
  43. // Dispatch subMeshes
  44. this._opaqueSubMeshes = [];
  45. this._transparentSubMeshes = [];
  46. this._alphaTestSubMeshes = [];
  47. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  48. var mesh = this.renderList[meshIndex];
  49. if (mesh.isEnabled() && mesh.isVisible) {
  50. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  51. var subMesh = mesh.subMeshes[subIndex];
  52. var material = subMesh.getMaterial();
  53. if (material.needAlphaTesting()) { // Alpha test
  54. this._alphaTestSubMeshes.push(subMesh);
  55. } else if (material.needAlphaBlending()) { // Transparent
  56. if (material.alpha > 0) {
  57. this._transparentSubMeshes.push(subMesh); // Opaque
  58. }
  59. } else {
  60. this._opaqueSubMeshes.push(subMesh);
  61. }
  62. }
  63. }
  64. }
  65. // Render
  66. if (this.customRenderFunction) {
  67. this.customRenderFunction(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  68. } else {
  69. scene._localRender(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  70. }
  71. // Unbind
  72. engine.unBindFramebuffer(this._texture);
  73. if (this.onAfterRender) {
  74. this.onAfterRender();
  75. }
  76. };
  77. })();