ToneMappingNode.js 6.0 KB

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