ColorSpaceNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import TempNode from '../core/Node.js';
  2. import { ShaderNode, vec3, pow, mul, sub, mix, vec4, lessThanEqual } from '../shadernode/ShaderNodeBaseElements.js';
  3. import { LinearEncoding, sRGBEncoding } from 'three';
  4. export const LinearToLinear = new ShaderNode( ( inputs ) => {
  5. return inputs.value;
  6. } );
  7. export const LinearTosRGB = new ShaderNode( ( inputs ) => {
  8. const { value } = inputs;
  9. const rgb = value.rgb;
  10. const a = sub( mul( pow( value.rgb, vec3( 0.41666 ) ), 1.055 ), vec3( 0.055 ) );
  11. const b = mul( rgb, 12.92 );
  12. const factor = vec3( lessThanEqual( rgb, vec3( 0.0031308 ) ) );
  13. const rgbResult = mix( a, b, factor );
  14. return vec4( rgbResult, value.a );
  15. } );
  16. const EncodingLib = {
  17. LinearToLinear,
  18. LinearTosRGB
  19. };
  20. class ColorSpaceNode extends TempNode {
  21. static LINEAR_TO_LINEAR = 'LinearToLinear';
  22. static LINEAR_TO_SRGB = 'LinearTosRGB';
  23. constructor( method, node ) {
  24. super( 'vec4' );
  25. this.method = method;
  26. this.node = node;
  27. }
  28. fromEncoding( encoding ) {
  29. let method = null;
  30. if ( encoding === LinearEncoding ) {
  31. method = 'Linear';
  32. } else if ( encoding === sRGBEncoding ) {
  33. method = 'sRGB';
  34. }
  35. this.method = 'LinearTo' + method;
  36. return this;
  37. }
  38. construct() {
  39. const method = this.method;
  40. const node = this.node;
  41. let outputNode = null;
  42. if ( method !== ColorSpaceNode.LINEAR_TO_LINEAR ) {
  43. const encodingFunctionNode = EncodingLib[ method ];
  44. outputNode = encodingFunctionNode.call( {
  45. value: node
  46. } );
  47. } else {
  48. outputNode = node;
  49. }
  50. return outputNode;
  51. }
  52. }
  53. export default ColorSpaceNode;