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. constructor( method, node ) {
  22. super( 'vec4' );
  23. this.method = method;
  24. this.node = node;
  25. }
  26. fromEncoding( encoding ) {
  27. let method = null;
  28. if ( encoding === LinearEncoding ) {
  29. method = 'Linear';
  30. } else if ( encoding === sRGBEncoding ) {
  31. method = 'sRGB';
  32. }
  33. this.method = 'LinearTo' + method;
  34. return this;
  35. }
  36. construct() {
  37. const method = this.method;
  38. const node = this.node;
  39. let outputNode = null;
  40. if ( method !== ColorSpaceNode.LINEAR_TO_LINEAR ) {
  41. const encodingFunctionNode = EncodingLib[ method ];
  42. outputNode = encodingFunctionNode.call( {
  43. value: node
  44. } );
  45. } else {
  46. outputNode = node;
  47. }
  48. return outputNode;
  49. }
  50. }
  51. ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
  52. ColorSpaceNode.LINEAR_TO_SRGB = 'LinearTosRGB';
  53. export default ColorSpaceNode;