babylon.renderTargetTexture.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. };
  9. BABYLON.RenderTargetTexture.prototype = Object.create(BABYLON.Texture.prototype);
  10. // Members
  11. BABYLON.RenderTargetTexture.prototype.renderList = [];
  12. BABYLON.RenderTargetTexture.prototype.isRenderTarget = true;
  13. BABYLON.RenderTargetTexture.prototype.coordinatesMode = BABYLON.Texture.PROJECTION_MODE;
  14. // Methods
  15. BABYLON.RenderTargetTexture.prototype.onBeforeRender = null;
  16. BABYLON.RenderTargetTexture.prototype.onAfterRender = null;
  17. BABYLON.RenderTargetTexture.prototype.resize = function(size, generateMipMaps) {
  18. this.releaseInternalTexture();
  19. this._texture = this._scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  20. };
  21. BABYLON.RenderTargetTexture.prototype.render = function () {
  22. if (this.onBeforeRender) {
  23. this.onBeforeRender();
  24. }
  25. var scene = this._scene;
  26. var engine = scene.getEngine();
  27. if (this._waitingRenderList) {
  28. this.renderList = [];
  29. for (var index = 0; index < this._waitingRenderList.length; index++) {
  30. var id = this._waitingRenderList[index];
  31. this.renderList.push(this._scene.getMeshByID(id));
  32. }
  33. delete this._waitingRenderList;
  34. }
  35. if (!this.renderList || this.renderList.length == 0) {
  36. return;
  37. }
  38. // Bind
  39. engine.bindFramebuffer(this._texture);
  40. // Clear
  41. engine.clear(scene.clearColor, true, true);
  42. // Dispatch subMeshes
  43. this._opaqueSubMeshes = [];
  44. this._transparentSubMeshes = [];
  45. this._alphaTestSubMeshes = [];
  46. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  47. var mesh = this.renderList[meshIndex];
  48. if (mesh.material && mesh.isEnabled() && mesh.isVisible) {
  49. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  50. var subMesh = mesh.subMeshes[subIndex];
  51. var material = subMesh.getMaterial();
  52. if (material.needAlphaTesting()) { // Alpha test
  53. this._alphaTestSubMeshes.push(subMesh);
  54. } else if (material.needAlphaBlending()) { // Transparent
  55. if (material.alpha > 0) {
  56. this._transparentSubMeshes.push(subMesh); // Opaque
  57. }
  58. } else {
  59. this._opaqueSubMeshes.push(subMesh);
  60. }
  61. }
  62. }
  63. }
  64. // Render
  65. scene._localRender(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  66. // Unbind
  67. engine.unBindFramebuffer(this._texture);
  68. if (this.onAfterRender) {
  69. this.onAfterRender();
  70. }
  71. };
  72. })();