AdaptiveToneMappingPass.js 9.3 KB

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