AdaptiveToneMappingPass.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. "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 delta;",
  58. "uniform float tau;",
  59. "void main() {",
  60. "vec4 lastLum = texture2D( lastLum, vUv, MIP_LEVEL_1X1 );",
  61. "vec4 currentLum = texture2D( currentLum, vUv, MIP_LEVEL_1X1 );",
  62. "float fLastLum = lastLum.r;",
  63. "float fCurrentLum = currentLum.r;",
  64. //The adaption seems to work better in extreme lighting differences
  65. //if the input luminance is squared.
  66. "fCurrentLum *= fCurrentLum;",
  67. // Adapt the luminance using Pattanaik's technique
  68. "float fAdaptedLum = fLastLum + (fCurrentLum - fLastLum) * (1.0 - exp(-delta * tau));",
  69. // "fAdaptedLum = sqrt(fAdaptedLum);",
  70. "gl_FragColor = vec4( vec3( fAdaptedLum ), 1.0 );",
  71. "}"
  72. ].join( '\n' )
  73. };
  74. this.materialAdaptiveLum = new THREE.ShaderMaterial( {
  75. uniforms: THREE.UniformsUtils.clone( this.adaptLuminanceShader.uniforms ),
  76. vertexShader: this.adaptLuminanceShader.vertexShader,
  77. fragmentShader: this.adaptLuminanceShader.fragmentShader,
  78. defines: this.adaptLuminanceShader.defines,
  79. blending: THREE.NoBlending
  80. } );
  81. if ( THREE.ToneMapShader === undefined )
  82. console.error( "THREE.AdaptiveToneMappingPass relies on THREE.ToneMapShader" );
  83. this.materialToneMap = new THREE.ShaderMaterial( {
  84. uniforms: THREE.UniformsUtils.clone( THREE.ToneMapShader.uniforms ),
  85. vertexShader: THREE.ToneMapShader.vertexShader,
  86. fragmentShader: THREE.ToneMapShader.fragmentShader,
  87. blending: THREE.NoBlending
  88. } );
  89. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  90. this.scene = new THREE.Scene();
  91. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  92. this.quad.frustumCulled = false; // Avoid getting clipped
  93. this.scene.add( this.quad );
  94. };
  95. THREE.AdaptiveToneMappingPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  96. constructor: THREE.AdaptiveToneMappingPass,
  97. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  98. if ( this.needsInit ) {
  99. this.reset( renderer );
  100. this.luminanceRT.texture.type = readBuffer.texture.type;
  101. this.previousLuminanceRT.texture.type = readBuffer.texture.type;
  102. this.currentLuminanceRT.texture.type = readBuffer.texture.type;
  103. this.needsInit = false;
  104. }
  105. if ( this.adaptive ) {
  106. //Render the luminance of the current scene into a render target with mipmapping enabled
  107. this.quad.material = this.materialLuminance;
  108. this.materialLuminance.uniforms.tDiffuse.value = readBuffer.texture;
  109. renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  110. //Use the new luminance values, the previous luminance and the frame delta to
  111. //adapt the luminance over time.
  112. this.quad.material = this.materialAdaptiveLum;
  113. this.materialAdaptiveLum.uniforms.delta.value = delta;
  114. this.materialAdaptiveLum.uniforms.lastLum.value = this.previousLuminanceRT.texture;
  115. this.materialAdaptiveLum.uniforms.currentLum.value = this.currentLuminanceRT.texture;
  116. renderer.render( this.scene, this.camera, this.luminanceRT );
  117. //Copy the new adapted luminance value so that it can be used by the next frame.
  118. this.quad.material = this.materialCopy;
  119. this.copyUniforms.tDiffuse.value = this.luminanceRT.texture;
  120. renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  121. }
  122. this.quad.material = this.materialToneMap;
  123. this.materialToneMap.uniforms.tDiffuse.value = readBuffer.texture;
  124. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  125. },
  126. reset: function( renderer ) {
  127. // render targets
  128. if ( this.luminanceRT ) {
  129. this.luminanceRT.dispose();
  130. }
  131. if ( this.currentLuminanceRT ) {
  132. this.currentLuminanceRT.dispose();
  133. }
  134. if ( this.previousLuminanceRT ) {
  135. this.previousLuminanceRT.dispose();
  136. }
  137. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat }; // was RGB format. changed to RGBA format. see discussion in #8415 / #8450
  138. this.luminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  139. this.luminanceRT.texture.generateMipmaps = false;
  140. this.previousLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  141. this.previousLuminanceRT.texture.generateMipmaps = false;
  142. // We only need mipmapping for the current luminosity because we want a down-sampled version to sample in our adaptive shader
  143. pars.minFilter = THREE.LinearMipMapLinearFilter;
  144. this.currentLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  145. if ( this.adaptive ) {
  146. this.materialToneMap.defines[ "ADAPTED_LUMINANCE" ] = "";
  147. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT.texture;
  148. }
  149. //Put something in the adaptive luminance texture so that the scene can render initially
  150. this.quad.material = new THREE.MeshBasicMaterial( { color: 0x777777 } );
  151. this.materialLuminance.needsUpdate = true;
  152. this.materialAdaptiveLum.needsUpdate = true;
  153. this.materialToneMap.needsUpdate = true;
  154. // 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. setMaxLuminance: function( maxLum ) {
  176. if ( maxLum ) {
  177. this.materialToneMap.uniforms.maxLuminance.value = maxLum;
  178. }
  179. },
  180. setAverageLuminance: function( avgLum ) {
  181. if ( avgLum ) {
  182. this.materialToneMap.uniforms.averageLuminance.value = avgLum;
  183. }
  184. },
  185. setMiddleGrey: function( middleGrey ) {
  186. if ( middleGrey ) {
  187. this.materialToneMap.uniforms.middleGrey.value = middleGrey;
  188. }
  189. },
  190. dispose: function() {
  191. if ( this.luminanceRT ) {
  192. this.luminanceRT.dispose();
  193. }
  194. if ( this.previousLuminanceRT ) {
  195. this.previousLuminanceRT.dispose();
  196. }
  197. if ( this.currentLuminanceRT ) {
  198. this.currentLuminanceRT.dispose();
  199. }
  200. if ( this.materialLuminance ) {
  201. this.materialLuminance.dispose();
  202. }
  203. if ( this.materialAdaptiveLum ) {
  204. this.materialAdaptiveLum.dispose();
  205. }
  206. if ( this.materialCopy ) {
  207. this.materialCopy.dispose();
  208. }
  209. if ( this.materialToneMap ) {
  210. this.materialToneMap.dispose();
  211. }
  212. }
  213. } );