AdaptiveToneMappingPass.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. 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.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.log2( this.resolution ).toFixed(1),
  38. },
  39. uniforms: {
  40. "lastLum": { type: "t", value: null },
  41. "currentLum": { type: "t", value: null },
  42. "delta": { type: 'f', value: 0.016 },
  43. "tau": { type: 'f', value: 1.0 }
  44. },
  45. vertexShader: [
  46. "varying vec2 vUv;",
  47. "void main() {",
  48. "vUv = uv;",
  49. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  50. "}"
  51. ].join('\n'),
  52. fragmentShader: [
  53. "varying vec2 vUv;",
  54. "uniform sampler2D lastLum;",
  55. "uniform sampler2D currentLum;",
  56. "uniform float delta;",
  57. "uniform float tau;",
  58. "void main() {",
  59. "vec4 lastLum = texture2D( lastLum, vUv, MIP_LEVEL_1X1 );",
  60. "vec4 currentLum = texture2D( currentLum, vUv, MIP_LEVEL_1X1 );",
  61. "#ifdef HDR_INPUT_LOGLUV",
  62. "float fLastLum = HDRDecodeLOGLUV( lastLum ).r;",
  63. "float fCurrentLum = HDRDecodeLOGLUV( currentLum ).r;",
  64. "#elif defined( HDR_INPUT_RGBM )",
  65. "float fLastLum = HDRDecodeRGBM( lastLum ).r;",
  66. "float fCurrentLum = HDRDecodeRGBM( currentLum ).r;",
  67. "#else",
  68. "float fLastLum = lastLum.r;",
  69. "float fCurrentLum = currentLum.r;",
  70. "#endif",
  71. //The adaption seems to work better in extreme lighting differences
  72. //if the input luminance is squared.
  73. "fCurrentLum *= fCurrentLum;",
  74. // Adapt the luminance using Pattanaik's technique
  75. "float fAdaptedLum = fLastLum + (fCurrentLum - fLastLum) * (1.0 - exp(-delta * tau));",
  76. // "fAdaptedLum = sqrt(fAdaptedLum);",
  77. "gl_FragColor = vec4( vec3( fAdaptedLum ), 1.0 );",
  78. "}",
  79. ].join('\n')
  80. };
  81. this.materialAdaptiveLum = new THREE.ShaderMaterial( {
  82. uniforms: this.adaptLuminanceShader.uniforms,
  83. vertexShader: this.adaptLuminanceShader.vertexShader,
  84. fragmentShader: this.adaptLuminanceShader.fragmentShader,
  85. defines: this.adaptLuminanceShader.defines,
  86. blending: THREE.NoBlending
  87. } );
  88. if ( THREE.ToneMapShader === undefined )
  89. console.error( "THREE.AdaptiveToneMappingPass relies on THREE.ToneMapShader" );
  90. this.materialToneMap = new THREE.ShaderMaterial( {
  91. uniforms: THREE.ToneMapShader.uniforms,
  92. vertexShader: THREE.ToneMapShader.vertexShader,
  93. fragmentShader: THREE.ToneMapShader.fragmentShader,
  94. blending: THREE.NoBlending
  95. } );
  96. this.enabled = true;
  97. this.needsSwap = true;
  98. this.clear = false;
  99. this.camera = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  100. this.scene = new THREE.Scene();
  101. this.quad = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), null );
  102. this.scene.add( this.quad );
  103. };
  104. THREE.AdaptiveToneMappingPass.prototype = {
  105. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  106. if ( this.needsInit ) {
  107. this.reset( renderer );
  108. this.luminanceRT.type = readBuffer.type;
  109. this.previousLuminanceRT.type = readBuffer.type;
  110. this.currentLuminanceRT.type = readBuffer.type;
  111. this.needsInit = false;
  112. }
  113. if ( this.adaptive ) {
  114. //Render the luminance of the current scene into a render target with mipmapping enabled
  115. this.quad.material = this.materialLuminance;
  116. this.materialLuminance.uniforms.tDiffuse.value = readBuffer;
  117. renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  118. //Use the new luminance values, the previous luminance and the frame delta to
  119. //adapt the luminance over time.
  120. this.quad.material = this.materialAdaptiveLum;
  121. this.materialAdaptiveLum.uniforms.delta.value = delta;
  122. this.materialAdaptiveLum.uniforms.lastLum.value = this.previousLuminanceRT;
  123. this.materialAdaptiveLum.uniforms.currentLum.value = this.currentLuminanceRT;
  124. renderer.render( this.scene, this.camera, this.luminanceRT );
  125. //Copy the new adapted luminance value so that it can be used by the next frame.
  126. this.quad.material = this.materialCopy;
  127. this.copyUniforms.tDiffuse.value = this.luminanceRT;
  128. renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  129. }
  130. this.quad.material = this.materialToneMap;
  131. this.materialToneMap.uniforms.tDiffuse.value = readBuffer;
  132. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  133. },
  134. reset: function( renderer ) {
  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.RGBFormat };
  146. this.luminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  147. this.luminanceRT.generateMipmaps = false;
  148. this.previousLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  149. this.previousLuminanceRT.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. this.currentLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  153. if ( this.adaptive ) {
  154. this.materialToneMap.defines["ADAPTED_LUMINANCE"] = "";
  155. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT;
  156. }
  157. //Put something in the adaptive luminance texture so that the scene can render initially
  158. this.quad.material = new THREE.MeshBasicMaterial( {color: 0x777777 });
  159. this.materialLuminance.needsUpdate = true;
  160. this.materialAdaptiveLum.needsUpdate = true;
  161. this.materialToneMap.needsUpdate = true;
  162. // renderer.render( this.scene, this.camera, this.luminanceRT );
  163. // renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  164. // renderer.render( this.scene, this.camera, this.currentLuminanceRT );
  165. },
  166. setAdaptive: function( adaptive ) {
  167. if ( adaptive ) {
  168. this.adaptive = true;
  169. this.materialToneMap.defines["ADAPTED_LUMINANCE"] = "";
  170. this.materialToneMap.uniforms.luminanceMap.value = this.luminanceRT;
  171. }
  172. else {
  173. this.adaptive = false;
  174. delete this.materialToneMap.defines["ADAPTED_LUMINANCE"];
  175. this.materialToneMap.uniforms.luminanceMap.value = undefined;
  176. }
  177. this.materialToneMap.needsUpdate = true;
  178. },
  179. setAdaptionRate: function( rate ) {
  180. if ( rate ) {
  181. this.materialAdaptiveLum.uniforms.tau.value = Math.abs( rate );
  182. }
  183. },
  184. setMaxLuminance: function( maxLum ) {
  185. if ( maxLum ) {
  186. this.materialToneMap.uniforms.maxLuminance.value = maxLum;
  187. }
  188. },
  189. setAverageLuminance: function( avgLum ) {
  190. if ( avgLum ) {
  191. this.materialToneMap.uniforms.averageLuminance.value = avgLum;
  192. }
  193. },
  194. setMiddleGrey: function( middleGrey ) {
  195. if ( middleGrey ) {
  196. this.materialToneMap.uniforms.middleGrey.value = middleGrey;
  197. }
  198. },
  199. dispose: function() {
  200. if ( this.luminanceRT ) {
  201. this.luminanceRT.dispose();
  202. }
  203. if ( this.previousLuminanceRT ) {
  204. this.previousLuminanceRT.dispose();
  205. }
  206. if ( this.currentLuminanceRT ) {
  207. this.currentLuminanceRT.dispose();
  208. }
  209. if ( this.materialLuminance ) {
  210. this.materialLuminance.dispose();
  211. }
  212. if ( this.materialAdaptiveLum ) {
  213. this.materialAdaptiveLum.dispose();
  214. }
  215. if ( this.materialCopy ) {
  216. this.materialCopy.dispose();
  217. }
  218. if ( this.materialToneMap ) {
  219. this.materialToneMap.dispose();
  220. }
  221. }
  222. };