babylon.renderTargetTexture.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.render = function () {
  18. if (this.onBeforeRender) {
  19. this.onBeforeRender();
  20. }
  21. var scene = this._scene;
  22. var engine = scene.getEngine();
  23. if (this._waitingRenderList) {
  24. this.renderList = [];
  25. for (var index = 0; index < this._waitingRenderList.length; index++) {
  26. var id = this._waitingRenderList[index];
  27. this.renderList.push(this._scene.getMeshByID(id));
  28. }
  29. delete this._waitingRenderList;
  30. }
  31. if (!this.renderList || this.renderList.length == 0) {
  32. return;
  33. }
  34. // Bind
  35. engine.bindFramebuffer(this._texture);
  36. // Clear
  37. engine.clear(scene.clearColor, true, true);
  38. // Dispatch subMeshes
  39. this._opaqueSubMeshes = [];
  40. this._transparentSubMeshes = [];
  41. this._alphaTestSubMeshes = [];
  42. for (var meshIndex = 0; meshIndex < this.renderList.length; meshIndex++) {
  43. var mesh = this.renderList[meshIndex];
  44. if (mesh.material && mesh.isEnabled() && mesh.isVisible) {
  45. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  46. var subMesh = mesh.subMeshes[subIndex];
  47. var material = subMesh.getMaterial();
  48. if (material.needAlphaTesting()) { // Alpha test
  49. this._alphaTestSubMeshes.push(subMesh);
  50. } else if (material.needAlphaBlending()) { // Transparent
  51. if (material.alpha > 0) {
  52. this._transparentSubMeshes.push(subMesh); // Opaque
  53. }
  54. } else {
  55. this._opaqueSubMeshes.push(subMesh);
  56. }
  57. }
  58. }
  59. }
  60. // Render
  61. scene._localRender(this._opaqueSubMeshes, this._alphaTestSubMeshes, this._transparentSubMeshes, this.renderList);
  62. // Unbind
  63. engine.unBindFramebuffer(this._texture);
  64. if (this.onAfterRender) {
  65. this.onAfterRender();
  66. }
  67. };
  68. })();