ToneMappingNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import TempNode from '../core/TempNode.js';
  2. import { addMethodChaining, nodeObject, vec4 } from '../tsl/TSLCore.js';
  3. import { rendererReference } from '../accessors/RendererReferenceNode.js';
  4. import { NoToneMapping } from '../../constants.js';
  5. import { hash } from '../core/NodeUtils.js';
  6. /**
  7. * This node represents a tone mapping operation.
  8. *
  9. * @augments TempNode
  10. */
  11. class ToneMappingNode extends TempNode {
  12. static get type() {
  13. return 'ToneMappingNode';
  14. }
  15. /**
  16. * Constructs a new tone mapping node.
  17. *
  18. * @param {Number} toneMapping - The tone mapping type.
  19. * @param {Node} exposureNode - The tone mapping exposure.
  20. * @param {Node} [colorNode=null] - The color node to process.
  21. */
  22. constructor( toneMapping, exposureNode = toneMappingExposure, colorNode = null ) {
  23. super( 'vec3' );
  24. /**
  25. * The tone mapping type.
  26. *
  27. * @type {Number}
  28. */
  29. this.toneMapping = toneMapping;
  30. /**
  31. * The tone mapping exposure.
  32. *
  33. * @type {Node}
  34. * @default null
  35. */
  36. this.exposureNode = exposureNode;
  37. /**
  38. * Represents the color to process.
  39. *
  40. * @type {Node}
  41. * @default null
  42. */
  43. this.colorNode = colorNode;
  44. }
  45. /**
  46. * Overwrites the default `getCacheKey()` implementation by including the tone
  47. * mapping type into the cache key.
  48. *
  49. * @return {Number} The hash.
  50. */
  51. getCacheKey() {
  52. return hash( super.getCacheKey(), this.toneMapping );
  53. }
  54. setup( builder ) {
  55. const colorNode = this.colorNode || builder.context.color;
  56. const toneMapping = this.toneMapping;
  57. if ( toneMapping === NoToneMapping ) return colorNode;
  58. let outputNode = null;
  59. const toneMappingFn = builder.renderer.library.getToneMappingFunction( toneMapping );
  60. if ( toneMappingFn !== null ) {
  61. outputNode = vec4( toneMappingFn( colorNode.rgb, this.exposureNode ), colorNode.a );
  62. } else {
  63. console.error( 'ToneMappingNode: Unsupported Tone Mapping configuration.', toneMapping );
  64. outputNode = colorNode;
  65. }
  66. return outputNode;
  67. }
  68. }
  69. export default ToneMappingNode;
  70. export const toneMapping = ( mapping, exposure, color ) => nodeObject( new ToneMappingNode( mapping, nodeObject( exposure ), nodeObject( color ) ) );
  71. export const toneMappingExposure = /*@__PURE__*/ rendererReference( 'toneMappingExposure', 'float' );
  72. addMethodChaining( 'toneMapping', ( color, mapping, exposure ) => toneMapping( mapping, exposure, color ) );
粤ICP备19079148号