AdaptiveToneMappingPass.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * @author miibond
  3. * Generate a texture that represents the luminosity of the current scene, adapted over time
  4. * to simulate the optic nerve responding to the amount of light it is receiving.
  5. * Based on a GDC2007 presentation by Wolfgang Engel titled "Post-Processing Pipeline"
  6. *
  7. * Full-screen tone-mapping shader based on http://www.graphics.cornell.edu/~jaf/publications/sig02_paper.pdf
  8. */
  9. import {
  10. LinearFilter,
  11. LinearMipmapLinearFilter,
  12. MeshBasicMaterial,
  13. NoBlending,
  14. RGBAFormat,
  15. ShaderMaterial,
  16. UniformsUtils,
  17. WebGLRenderTarget
  18. } from "../../../build/three.module.js";
  19. import { Pass } from "../postprocessing/Pass.js";
  20. import { CopyShader } from "../shaders/CopyShader.js";
  21. import { LuminosityShader } from "../shaders/LuminosityShader.js";
  22. import { ToneMapShader } from "../shaders/ToneMapShader.js";
  23. var AdaptiveToneMappingPass = function ( adaptive, resolution ) {
  24. Pass.call( this );
  25. this.resolution = ( resolution !== undefined ) ? resolution : 256;
  26. this.needsInit = true;
  27. this.adaptive = adaptive !== undefined ? !! adaptive : true;
  28. this.luminanceRT = null;
  29. this.previousLuminanceRT = null;
  30. this.currentLuminanceRT = null;
  31. if ( CopyShader === undefined )
  32. console.error( "AdaptiveToneMappingPass relies on CopyShader" );
  33. var copyShader = CopyShader;
  34. this.copyUniforms = UniformsUtils.clone( copyShader.uniforms );
  35. this.materialCopy = new ShaderMaterial( {
  36. uniforms: this.copyUniforms,
  37. vertexShader: copyShader.vertexShader,
  38. fragmentShader: copyShader.fragmentShader,
  39. blending: NoBlending,
  40. depthTest: false
  41. } );
  42. if ( LuminosityShader === undefined )
  43. console.error( "AdaptiveToneMappingPass relies on LuminosityShader" );
  44. this.materialLuminance = new ShaderMaterial( {
  45. uniforms: UniformsUtils.clone( LuminosityShader.uniforms ),
  46. vertexShader: LuminosityShader.vertexShader,
  47. fragmentShader: LuminosityShader.fragmentShader,
  48. blending: NoBlending
  49. } );
  50. this.adaptLuminanceShader = {
  51. defines: {
  52. "MIP_LEVEL_1X1": ( Math.log( this.resolution ) / Math.log( 2.0 ) ).toFixed( 1 )
  53. },
  54. uniforms: {
  55. "lastLum": { value: null },
  56. "currentLum": { value: null },
  57. "minLuminance": { value: 0.01 },
  58. "delta": { value: 0.016 },
  59. "tau": { value: 1.0 }
  60. },
  61. vertexShader: [
  62. "varying vec2 vUv;",
  63. "void main() {",
  64. " vUv = uv;",
  65. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  66. "}"
  67. ].join( '\n' ),
  68. fragmentShader: [
  69. "varying vec2 vUv;",
  70. "uniform sampler2D lastLum;",
  71. "uniform sampler2D currentLum;",
  72. "uniform float minLuminance;",
  73. "uniform float delta;",
  74. "uniform float tau;",
  75. "void main() {",
  76. " vec4 lastLum = texture2D( lastLum, vUv, MIP_LEVEL_1X1 );",
  77. " vec4 currentLum = texture2D( currentLum, vUv, MIP_LEVEL_1X1 );",
  78. " float fLastLum = max( minLuminance, lastLum.r );",
  79. " float fCurrentLum = max( minLuminance, currentLum.r );",
  80. //The adaption seems to work better in extreme lighting differences
  81. //if the input luminance is squared.
  82. " fCurrentLum *= fCurrentLum;",
  83. // Adapt the luminance using Pattanaik's technique
  84. " float fAdaptedLum = fLastLum + (fCurrentLum - fLastLum) * (1.0 - exp(-delta * tau));",
  85. // "fAdaptedLum = sqrt(fAdaptedLum);",
  86. " gl_FragColor.r = fAdaptedLum;",
  87. "}"
  88. ].join( '\n' )
  89. };
  90. this.materialAdaptiveLum = new ShaderMaterial( {
  91. uniforms: UniformsUtils.clone( this.adaptLuminanceShader.uniforms ),
  92. vertexShader: this.adaptLuminanceShader.vertexShader,
  93. fragmentShader: this.adaptLuminanceShader.fragmentShader,
  94. defines: Object.assign( {}, this.adaptLuminanceShader.defines ),
  95. blending: NoBlending
  96. } );
  97. if ( ToneMapShader === undefined )
  98. console.error( "AdaptiveToneMappingPass relies on ToneMapShader" );
  99. this.materialToneMap = new ShaderMaterial( {
  100. uniforms: UniformsUtils.clone( ToneMapShader.uniforms ),
  101. vertexShader: ToneMapShader.vertexShader,
  102. fragmentShader: ToneMapShader.fragmentShader,
  103. blending: NoBlending
  104. } );
  105. this.fsQuad = new Pass.FullScreenQuad( null );
  106. };
  107. AdaptiveToneMappingPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  108. constructor: AdaptiveToneMappingPass,
  109. render: function ( renderer, writeBuffer, readBuffer, deltaTime/*, maskActive*/ ) {
  110. if ( this.needsInit ) {
  111. this.reset( renderer );
  112. this.luminanceRT.texture.type = readBuffer.texture.type;
  113. this.previousLuminanceRT.texture.type = readBuffer.texture.type;
  114. this.currentLuminanceRT.texture.type = readBuffer.texture.type;
  115. this.needsInit = false;
  116. }
  117. if ( this.adaptive ) {
  118. //Render the luminance of the current scene into a render target with mipmapping enabled
  119. this.fsQuad.material = this.materialLuminance;
  120. this.materialLuminance.uniforms.tDiffuse.value = readBuffer.texture;
  121. renderer.setRenderTarget( this.currentLuminanceRT );
  122. this.fsQuad.render( renderer );
  123. //Use the new luminance values, the previous luminance and the frame delta to
  124. //adapt the luminance over time.
  125. this.fsQuad.material = this.materialAdaptiveLum;
  126. this.materialAdaptiveLum.uniforms.delta.value = deltaTime;
  127. this.materialAdaptiveLum.uniforms.lastLum.value = this.previousLuminanceRT.texture;
  128. this.materialAdaptiveLum.uniforms.currentLum.value = this.currentLuminanceRT.texture;
  129. renderer.setRenderTarget( this.luminanceRT );
  130. this.fsQuad.render( renderer );
  131. //Copy the new adapted luminance value so that it can be used by the next frame.
  132. this.fsQuad.material = this.materialCopy;
  133. this.copyUniforms.tDiffuse.value = this.luminanceRT.texture;
  134. renderer.setRenderTarget( this.previousLuminanceRT );
  135. this.fsQuad.render( renderer );
  136. }
  137. this.fsQuad.material = this.materialToneMap;
  138. this.materialToneMap.uniforms.tDiffuse.value = readBuffer.texture;
  139. if ( this.renderToScreen ) {
  140. renderer.setRenderTarget( null );
  141. this.fsQuad.render( renderer );
  142. } else {
  143. renderer.setRenderTarget( writeBuffer );
  144. if ( this.clear ) renderer.clear();
  145. this.fsQuad.render( renderer );
  146. }
  147. },
  148. reset: function () {
  149. // render targets
  150. if ( this.luminanceRT ) {
  151. this.luminanceRT.dispose();
  152. }
  153. if ( this.currentLuminanceRT ) {
  154. this.currentLuminanceRT.dispose();
  155. }
  156. if ( this.previousLuminanceRT ) {
  157. this.previousLuminanceRT.dispose();
  158. }
  159. var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat }; // was RGB format. changed to RGBA format. see discussion in #8415 / #8450
  160. this.luminanceRT = new WebGLRenderTarget( this.resolution, this.resolution, pars );
  161. this.luminanceRT.texture.name = "AdaptiveToneMappingPass.l";
  162. this.luminanceRT.texture.generateMipmaps = false;
  163. this.previousLuminanceRT = new WebGLRenderTarget( this.resolution, this.resolution, pars );
  164. this.previousLuminanceRT.texture.name = "AdaptiveToneMappingPass.pl";
  165. this.previousLuminanceRT.texture.generateMipmaps = false;
  166. // We only need mipmapping for the current luminosity because we want a down-sampled version to sample in our adaptive shader
  167. pars.minFilter = LinearMipmapLinearFilter;
  168. pars.generateMipmaps = true;
  169. this.currentLuminanceRT = new WebGLRenderTarget( this.resolution, this.resolution, pars );
  170. this.currentLuminanceRT.texture.name = "AdaptiveToneMappingPass.cl";
  171. if ( this.adaptive ) {
  172. this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ] = "";
  173. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT.texture;
  174. }
  175. //Put something in the adaptive luminance texture so that the scene can render initially
  176. this.fsQuad.material = new MeshBasicMaterial( { color: 0x777777 } );
  177. this.materialLuminance.needsUpdate = true;
  178. this.materialAdaptiveLum.needsUpdate = true;
  179. this.materialToneMap.needsUpdate = true;
  180. // renderer.render( this.scene, this.camera, this.luminanceRT );
  181. // renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  182. // renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  183. },
  184. setAdaptive: function ( adaptive ) {
  185. if ( adaptive ) {
  186. this.adaptive = true;
  187. this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ] = "";
  188. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT.texture;
  189. } else {
  190. this.adaptive = false;
  191. delete this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ];
  192. this.materialToneMap.uniforms.luminanceMap.value = null;
  193. }
  194. this.materialToneMap.needsUpdate = true;
  195. },
  196. setAdaptionRate: function ( rate ) {
  197. if ( rate ) {
  198. this.materialAdaptiveLum.uniforms.tau.value = Math.abs( rate );
  199. }
  200. },
  201. setMinLuminance: function ( minLum ) {
  202. if ( minLum ) {
  203. this.materialToneMap.uniforms.minLuminance.value = minLum;
  204. this.materialAdaptiveLum.uniforms.minLuminance.value = minLum;
  205. }
  206. },
  207. setMaxLuminance: function ( maxLum ) {
  208. if ( maxLum ) {
  209. this.materialToneMap.uniforms.maxLuminance.value = maxLum;
  210. }
  211. },
  212. setAverageLuminance: function ( avgLum ) {
  213. if ( avgLum ) {
  214. this.materialToneMap.uniforms.averageLuminance.value = avgLum;
  215. }
  216. },
  217. setMiddleGrey: function ( middleGrey ) {
  218. if ( middleGrey ) {
  219. this.materialToneMap.uniforms.middleGrey.value = middleGrey;
  220. }
  221. },
  222. dispose: function () {
  223. if ( this.luminanceRT ) {
  224. this.luminanceRT.dispose();
  225. }
  226. if ( this.previousLuminanceRT ) {
  227. this.previousLuminanceRT.dispose();
  228. }
  229. if ( this.currentLuminanceRT ) {
  230. this.currentLuminanceRT.dispose();
  231. }
  232. if ( this.materialLuminance ) {
  233. this.materialLuminance.dispose();
  234. }
  235. if ( this.materialAdaptiveLum ) {
  236. this.materialAdaptiveLum.dispose();
  237. }
  238. if ( this.materialCopy ) {
  239. this.materialCopy.dispose();
  240. }
  241. if ( this.materialToneMap ) {
  242. this.materialToneMap.dispose();
  243. }
  244. }
  245. } );
  246. export { AdaptiveToneMappingPass };