AdaptiveToneMappingPass.js 8.8 KB

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