AdaptiveToneMappingPass.js 9.5 KB

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