AdaptiveToneMappingPass.js 9.0 KB

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