You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TSDGLBloomEffect.js 6.6KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * TSDGLBloomEffect.js
  3. * Keynote HTML Player
  4. *
  5. * Created by Tungwei Cheng
  6. * Copyright (c) 2018 Apple Inc. All rights reserved.
  7. */
  8. var kShaderUniformBloomAmount = "BloomAmount";
  9. var kShaderUniformBlurTexture = "BlurTexture";
  10. var TSDGLBloomEffect = Class.create({
  11. initialize: function(gl) {
  12. this.gl = gl;
  13. },
  14. initWithEffectSize: function(effectSize, blurScale) {
  15. this._effectSize = effectSize;
  16. // blurScale must be >= 1.0
  17. this._blurBufferSize = CGSizeMake(Math.max(16, Math.ceil(effectSize.width / blurScale)), Math.max(16, Math.ceil(effectSize.height / blurScale)));;
  18. this.p_setupShaders();
  19. this.p_setupBuffers();
  20. },
  21. p_setupShaders: function() {
  22. var gl = this.gl;
  23. var _blurBufferSize = this._blurBufferSize;
  24. var blurTextureSize = WebGraphics.makePoint(1.0 / _blurBufferSize.width, 1.0 / _blurBufferSize.height);
  25. var blurTransform = WebGraphics.makeOrthoMatrix4(0, _blurBufferSize.width, 0, _blurBufferSize.height, -1, +1);
  26. // shader 1: horizontal blur shader
  27. var _blurHorizontalShader = this._blurHorizontalShader = new TSDGLShader(gl);
  28. _blurHorizontalShader.initWithDefaultHorizontalBlurShader();
  29. _blurHorizontalShader.setMat4WithTransform3D(blurTransform, kTSDGLShaderUniformMVPMatrix);
  30. _blurHorizontalShader.setPoint2D(blurTextureSize, kTSDGLShaderUniformTextureSize);
  31. // shader 2: vertical blur shader
  32. var _blurVerticalShader = this._blurVerticalShader = new TSDGLShader(gl);
  33. _blurVerticalShader.initWithDefaultVerticalBlurShader();
  34. _blurVerticalShader.setMat4WithTransform3D(blurTransform, kTSDGLShaderUniformMVPMatrix);
  35. _blurVerticalShader.setPoint2D(blurTextureSize, kTSDGLShaderUniformTextureSize);
  36. // shader 3: transfer shader
  37. var _fboTransferShader = this._fboTransferShader = new TSDGLShader(gl);
  38. _fboTransferShader.initWithDefaultTextureShader();
  39. _fboTransferShader.setMat4WithTransform3D(blurTransform, kTSDGLShaderUniformMVPMatrix);
  40. // shader 4: bloom effect shader
  41. var _bloomShader = this._bloomShader = new TSDGLShader(gl);
  42. _bloomShader.initWithShaderFileNames("bloom", "bloom");
  43. _bloomShader.setGLint(0, kTSDGLShaderUniformTexture);
  44. _bloomShader.setGLint(1, kShaderUniformBlurTexture);
  45. },
  46. p_setupBuffers: function() {
  47. var gl = this.gl;
  48. var _effectSize = this._effectSize;
  49. var _blurBufferSize = this._blurBufferSize;
  50. var meshSize = CGSizeMake(2, 2);
  51. var effectRect = CGRectMake(0, 0, _effectSize.width, _effectSize.height);
  52. var blurBufferRect = CGRectMake(0, 0, _blurBufferSize.width, _blurBufferSize.height);
  53. // buffer 1: bloom effect
  54. var _dataBuffer = this._dataBuffer = new TSDGLDataBuffer(gl);
  55. _dataBuffer.initWithVertexRect(effectRect, TSDRectUnit, meshSize, false, false);
  56. // buffer 2: blur buffer
  57. var _blurDataBuffer = this._blurDataBuffer = new TSDGLDataBuffer(gl);
  58. _blurDataBuffer.initWithVertexRect(blurBufferRect, CGRectZero, meshSize, true, false);
  59. // buffer 3: transfer buffer
  60. var _blurTransferDataBuffer = this._blurTransferDataBuffer = new TSDGLDataBuffer(gl);
  61. _blurTransferDataBuffer.initWithVertexRect(blurBufferRect, TSDRectUnit, meshSize, false, false);
  62. // initialize color framebuffer with one texture for storing incoming rendering
  63. this._colorFramebuffer = new TSDGLFrameBuffer(gl, _effectSize, 1);
  64. // initialize blur framebuffer with two textures for blurring operations
  65. this._blurFramebuffer = new TSDGLFrameBuffer(gl, _blurBufferSize, 2);
  66. },
  67. bindFramebuffer: function() {
  68. this._colorFramebuffer.bindFramebuffer();
  69. },
  70. unbindFramebufferAndBindGLFramebuffer: function(previousFramebuffer) {
  71. this._colorFramebuffer.unbindFramebufferAndBindGLFramebuffer(previousFramebuffer);
  72. },
  73. p_blurColorBufferWithPreviousFramebuffer: function(previousFramebuffer) {
  74. var gl = this.gl;
  75. var _blurFramebuffer = this._blurFramebuffer;
  76. var _blurBufferSize = this._blurBufferSize;
  77. _blurFramebuffer.bindFramebuffer();
  78. gl.clear(gl.COLOR_BUFFER_BIT);
  79. gl.viewport(0, 0, _blurBufferSize.width, _blurBufferSize.height);
  80. // Step 1: Transfer color to blur buffer
  81. gl.bindTexture(gl.TEXTURE_2D, this._colorFramebuffer.currentGLTexture());
  82. this._blurTransferDataBuffer.drawWithShader(this._fboTransferShader, true);
  83. // Step 2: Blur horizontally
  84. var blurTexture = _blurFramebuffer.currentGLTexture();
  85. _blurFramebuffer.setCurrentTextureToNext();
  86. gl.clear(gl.COLOR_BUFFER_BIT);
  87. gl.bindTexture(gl.TEXTURE_2D, blurTexture);
  88. this._blurDataBuffer.drawWithShader(this._blurHorizontalShader, true);
  89. // Step 3: Blur Vertically
  90. gl.bindTexture(gl.TEXTURE_2D, null);
  91. blurTexture = _blurFramebuffer.currentGLTexture();
  92. _blurFramebuffer.setCurrentTextureToNext();
  93. gl.clear(gl.COLOR_BUFFER_BIT);
  94. gl.bindTexture(gl.TEXTURE_2D, blurTexture);
  95. this._blurDataBuffer.drawWithShader(this._blurVerticalShader, true);
  96. _blurFramebuffer.unbindFramebufferAndBindGLFramebuffer(previousFramebuffer);
  97. gl.bindTexture(gl.TEXTURE_2D, null);
  98. },
  99. drawBloomEffectWithMVPMatrix: function(MVPMatrix, bloomAmount, currentGLFramebuffer) {
  100. var gl = this.gl;
  101. var _effectSize = this._effectSize;
  102. var oldViewportRect = gl.getParameter(gl.VIEWPORT);
  103. // Blur color buffer into blur FBO
  104. this.p_blurColorBufferWithPreviousFramebuffer(currentGLFramebuffer);
  105. // change viewport back to effect size
  106. gl.viewport(0, 0, _effectSize.width, _effectSize.height);
  107. // Draw Bloom
  108. gl.activeTexture(gl.TEXTURE1);
  109. gl.bindTexture(gl.TEXTURE_2D, this._blurFramebuffer.currentGLTexture());
  110. gl.activeTexture(gl.TEXTURE0);
  111. gl.bindTexture(gl.TEXTURE_2D, this._colorFramebuffer.currentGLTexture());
  112. var _bloomShader = this._bloomShader;
  113. _bloomShader.setMat4WithTransform3D(MVPMatrix, kTSDGLShaderUniformMVPMatrix);
  114. _bloomShader.setGLFloat(bloomAmount, kShaderUniformBloomAmount);
  115. this._dataBuffer.drawWithShader(_bloomShader, true);
  116. gl.activeTexture(gl.TEXTURE1);
  117. gl.bindTexture(gl.TEXTURE_2D, null);
  118. gl.activeTexture(gl.TEXTURE0);
  119. gl.bindTexture(gl.TEXTURE_2D, null);
  120. // change viewport back to original size
  121. gl.viewport(oldViewportRect[0], oldViewportRect[1], oldViewportRect[2], oldViewportRect[3]);
  122. }
  123. });