ColorSpaceNode.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import TempNode from '../core/TempNode.js';
  2. import { addMethodChaining, mat3, nodeObject, vec4 } from '../tsl/TSLCore.js';
  3. import { SRGBTransfer } from '../../constants.js';
  4. import { ColorManagement } from '../../math/ColorManagement.js';
  5. import { sRGBTransferEOTF, sRGBTransferOETF } from './ColorSpaceFunctions.js';
  6. import { Matrix3 } from '../../math/Matrix3.js';
  7. const WORKING_COLOR_SPACE = 'WorkingColorSpace';
  8. const OUTPUT_COLOR_SPACE = 'OutputColorSpace';
  9. /**
  10. * This node represents a color space conversion. Meaning it converts
  11. * a color value from a source to a target color space.
  12. *
  13. * @augments TempNode
  14. */
  15. class ColorSpaceNode extends TempNode {
  16. static get type() {
  17. return 'ColorSpaceNode';
  18. }
  19. /**
  20. * Constructs a new color space node.
  21. *
  22. * @param {Node} colorNode - Represents the color to convert.
  23. * @param {string} source - The source color space.
  24. * @param {string} target - The target color space.
  25. */
  26. constructor( colorNode, source, target ) {
  27. super( 'vec4' );
  28. /**
  29. * Represents the color to convert.
  30. *
  31. * @type {Node}
  32. */
  33. this.colorNode = colorNode;
  34. /**
  35. * The source color space.
  36. *
  37. * @type {string}
  38. */
  39. this.source = source;
  40. /**
  41. * The target color space.
  42. *
  43. * @type {string}
  44. */
  45. this.target = target;
  46. }
  47. /**
  48. * This method resolves the constants `WORKING_COLOR_SPACE` and
  49. * `OUTPUT_COLOR_SPACE` based on the current configuration of the
  50. * color management and renderer.
  51. *
  52. * @param {NodeBuilder} builder - The current node builder.
  53. * @param {string} colorSpace - The color space to resolve.
  54. * @return {string} The resolved color space.
  55. */
  56. resolveColorSpace( builder, colorSpace ) {
  57. if ( colorSpace === WORKING_COLOR_SPACE ) {
  58. return ColorManagement.workingColorSpace;
  59. } else if ( colorSpace === OUTPUT_COLOR_SPACE ) {
  60. return builder.context.outputColorSpace || builder.renderer.outputColorSpace;
  61. }
  62. return colorSpace;
  63. }
  64. setup( builder ) {
  65. const { colorNode } = this;
  66. const source = this.resolveColorSpace( builder, this.source );
  67. const target = this.resolveColorSpace( builder, this.target );
  68. let outputNode = colorNode;
  69. if ( ColorManagement.enabled === false || source === target || ! source || ! target ) {
  70. return outputNode;
  71. }
  72. if ( ColorManagement.getTransfer( source ) === SRGBTransfer ) {
  73. outputNode = vec4( sRGBTransferEOTF( outputNode.rgb ), outputNode.a );
  74. }
  75. if ( ColorManagement.getPrimaries( source ) !== ColorManagement.getPrimaries( target ) ) {
  76. outputNode = vec4(
  77. mat3( ColorManagement._getMatrix( new Matrix3(), source, target ) ).mul( outputNode.rgb ),
  78. outputNode.a
  79. );
  80. }
  81. if ( ColorManagement.getTransfer( target ) === SRGBTransfer ) {
  82. outputNode = vec4( sRGBTransferOETF( outputNode.rgb ), outputNode.a );
  83. }
  84. return outputNode;
  85. }
  86. }
  87. export default ColorSpaceNode;
  88. /**
  89. * TSL function for converting a given color node from the current working color space to the given color space.
  90. *
  91. * @tsl
  92. * @function
  93. * @param {Node} node - Represents the node to convert.
  94. * @param {string} targetColorSpace - The target color space.
  95. * @returns {ColorSpaceNode}
  96. */
  97. export const workingToColorSpace = ( node, targetColorSpace ) => new ColorSpaceNode( nodeObject( node ), WORKING_COLOR_SPACE, targetColorSpace );
  98. /**
  99. * TSL function for converting a given color node from the given color space to the current working color space.
  100. *
  101. * @tsl
  102. * @function
  103. * @param {Node} node - Represents the node to convert.
  104. * @param {string} sourceColorSpace - The source color space.
  105. * @returns {ColorSpaceNode}
  106. */
  107. export const colorSpaceToWorking = ( node, sourceColorSpace ) => new ColorSpaceNode( nodeObject( node ), sourceColorSpace, WORKING_COLOR_SPACE );
  108. /**
  109. * TSL function for converting a given color node from one color space to another one.
  110. *
  111. * @tsl
  112. * @function
  113. * @param {Node} node - Represents the node to convert.
  114. * @param {string} sourceColorSpace - The source color space.
  115. * @param {string} targetColorSpace - The target color space.
  116. * @returns {ColorSpaceNode}
  117. */
  118. export const convertColorSpace = ( node, sourceColorSpace, targetColorSpace ) => new ColorSpaceNode( nodeObject( node ), sourceColorSpace, targetColorSpace );
  119. addMethodChaining( 'workingToColorSpace', workingToColorSpace );
  120. addMethodChaining( 'colorSpaceToWorking', colorSpaceToWorking );
粤ICP备19079148号