AdaptiveToneMappingPass.js 9.5 KB

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