ColorSpaceNode.js 1.7 KB

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