ColorSpaceNode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {
  2. LinearEncoding,
  3. sRGBEncoding
  4. } from 'three';
  5. import { TempNode } from '../core/TempNode.js';
  6. import { FunctionNode } from '../core/FunctionNode.js';
  7. class ColorSpaceNode extends TempNode {
  8. constructor( input, method ) {
  9. super( 'v4' );
  10. this.input = input;
  11. this.method = method || ColorSpaceNode.LINEAR_TO_LINEAR;
  12. }
  13. generate( builder, output ) {
  14. const input = this.input.build( builder, 'v4' );
  15. const outputType = this.getType( builder );
  16. const methodNode = ColorSpaceNode.Nodes[ this.method ];
  17. const method = builder.include( methodNode );
  18. if ( method === ColorSpaceNode.LINEAR_TO_LINEAR ) {
  19. return builder.format( input, outputType, output );
  20. } else {
  21. if ( methodNode.inputs.length === 2 ) {
  22. const factor = this.factor.build( builder, 'f' );
  23. return builder.format( method + '( ' + input + ', ' + factor + ' )', outputType, output );
  24. } else {
  25. return builder.format( method + '( ' + input + ' )', outputType, output );
  26. }
  27. }
  28. }
  29. fromEncoding( encoding ) {
  30. const components = ColorSpaceNode.getEncodingComponents( encoding );
  31. this.method = 'LinearTo' + components[ 0 ];
  32. this.factor = components[ 1 ];
  33. }
  34. fromDecoding() {
  35. // TODO: Remove fromDecoding()
  36. const components = ColorSpaceNode.getEncodingComponents( LinearEncoding );
  37. this.method = components[ 0 ] + 'ToLinear';
  38. this.factor = components[ 1 ];
  39. }
  40. copy( source ) {
  41. super.copy( source );
  42. this.input = source.input;
  43. this.method = source.method;
  44. return this;
  45. }
  46. toJSON( meta ) {
  47. let data = this.getJSONNode( meta );
  48. if ( ! data ) {
  49. data = this.createJSONNode( meta );
  50. data.input = this.input.toJSON( meta ).uuid;
  51. data.method = this.method;
  52. }
  53. return data;
  54. }
  55. }
  56. ColorSpaceNode.Nodes = ( function () {
  57. const LinearToLinear = new FunctionNode( /* glsl */`
  58. vec4 LinearToLinear( in vec4 value ) {
  59. return value;
  60. }`
  61. );
  62. const LinearTosRGB = new FunctionNode( /* glsl */`
  63. vec4 LinearTosRGB( in vec4 value ) {
  64. return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.w );
  65. }`
  66. );
  67. return {
  68. LinearToLinear: LinearToLinear,
  69. LinearTosRGB: LinearTosRGB
  70. };
  71. } )();
  72. ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
  73. ColorSpaceNode.LINEAR_TO_SRGB = 'LinearTosRGB';
  74. ColorSpaceNode.getEncodingComponents = function ( encoding ) {
  75. switch ( encoding ) {
  76. case LinearEncoding:
  77. return [ 'Linear' ];
  78. case sRGBEncoding:
  79. return [ 'sRGB' ];
  80. }
  81. };
  82. ColorSpaceNode.prototype.nodeType = 'ColorSpace';
  83. ColorSpaceNode.prototype.hashProperties = [ 'method' ];
  84. export { ColorSpaceNode };