AdaptiveToneMappingPass.js 9.3 KB

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