AdaptiveToneMappingPass.js 9.2 KB

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