ToneMappingNode.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { tslFn, nodeObject, float, mat3, vec3 } from '../shadernode/ShaderNode.js';
  4. import { NoToneMapping, LinearToneMapping, ReinhardToneMapping, CineonToneMapping, ACESFilmicToneMapping, AgXToneMapping } from 'three';
  5. import { clamp, log2, max, pow } from '../math/MathNode.js';
  6. import { mul } from '../math/OperatorNode.js';
  7. // exposure only
  8. const LinearToneMappingNode = tslFn( ( { color, exposure } ) => {
  9. return color.mul( exposure ).clamp();
  10. } );
  11. // source: https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf
  12. const ReinhardToneMappingNode = tslFn( ( { color, exposure } ) => {
  13. color = color.mul( exposure );
  14. return color.div( color.add( 1.0 ) ).clamp();
  15. } );
  16. // source: http://filmicworlds.com/blog/filmic-tonemapping-operators/
  17. const OptimizedCineonToneMappingNode = tslFn( ( { color, exposure } ) => {
  18. // optimized filmic operator by Jim Hejl and Richard Burgess-Dawson
  19. color = color.mul( exposure );
  20. color = color.sub( 0.004 ).max( 0.0 );
  21. const a = color.mul( color.mul( 6.2 ).add( 0.5 ) );
  22. const b = color.mul( color.mul( 6.2 ).add( 1.7 ) ).add( 0.06 );
  23. return a.div( b ).pow( 2.2 );
  24. } );
  25. // source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
  26. const RRTAndODTFit = tslFn( ( { color } ) => {
  27. const a = color.mul( color.add( 0.0245786 ) ).sub( 0.000090537 );
  28. const b = color.mul( color.add( 0.4329510 ).mul( 0.983729 ) ).add( 0.238081 );
  29. return a.div( b );
  30. } );
  31. // source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
  32. const ACESFilmicToneMappingNode = tslFn( ( { color, exposure } ) => {
  33. // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
  34. const ACESInputMat = mat3(
  35. 0.59719, 0.35458, 0.04823,
  36. 0.07600, 0.90834, 0.01566,
  37. 0.02840, 0.13383, 0.83777
  38. );
  39. // ODT_SAT => XYZ => D60_2_D65 => sRGB
  40. const ACESOutputMat = mat3(
  41. 1.60475, - 0.53108, - 0.07367,
  42. - 0.10208, 1.10813, - 0.00605,
  43. - 0.00327, - 0.07276, 1.07602
  44. );
  45. color = color.mul( exposure ).div( 0.6 );
  46. color = ACESInputMat.mul( color );
  47. // Apply RRT and ODT
  48. color = RRTAndODTFit( { color } );
  49. color = ACESOutputMat.mul( color );
  50. // Clamp to [0, 1]
  51. return color.clamp();
  52. } );
  53. 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 ) );
  54. 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 ) );
  55. const agxDefaultContrastApprox = tslFn( ( [ x_immutable ] ) => {
  56. const x = vec3( x_immutable ).toVar();
  57. const x2 = vec3( x.mul( x ) ).toVar();
  58. const x4 = vec3( x2.mul( x2 ) ).toVar();
  59. 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 ) ) ) );
  60. } );
  61. const AGXToneMappingNode = tslFn( ( { color, exposure } ) => {
  62. const colortone = vec3( color ).toVar();
  63. const AgXInsetMatrix = mat3( vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ), vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ), vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 ) );
  64. const AgXOutsetMatrix = mat3( vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ), vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ), vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 ) );
  65. const AgxMinEv = float( - 12.47393 );
  66. const AgxMaxEv = float( 4.026069 );
  67. colortone.mulAssign( exposure );
  68. colortone.assign( LINEAR_SRGB_TO_LINEAR_REC2020.mul( colortone ) );
  69. colortone.assign( AgXInsetMatrix.mul( colortone ) );
  70. colortone.assign( max( colortone, 1e-10 ) );
  71. colortone.assign( log2( colortone ) );
  72. colortone.assign( colortone.sub( AgxMinEv ).div( AgxMaxEv.sub( AgxMinEv ) ) );
  73. colortone.assign( clamp( colortone, 0.0, 1.0 ) );
  74. colortone.assign( agxDefaultContrastApprox( colortone ) );
  75. colortone.assign( AgXOutsetMatrix.mul( colortone ) );
  76. colortone.assign( pow( max( vec3( 0.0 ), colortone ), vec3( 2.2 ) ) );
  77. colortone.assign( LINEAR_REC2020_TO_LINEAR_SRGB.mul( colortone ) );
  78. colortone.assign( clamp( colortone, 0.0, 1.0 ) );
  79. return colortone;
  80. } );
  81. const toneMappingLib = {
  82. [ LinearToneMapping ]: LinearToneMappingNode,
  83. [ ReinhardToneMapping ]: ReinhardToneMappingNode,
  84. [ CineonToneMapping ]: OptimizedCineonToneMappingNode,
  85. [ ACESFilmicToneMapping ]: ACESFilmicToneMappingNode,
  86. [ AgXToneMapping ]: AGXToneMappingNode
  87. };
  88. class ToneMappingNode extends TempNode {
  89. constructor( toneMapping = NoToneMapping, exposureNode = float( 1 ), colorNode = null ) {
  90. super( 'vec3' );
  91. this.toneMapping = toneMapping;
  92. this.exposureNode = exposureNode;
  93. this.colorNode = colorNode;
  94. }
  95. getCacheKey() {
  96. let cacheKey = super.getCacheKey();
  97. cacheKey = '{toneMapping:' + this.toneMapping + ',nodes:' + cacheKey + '}';
  98. return cacheKey;
  99. }
  100. setup( builder ) {
  101. const colorNode = this.colorNode || builder.context.color;
  102. const toneMapping = this.toneMapping;
  103. if ( toneMapping === NoToneMapping ) return colorNode;
  104. const toneMappingParams = { exposure: this.exposureNode, color: colorNode };
  105. const toneMappingNode = toneMappingLib[ toneMapping ];
  106. let outputNode = null;
  107. if ( toneMappingNode ) {
  108. outputNode = toneMappingNode( toneMappingParams );
  109. } else {
  110. console.error( 'ToneMappingNode: Unsupported Tone Mapping configuration.', toneMapping );
  111. outputNode = colorNode;
  112. }
  113. return outputNode;
  114. }
  115. }
  116. export default ToneMappingNode;
  117. export const toneMapping = ( mapping, exposure, color ) => nodeObject( new ToneMappingNode( mapping, nodeObject( exposure ), nodeObject( color ) ) );
  118. addNodeClass( 'ToneMappingNode', ToneMappingNode );