AdaptiveToneMappingPass.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  92. this.scene = new THREE.Scene();
  93. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  94. this.quad.frustumCulled = false; // Avoid getting clipped
  95. this.scene.add( this.quad );
  96. };
  97. THREE.AdaptiveToneMappingPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  98. constructor: THREE.AdaptiveToneMappingPass,
  99. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  100. if ( this.needsInit ) {
  101. this.reset( renderer );
  102. this.luminanceRT.texture.type = readBuffer.texture.type;
  103. this.previousLuminanceRT.texture.type = readBuffer.texture.type;
  104. this.currentLuminanceRT.texture.type = readBuffer.texture.type;
  105. this.needsInit = false;
  106. }
  107. if ( this.adaptive ) {
  108. //Render the luminance of the current scene into a render target with mipmapping enabled
  109. this.quad.material = this.materialLuminance;
  110. this.materialLuminance.uniforms.tDiffuse.value = readBuffer.texture;
  111. renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  112. //Use the new luminance values, the previous luminance and the frame delta to
  113. //adapt the luminance over time.
  114. this.quad.material = this.materialAdaptiveLum;
  115. this.materialAdaptiveLum.uniforms.delta.value = delta;
  116. this.materialAdaptiveLum.uniforms.lastLum.value = this.previousLuminanceRT.texture;
  117. this.materialAdaptiveLum.uniforms.currentLum.value = this.currentLuminanceRT.texture;
  118. renderer.render( this.scene, this.camera, this.luminanceRT );
  119. //Copy the new adapted luminance value so that it can be used by the next frame.
  120. this.quad.material = this.materialCopy;
  121. this.copyUniforms.tDiffuse.value = this.luminanceRT.texture;
  122. renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  123. }
  124. this.quad.material = this.materialToneMap;
  125. this.materialToneMap.uniforms.tDiffuse.value = readBuffer.texture;
  126. if ( this.renderToScreen ) {
  127. renderer.render( this.scene, this.camera );
  128. } else {
  129. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  130. }
  131. },
  132. reset: function( renderer ) {
  133. // render targets
  134. if ( this.luminanceRT ) {
  135. this.luminanceRT.dispose();
  136. }
  137. if ( this.currentLuminanceRT ) {
  138. this.currentLuminanceRT.dispose();
  139. }
  140. if ( this.previousLuminanceRT ) {
  141. this.previousLuminanceRT.dispose();
  142. }
  143. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat }; // was RGB format. changed to RGBA format. see discussion in #8415 / #8450
  144. this.luminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  145. this.luminanceRT.texture.name = "AdaptiveToneMappingPass.l";
  146. this.luminanceRT.texture.generateMipmaps = false;
  147. this.previousLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  148. this.previousLuminanceRT.texture.name = "AdaptiveToneMappingPass.pl";
  149. this.previousLuminanceRT.texture.generateMipmaps = false;
  150. // We only need mipmapping for the current luminosity because we want a down-sampled version to sample in our adaptive shader
  151. pars.minFilter = THREE.LinearMipMapLinearFilter;
  152. pars.generateMipmaps = true;
  153. this.currentLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  154. this.currentLuminanceRT.texture.name = "AdaptiveToneMappingPass.cl";
  155. if ( this.adaptive ) {
  156. this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ] = "";
  157. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT.texture;
  158. }
  159. //Put something in the adaptive luminance texture so that the scene can render initially
  160. this.quad.material = new THREE.MeshBasicMaterial( { color: 0x777777 } );
  161. this.materialLuminance.needsUpdate = true;
  162. this.materialAdaptiveLum.needsUpdate = true;
  163. this.materialToneMap.needsUpdate = true;
  164. // renderer.render( this.scene, this.camera, this.luminanceRT );
  165. // renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  166. // renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  167. },
  168. setAdaptive: function( adaptive ) {
  169. if ( adaptive ) {
  170. this.adaptive = true;
  171. this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ] = "";
  172. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT.texture;
  173. } else {
  174. this.adaptive = false;
  175. delete this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ];
  176. this.materialToneMap.uniforms.luminanceMap.value = null;
  177. }
  178. this.materialToneMap.needsUpdate = true;
  179. },
  180. setAdaptionRate: function( rate ) {
  181. if ( rate ) {
  182. this.materialAdaptiveLum.uniforms.tau.value = Math.abs( rate );
  183. }
  184. },
  185. setMinLuminance: function( minLum ) {
  186. if ( minLum ) {
  187. this.materialToneMap.uniforms.minLuminance.value = minLum;
  188. this.materialAdaptiveLum.uniforms.minLuminance.value = minLum;
  189. }
  190. },
  191. setMaxLuminance: function( maxLum ) {
  192. if ( maxLum ) {
  193. this.materialToneMap.uniforms.maxLuminance.value = maxLum;
  194. }
  195. },
  196. setAverageLuminance: function( avgLum ) {
  197. if ( avgLum ) {
  198. this.materialToneMap.uniforms.averageLuminance.value = avgLum;
  199. }
  200. },
  201. setMiddleGrey: function( middleGrey ) {
  202. if ( middleGrey ) {
  203. this.materialToneMap.uniforms.middleGrey.value = middleGrey;
  204. }
  205. },
  206. dispose: function() {
  207. if ( this.luminanceRT ) {
  208. this.luminanceRT.dispose();
  209. }
  210. if ( this.previousLuminanceRT ) {
  211. this.previousLuminanceRT.dispose();
  212. }
  213. if ( this.currentLuminanceRT ) {
  214. this.currentLuminanceRT.dispose();
  215. }
  216. if ( this.materialLuminance ) {
  217. this.materialLuminance.dispose();
  218. }
  219. if ( this.materialAdaptiveLum ) {
  220. this.materialAdaptiveLum.dispose();
  221. }
  222. if ( this.materialCopy ) {
  223. this.materialCopy.dispose();
  224. }
  225. if ( this.materialToneMap ) {
  226. this.materialToneMap.dispose();
  227. }
  228. }
  229. } );