AdaptiveToneMappingPass.js 9.3 KB

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