AdaptiveToneMappingPass.js 8.7 KB

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