2
0

AdaptiveToneMappingPass.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "float fLastLum = lastLum.r;",
  62. "float fCurrentLum = currentLum.r;",
  63. //The adaption seems to work better in extreme lighting differences
  64. //if the input luminance is squared.
  65. "fCurrentLum *= fCurrentLum;",
  66. // Adapt the luminance using Pattanaik's technique
  67. "float fAdaptedLum = fLastLum + (fCurrentLum - fLastLum) * (1.0 - exp(-delta * tau));",
  68. // "fAdaptedLum = sqrt(fAdaptedLum);",
  69. "gl_FragColor = vec4( vec3( fAdaptedLum ), 1.0 );",
  70. "}",
  71. ].join('\n')
  72. };
  73. this.materialAdaptiveLum = new THREE.ShaderMaterial( {
  74. uniforms: this.adaptLuminanceShader.uniforms,
  75. vertexShader: this.adaptLuminanceShader.vertexShader,
  76. fragmentShader: this.adaptLuminanceShader.fragmentShader,
  77. defines: this.adaptLuminanceShader.defines,
  78. blending: THREE.NoBlending
  79. } );
  80. if ( THREE.ToneMapShader === undefined )
  81. console.error( "THREE.AdaptiveToneMappingPass relies on THREE.ToneMapShader" );
  82. this.materialToneMap = new THREE.ShaderMaterial( {
  83. uniforms: THREE.ToneMapShader.uniforms,
  84. vertexShader: THREE.ToneMapShader.vertexShader,
  85. fragmentShader: THREE.ToneMapShader.fragmentShader,
  86. blending: THREE.NoBlending
  87. } );
  88. this.enabled = true;
  89. this.needsSwap = true;
  90. this.clear = false;
  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.PlaneGeometry( 2, 2 ), null );
  94. this.scene.add( this.quad );
  95. };
  96. THREE.AdaptiveToneMappingPass.prototype = {
  97. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  98. if ( this.needsInit ) {
  99. this.reset( renderer );
  100. this.luminanceRT.type = readBuffer.type;
  101. this.previousLuminanceRT.type = readBuffer.type;
  102. this.currentLuminanceRT.type = readBuffer.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;
  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;
  115. this.materialAdaptiveLum.uniforms.currentLum.value = this.currentLuminanceRT;
  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;
  120. renderer.render( this.scene, this.camera, this.previousLuminanceRT );
  121. }
  122. this.quad.material = this.materialToneMap;
  123. this.materialToneMap.uniforms.tDiffuse.value = readBuffer;
  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.RGBFormat };
  138. this.luminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  139. this.luminanceRT.generateMipmaps = false;
  140. this.previousLuminanceRT = new THREE.WebGLRenderTarget( this.resolution, this.resolution, pars );
  141. this.previousLuminanceRT.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;
  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;
  163. }
  164. else {
  165. this.adaptive = false;
  166. delete this.materialToneMap.defines["ADAPTED_LUMINANCE"];
  167. this.materialToneMap.uniforms.luminanceMap.value = undefined;
  168. }
  169. this.materialToneMap.needsUpdate = true;
  170. },
  171. setAdaptionRate: function( rate ) {
  172. if ( rate ) {
  173. this.materialAdaptiveLum.uniforms.tau.value = Math.abs( rate );
  174. }
  175. },
  176. setMaxLuminance: function( maxLum ) {
  177. if ( maxLum ) {
  178. this.materialToneMap.uniforms.maxLuminance.value = maxLum;
  179. }
  180. },
  181. setAverageLuminance: function( avgLum ) {
  182. if ( avgLum ) {
  183. this.materialToneMap.uniforms.averageLuminance.value = avgLum;
  184. }
  185. },
  186. setMiddleGrey: function( middleGrey ) {
  187. if ( middleGrey ) {
  188. this.materialToneMap.uniforms.middleGrey.value = middleGrey;
  189. }
  190. },
  191. dispose: function() {
  192. if ( this.luminanceRT ) {
  193. this.luminanceRT.dispose();
  194. }
  195. if ( this.previousLuminanceRT ) {
  196. this.previousLuminanceRT.dispose();
  197. }
  198. if ( this.currentLuminanceRT ) {
  199. this.currentLuminanceRT.dispose();
  200. }
  201. if ( this.materialLuminance ) {
  202. this.materialLuminance.dispose();
  203. }
  204. if ( this.materialAdaptiveLum ) {
  205. this.materialAdaptiveLum.dispose();
  206. }
  207. if ( this.materialCopy ) {
  208. this.materialCopy.dispose();
  209. }
  210. if ( this.materialToneMap ) {
  211. this.materialToneMap.dispose();
  212. }
  213. }
  214. };