ToneMappingNode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { addNodeElement, tslFn, nodeObject, float, mat3, vec3, If } from '../shadernode/ShaderNode.js';
  4. import { rendererReference } from '../accessors/RendererReferenceNode.js';
  5. import { cond } from '../math/CondNode.js';
  6. import { clamp, log2, max, min, pow, mix } from '../math/MathNode.js';
  7. import { mul, sub, div } from '../math/OperatorNode.js';
  8. import { NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping, NeutralToneMapping } from 'three';
  9. // exposure only
  10. const LinearToneMappingNode = tslFn( ( { color, exposure } ) => {
  11. return color.mul( exposure ).clamp();
  12. } );
  13. // source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf
  14. const ReinhardToneMappingNode = tslFn( ( { color, exposure } ) => {
  15. color = color.mul( exposure );
  16. return color.div( color.add( 1.0 ) ).clamp();
  17. } );
  18. // source: http://filmicworlds.com/blog/filmic-tonemapping-operators/
  19. const OptimizedCineonToneMappingNode = tslFn( ( { color, exposure } ) => {
  20. // optimized filmic operator by Jim Hejl and Richard Burgess-Dawson
  21. color = color.mul( exposure );
  22. color = color.sub( 0.004 ).max( 0.0 );
  23. const a = color.mul( color.mul( 6.2 ).add( 0.5 ) );
  24. const b = color.mul( color.mul( 6.2 ).add( 1.7 ) ).add( 0.06 );
  25. return a.div( b ).pow( 2.2 );
  26. } );
  27. // source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
  28. const RRTAndODTFit = tslFn( ( { color } ) => {
  29. const a = color.mul( color.add( 0.0245786 ) ).sub( 0.000090537 );
  30. const b = color.mul( color.add( 0.4329510 ).mul( 0.983729 ) ).add( 0.238081 );
  31. return a.div( b );
  32. } );
  33. // source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
  34. const ACESFilmicToneMappingNode = tslFn( ( { color, exposure } ) => {
  35. // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
  36. const ACESInputMat = mat3(
  37. 0.59719, 0.35458, 0.04823,
  38. 0.07600, 0.90834, 0.01566,
  39. 0.02840, 0.13383, 0.83777
  40. );
  41. // ODT_SAT => XYZ => D60_2_D65 => sRGB
  42. const ACESOutputMat = mat3(
  43. 1.60475, - 0.53108, - 0.07367,
  44. - 0.10208, 1.10813, - 0.00605,
  45. - 0.00327, - 0.07276, 1.07602
  46. );
  47. color = color.mul( exposure ).div( 0.6 );
  48. color = ACESInputMat.mul( color );
  49. // Apply RRT and ODT
  50. color = RRTAndODTFit( { color } );
  51. color = ACESOutputMat.mul( color );
  52. // Clamp to [0, 1]
  53. return color.clamp();
  54. } );
  55. const LINEAR_REC2020_TO_LINEAR_SRGB = mat3( vec3( 1.6605, - 0.1246, - 0.0182 ), vec3( - 0.5876, 1.1329, - 0.1006 ), vec3( - 0.0728, - 0.0083, 1.1187 ) );
  56. const LINEAR_SRGB_TO_LINEAR_REC2020 = mat3( vec3( 0.6274, 0.0691, 0.0164 ), vec3( 0.3293, 0.9195, 0.0880 ), vec3( 0.0433, 0.0113, 0.8956 ) );
  57. const agxDefaultContrastApprox = tslFn( ( [ x_immutable ] ) => {
  58. const x = vec3( x_immutable ).toVar();
  59. const x2 = vec3( x.mul( x ) ).toVar();
  60. const x4 = vec3( x2.mul( x2 ) ).toVar();
  61. return float( 15.5 ).mul( x4.mul( x2 ) ).sub( mul( 40.14, x4.mul( x ) ) ).add( mul( 31.96, x4 ).sub( mul( 6.868, x2.mul( x ) ) ).add( mul( 0.4298, x2 ).add( mul( 0.1191, x ).sub( 0.00232 ) ) ) );
  62. } );
  63. const AGXToneMappingNode = tslFn( ( { color, exposure } ) => {
  64. const colortone = vec3( color ).toVar();
  65. const AgXInsetMatrix = mat3( vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ), vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ), vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 ) );
  66. const AgXOutsetMatrix = mat3( vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ), vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ), vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 ) );
  67. const AgxMinEv = float( - 12.47393 );
  68. const AgxMaxEv = float( 4.026069 );
  69. colortone.mulAssign( exposure );
  70. colortone.assign( LINEAR_SRGB_TO_LINEAR_REC2020.mul( colortone ) );
  71. colortone.assign( AgXInsetMatrix.mul( colortone ) );
  72. colortone.assign( max( colortone, 1e-10 ) );
  73. colortone.assign( log2( colortone ) );
  74. colortone.assign( colortone.sub( AgxMinEv ).div( AgxMaxEv.sub( AgxMinEv ) ) );
  75. colortone.assign( clamp( colortone, 0.0, 1.0 ) );
  76. colortone.assign( agxDefaultContrastApprox( colortone ) );
  77. colortone.assign( AgXOutsetMatrix.mul( colortone ) );
  78. colortone.assign( pow( max( vec3( 0.0 ), colortone ), vec3( 2.2 ) ) );
  79. colortone.assign( LINEAR_REC2020_TO_LINEAR_SRGB.mul( colortone ) );
  80. colortone.assign( clamp( colortone, 0.0, 1.0 ) );
  81. return colortone;
  82. } );
  83. // https://modelviewer.dev/examples/tone-mapping
  84. const NeutralToneMappingNode = tslFn( ( { color, exposure } ) => {
  85. const StartCompression = float( 0.8 - 0.04 );
  86. const Desaturation = float( 0.15 );
  87. color = color.mul( exposure );
  88. const x = min( color.r, min( color.g, color.b ) );
  89. const offset = cond( x.lessThan( 0.08 ), x.sub( mul( 6.25, x.mul( x ) ) ), 0.04 );
  90. color.subAssign( offset );
  91. const peak = max( color.r, max( color.g, color.b ) );
  92. If( peak.lessThan( StartCompression ), () => {
  93. return color;
  94. } );
  95. const d = sub( 1, StartCompression );
  96. const newPeak = sub( 1, d.mul( d ).div( peak.add( d.sub( StartCompression ) ) ) );
  97. color.mulAssign( newPeak.div( peak ) );
  98. const g = sub( 1, div( 1, Desaturation.mul( peak.sub( newPeak ) ).add( 1 ) ) );
  99. return mix( color, vec3( newPeak ), g );
  100. } ).setLayout( {
  101. name: 'NeutralToneMapping',
  102. type: 'vec3',
  103. inputs: [
  104. { name: 'color', type: 'vec3' },
  105. { name: 'exposure', type: 'float' }
  106. ]
  107. } );
  108. const toneMappingLib = {
  109. [ LinearToneMapping ]: LinearToneMappingNode,
  110. [ ReinhardToneMapping ]: ReinhardToneMappingNode,
  111. [ CineonToneMapping ]: OptimizedCineonToneMappingNode,
  112. [ ACESFilmicToneMapping ]: ACESFilmicToneMappingNode,
  113. [ AgXToneMapping ]: AGXToneMappingNode,
  114. [ NeutralToneMapping ]: NeutralToneMappingNode
  115. };
  116. class ToneMappingNode extends TempNode {
  117. constructor( toneMapping = NoToneMapping, exposureNode = toneMappingExposure, colorNode = null ) {
  118. super( 'vec3' );
  119. this.toneMapping = toneMapping;
  120. this.exposureNode = exposureNode;
  121. this.colorNode = colorNode;
  122. }
  123. getCacheKey() {
  124. let cacheKey = super.getCacheKey();
  125. cacheKey = '{toneMapping:' + this.toneMapping + ',nodes:' + cacheKey + '}';
  126. return cacheKey;
  127. }
  128. setup( builder ) {
  129. const colorNode = this.colorNode || builder.context.color;
  130. const toneMapping = this.toneMapping;
  131. if ( toneMapping === NoToneMapping ) return colorNode;
  132. const toneMappingParams = { exposure: this.exposureNode, color: colorNode };
  133. const toneMappingNode = toneMappingLib[ toneMapping ];
  134. let outputNode = null;
  135. if ( toneMappingNode ) {
  136. outputNode = toneMappingNode( toneMappingParams );
  137. } else {
  138. console.error( 'ToneMappingNode: Unsupported Tone Mapping configuration.', toneMapping );
  139. outputNode = colorNode;
  140. }
  141. return outputNode;
  142. }
  143. }
  144. export default ToneMappingNode;
  145. export const toneMapping = ( mapping, exposure, color ) => nodeObject( new ToneMappingNode( mapping, nodeObject( exposure ), nodeObject( color ) ) );
  146. export const toneMappingExposure = rendererReference( 'toneMappingExposure', 'float' );
  147. addNodeElement( 'toneMapping', ( color, mapping, exposure ) => toneMapping( mapping, exposure, color ) );
  148. addNodeClass( 'ToneMappingNode', ToneMappingNode );