babylon.postProcess.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.PostProcess = function (name, fragmentUrl, parameters, samplers, ratio, camera) {
  4. this.name = name;
  5. this._camera = camera;
  6. this._scene = camera.getScene();
  7. camera.postProcesses.push(this);
  8. this._engine = this._scene.getEngine();
  9. this._renderRatio = ratio;
  10. this.width = -1;
  11. this.height = -1;
  12. samplers = samplers || [];
  13. samplers.push("textureSampler");
  14. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: fragmentUrl },
  15. ["position"],
  16. parameters || [],
  17. samplers, "");
  18. };
  19. // Methods
  20. BABYLON.PostProcess.prototype.onApply = null;
  21. BABYLON.PostProcess.prototype._onDispose = null;
  22. BABYLON.PostProcess.prototype.onSizeChanged = null;
  23. BABYLON.PostProcess.prototype.activate = function () {
  24. var desiredWidth = this._engine._renderingCanvas.width * this._renderRatio;
  25. var desiredHeight = this._engine._renderingCanvas.height * this._renderRatio;
  26. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  27. if (this._texture) {
  28. this._engine._releaseTexture(this._texture);
  29. this._texture = null;
  30. }
  31. this.width = desiredWidth;
  32. this.height = desiredHeight;
  33. this._texture = this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: this._camera.postProcesses.indexOf(this) === 0 });
  34. if (this.onSizeChanged) {
  35. this.onSizeChanged();
  36. }
  37. }
  38. this._engine.bindFramebuffer(this._texture);
  39. };
  40. BABYLON.PostProcess.prototype.apply = function () {
  41. // Check
  42. if (!this._effect.isReady())
  43. return null;
  44. // Render
  45. this._engine.enableEffect(this._effect);
  46. this._engine.setState(false);
  47. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  48. // Texture
  49. this._effect._bindTexture("textureSampler", this._texture);
  50. // Parameters
  51. if (this.onApply) {
  52. this.onApply(this._effect);
  53. }
  54. return this._effect;
  55. };
  56. BABYLON.PostProcess.prototype.dispose = function () {
  57. if (this._onDispose) {
  58. this._onDispose();
  59. }
  60. if (this._texture) {
  61. this._engine._releaseTexture(this._texture);
  62. this._texture = null;
  63. }
  64. var index = this._camera.postProcesses.indexOf(this);
  65. this._camera.postProcesses.splice(index, 1);
  66. };
  67. })();