ColorSpaceNode.js 1.6 KB

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