BloomPass2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import {
  2. AdditiveBlending,
  3. Color,
  4. HalfFloatType,
  5. MeshBasicMaterial,
  6. ShaderMaterial,
  7. UniformsUtils,
  8. Vector2,
  9. Vector3,
  10. WebGLRenderTarget,
  11. NoBlending,
  12. NormalBlending
  13. } from 'three';
  14. import { Pass, FullScreenQuad } from './Pass.js';
  15. import { CopyShader } from '../shaders/CopyShader.js';
  16. import { LuminosityHighPassShader } from '../shaders/LuminosityHighPassShader.js';
  17. /**
  18. * UnrealBloomPass is inspired by the bloom pass of Unreal Engine. It creates a
  19. * mip map chain of bloom textures and blurs them with different radii. Because
  20. * of the weighted combination of mips, and because larger blurs are done on
  21. * higher mips, this effect provides good quality and performance.
  22. *
  23. * Reference:
  24. * - https://docs.unrealengine.com/latest/INT/Engine/Rendering/PostProcessEffects/Bloom/
  25. */
  26. class BloomPass extends Pass {
  27. constructor( width, height ) {
  28. super();
  29. this.strength = 1
  30. this.interation = 6;
  31. this.radius = 1;
  32. this.threshold = 1;
  33. this.width = ( width !== undefined ) ? width : 256
  34. this.height = ( height !== undefined ) ? height : 256
  35. this.clearColor = new Color( 0, 0, 0 );
  36. // render targets
  37. this.renderTargetsDownSample = [];
  38. this.renderTargetsUpSample = [];
  39. let resx = Math.round( this.width / 2 );
  40. let resy = Math.round( this.height / 2 );
  41. this.renderTargetBright = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } );
  42. this.renderTargetBright.texture.name = 'UnrealBloomPass.bright';
  43. this.renderTargetBright.texture.generateMipmaps = false;
  44. for(let i = 0; i < this.interation + 1; i++) {
  45. const rendererTargetDownSample = new WebGLRenderTarget(resx, resy, { type: HalfFloatType});
  46. rendererTargetDownSample.texture.name = 'DownSample_' + i;
  47. rendererTargetDownSample.texture.generateMipmaps = false;
  48. this.renderTargetsDownSample.push(rendererTargetDownSample);
  49. if(i == this.interation) break;
  50. const rendererTargetUpSample = new WebGLRenderTarget(resx, resy, { type: HalfFloatType});
  51. rendererTargetUpSample.texture.name = 'UpSample_' + i;
  52. rendererTargetUpSample.texture.generateMipmaps = false;
  53. this.renderTargetsUpSample.push(rendererTargetUpSample);
  54. resx = Math.round( resx / 2 );
  55. resy = Math.round( resy / 2 );
  56. }
  57. // console.log(this.renderTargetsDownSample, this.renderTargetsUpSample)
  58. // this.renderTargetDown = new WebGLRenderTarget( resx, resy, { type: HalfFloatType } );
  59. // this.renderTargetDown.texture.generateMipmaps = false;
  60. // luminosity high pass material
  61. const highPassShader = LuminosityHighPassShader;
  62. this.highPassUniforms = UniformsUtils.clone( highPassShader.uniforms );
  63. this.highPassUniforms[ 'luminosityThreshold' ].value = this.threshold;
  64. this.highPassUniforms[ 'smoothWidth' ].value = 0.01;
  65. this.materialHighPassFilter = new ShaderMaterial( {
  66. uniforms: this.highPassUniforms,
  67. vertexShader: highPassShader.vertexShader,
  68. fragmentShader: highPassShader.fragmentShader
  69. } );
  70. // blur material
  71. this.downSampleMaterial = new ShaderMaterial({
  72. uniforms: UniformsUtils.clone( DownSampleShader.uniforms ),
  73. vertexShader: DownSampleShader.vertexShader,
  74. fragmentShader: DownSampleShader.fragmentShader,
  75. blending: NoBlending
  76. })
  77. this.upSampleMaterial = new ShaderMaterial({
  78. uniforms: UniformsUtils.clone( UpSampleShader.uniforms ),
  79. vertexShader: UpSampleShader.vertexShader,
  80. fragmentShader: UpSampleShader.fragmentShader,
  81. blending: NoBlending
  82. })
  83. // blend material
  84. const copyShader = BlendShader;
  85. this.copyUniforms = UniformsUtils.clone( copyShader.uniforms );
  86. this.copyMaterial = new ShaderMaterial( {
  87. uniforms: this.copyUniforms,
  88. vertexShader: copyShader.vertexShader,
  89. fragmentShader: copyShader.fragmentShader,
  90. blending: AdditiveBlending,
  91. // depthTest: false,
  92. // depthWrite: false,
  93. // transparent: true
  94. } );
  95. this.enabled = true;
  96. this.needsSwap = false;
  97. this._oldClearColor = new Color();
  98. this.oldClearAlpha = 1;
  99. this.fsQuad = new FullScreenQuad( null );
  100. }
  101. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  102. renderer.getClearColor( this._oldClearColor );
  103. this.oldClearAlpha = renderer.getClearAlpha();
  104. const oldAutoClear = renderer.autoClear;
  105. renderer.autoClear = false;
  106. renderer.setClearColor( this.clearColor, 0 );
  107. // 1. Extract Bright Areas
  108. this.highPassUniforms[ 'tDiffuse' ].value = readBuffer.texture;
  109. this.highPassUniforms[ 'luminosityThreshold' ].value = this.threshold;
  110. this.fsQuad.material = this.materialHighPassFilter;
  111. renderer.setRenderTarget( this.renderTargetBright );
  112. renderer.clear();
  113. this.fsQuad.render( renderer );
  114. // 2. DownSample
  115. this.downSampleMaterial.uniforms[ 'offset' ].value = this.radius;
  116. for(let i = 0; i < this.interation + 1; i++) {
  117. let preTarget = this.renderTargetsDownSample[i-1];
  118. if(i == 0) {
  119. preTarget = this.renderTargetBright;
  120. }
  121. this.downSampleMaterial.uniforms[ 'tDiffuse' ].value = preTarget.texture
  122. this.downSampleMaterial.uniforms[ 'resolution' ].value.set(preTarget.width, preTarget.height);
  123. this.renderPass(renderer, this.downSampleMaterial, this.renderTargetsDownSample[i]);
  124. }
  125. // 3. UpSample
  126. this.upSampleMaterial.uniforms[ 'offset' ].value = this.radius;
  127. for(let i = this.interation - 1; i >= 0; i--) {
  128. let preTarget = this.renderTargetsUpSample[i + 1];
  129. let addTarget = this.renderTargetsDownSample[i]
  130. if(i == this.interation - 1) {
  131. preTarget = this.renderTargetsDownSample[i + 1];
  132. }
  133. this.upSampleMaterial.uniforms[ 'tDiffuse' ].value = preTarget.texture
  134. this.upSampleMaterial.uniforms[ 'tAddDiffuse' ].value = addTarget.texture
  135. this.upSampleMaterial.uniforms[ 'resolution' ].value.set(preTarget.width, preTarget.height);
  136. this.renderPass(renderer, this.upSampleMaterial, this.renderTargetsUpSample[i]);
  137. }
  138. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.renderTargetsUpSample[0].texture;
  139. this.copyMaterial.uniforms[ 'strength' ].value = this.strength;
  140. this.renderPass( renderer, this.copyMaterial, readBuffer );
  141. // Restore renderer settings
  142. renderer.setClearColor( this._oldClearColor, this.oldClearAlpha );
  143. renderer.autoClear = oldAutoClear;
  144. }
  145. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  146. // save original state
  147. renderer.getClearColor( this._oldClearColor );
  148. this.oldClearAlpha = renderer.getClearAlpha();
  149. const oldAutoClear = renderer.autoClear;
  150. renderer.setRenderTarget( renderTarget );
  151. // setup pass state
  152. renderer.autoClear = false;
  153. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  154. renderer.setClearColor( clearColor );
  155. renderer.setClearAlpha( clearAlpha || 0 );
  156. renderer.clear();
  157. }
  158. this.fsQuad.material = passMaterial;
  159. this.fsQuad.render( renderer );
  160. // restore original state
  161. renderer.autoClear = oldAutoClear;
  162. renderer.setClearColor( this._oldClearColor );
  163. renderer.setClearAlpha( this.oldClearAlpha );
  164. }
  165. dispose() {
  166. for ( let i = 0; i < this.renderTargetsDownSample.length; i ++ ) {
  167. this.renderTargetsDownSample[ i ].dispose();
  168. }
  169. for ( let i = 0; i < this.renderTargetsUpSample.length; i ++ ) {
  170. this.renderTargetsUpSample[ i ].dispose();
  171. }
  172. this.renderTargetBright.dispose();
  173. this.materialHighPassFilter.dispose();
  174. this.downSampleMaterial.dispose();
  175. this.upSampleMaterial.dispose();
  176. this.copyMaterial.dispose();
  177. this.fsQuad.dispose();
  178. }
  179. setSize( width, height ) {
  180. this.width = width;
  181. this.height = height;
  182. let resx = Math.round( width / 2 );
  183. let resy = Math.round( height / 2 );
  184. this.renderTargetBright.setSize( resx, resy ); //threshold
  185. for ( let i = 0; i < this.nMips; i ++ ) {
  186. this.renderTargetsDownSample[ i ].setSize( resx, resy );
  187. this.renderTargetsUpSample[ i ].setSize( resx, resy );
  188. resx = Math.round( resx / 2 );
  189. resy = Math.round( resy / 2 );
  190. }
  191. }
  192. }
  193. const DownSampleShader = {
  194. name: 'DownSampleShader',
  195. uniforms: {
  196. 'tDiffuse': { value: null },
  197. 'resolution': { value: new Vector2() },
  198. 'offset' : { value: 1.0 },
  199. },
  200. vertexShader: /* glsl */`
  201. varying vec2 vUv;
  202. void main() {
  203. vec4 modelViewPosition = modelViewMatrix * vec4( position, 1.0 );
  204. gl_Position = projectionMatrix * modelViewPosition;
  205. vUv = uv;
  206. }`,
  207. fragmentShader: /* glsl */`
  208. precision highp sampler2D;
  209. uniform sampler2D tDiffuse;
  210. uniform vec2 resolution;
  211. varying vec2 vUv;
  212. float GaussWeight2D(float x, float y, float sigma)
  213. {
  214. float PI = 3.14159265358;
  215. float E = 2.71828182846;
  216. float sigma_2 = pow(sigma, 2.0);
  217. float a = -(x*x + y*y) / (2.0 * sigma_2);
  218. return pow(E, a) / (2.0 * PI * sigma_2);
  219. }
  220. vec4 GaussNxN(sampler2D tex, vec2 uv, int n, vec2 stride, float sigma)
  221. {
  222. vec4 color = vec4(0.0);
  223. int r = n / 2;
  224. float weight = 0.0;
  225. for(int i=-r; i<=r; i++)
  226. {
  227. for(int j=-r; j<=r; j++)
  228. {
  229. float w = GaussWeight2D(float(i), float(j), sigma);
  230. vec2 coord = uv + vec2(i, j) * stride;
  231. color += texture2D(tex, coord) * w;
  232. weight += w;
  233. }
  234. }
  235. color /= weight;
  236. return color;
  237. }
  238. void main() {
  239. vec4 sum = vec4(0.0);
  240. vec2 stride = 1.0 / resolution;
  241. sum = GaussNxN(tDiffuse, vUv, 5, stride, 1.0);
  242. gl_FragColor = sum;
  243. }`
  244. };
  245. const UpSampleShader = {
  246. name: 'UpSampleShader',
  247. uniforms: {
  248. 'tDiffuse': { value: null },
  249. 'tAddDiffuse': { value: null },
  250. 'resolution': { value: new Vector2() },
  251. 'offset' : { value: 1.0 },
  252. },
  253. vertexShader: /* glsl */`
  254. varying vec2 vUv;
  255. void main() {
  256. vec4 modelViewPosition = modelViewMatrix * vec4( position, 1.0 );
  257. gl_Position = projectionMatrix * modelViewPosition;
  258. vUv = uv;
  259. }`,
  260. fragmentShader: /* glsl */`
  261. precision highp sampler2D;
  262. uniform sampler2D tDiffuse;
  263. uniform sampler2D tAddDiffuse;
  264. uniform vec2 resolution;
  265. uniform float offset;
  266. varying vec2 vUv;
  267. float GaussWeight2D(float x, float y, float sigma)
  268. {
  269. float PI = 3.14159265358;
  270. float E = 2.71828182846;
  271. float sigma_2 = pow(sigma, 2.0);
  272. float a = -(x*x + y*y) / (2.0 * sigma_2);
  273. return pow(E, a) / (2.0 * PI * sigma_2);
  274. }
  275. vec4 GaussNxN(sampler2D tex, vec2 uv, int n, vec2 stride, float sigma)
  276. {
  277. vec4 color = vec4(0.0);
  278. int r = n / 2;
  279. float weight = 0.0;
  280. for(int i=-r; i<=r; i++)
  281. {
  282. for(int j=-r; j<=r; j++)
  283. {
  284. float w = GaussWeight2D(float(i), float(j), sigma);
  285. vec2 coord = uv + vec2(i, j) * stride;
  286. color += texture2D(tex, coord) * w;
  287. weight += w;
  288. }
  289. }
  290. color /= weight;
  291. return color;
  292. }
  293. void main() {
  294. vec2 stride = 1.0 / resolution;
  295. vec2 curStride = 1.0 / resolution * 2.0;
  296. vec4 sum = GaussNxN(tDiffuse, vUv, 5, stride, offset);
  297. vec4 add = GaussNxN(tAddDiffuse, vUv, 5, stride, offset);
  298. gl_FragColor = sum + add;
  299. }`
  300. };
  301. const BlendShader = {
  302. name: 'BlendShader',
  303. uniforms: {
  304. 'tDiffuse': { value: null },
  305. 'strength': { value: 0.2 }
  306. },
  307. vertexShader: /* glsl */`
  308. varying vec2 vUv;
  309. void main() {
  310. vUv = uv;
  311. vec4 modelViewPosition = modelViewMatrix * vec4( position, 1.0 );
  312. gl_Position = projectionMatrix * modelViewPosition;
  313. }`,
  314. fragmentShader: /* glsl */`
  315. precision highp sampler2D;
  316. uniform sampler2D tDiffuse;
  317. uniform float strength;
  318. varying vec2 vUv;
  319. void main() {
  320. vec4 bloom = texture2D(tDiffuse, vUv);
  321. gl_FragColor = bloom * strength;
  322. // gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  323. }`
  324. };
  325. export { BloomPass };