ColorSpaceNode.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {
  2. GammaEncoding,
  3. LinearEncoding,
  4. RGBEEncoding,
  5. sRGBEncoding
  6. } from '../../../../build/three.module.js';
  7. import { TempNode } from '../core/TempNode.js';
  8. import { FunctionNode } from '../core/FunctionNode.js';
  9. import { ExpressionNode } from '../core/ExpressionNode.js';
  10. class ColorSpaceNode extends TempNode {
  11. constructor( input, method ) {
  12. super( 'v4' );
  13. this.input = input;
  14. this.method = method || ColorSpaceNode.LINEAR_TO_LINEAR;
  15. }
  16. generate( builder, output ) {
  17. const input = this.input.build( builder, 'v4' );
  18. const outputType = this.getType( builder );
  19. const methodNode = ColorSpaceNode.Nodes[ this.method ];
  20. const method = builder.include( methodNode );
  21. if ( method === ColorSpaceNode.LINEAR_TO_LINEAR ) {
  22. return builder.format( input, outputType, output );
  23. } else {
  24. if ( methodNode.inputs.length === 2 ) {
  25. const factor = this.factor.build( builder, 'f' );
  26. return builder.format( method + '( ' + input + ', ' + factor + ' )', outputType, output );
  27. } else {
  28. return builder.format( method + '( ' + input + ' )', outputType, output );
  29. }
  30. }
  31. }
  32. fromEncoding( encoding ) {
  33. const components = ColorSpaceNode.getEncodingComponents( encoding );
  34. this.method = 'LinearTo' + components[ 0 ];
  35. this.factor = components[ 1 ];
  36. }
  37. fromDecoding( encoding ) {
  38. const components = ColorSpaceNode.getEncodingComponents( encoding );
  39. this.method = components[ 0 ] + 'ToLinear';
  40. this.factor = components[ 1 ];
  41. }
  42. copy( source ) {
  43. super.copy( source );
  44. this.input = source.input;
  45. this.method = source.method;
  46. return this;
  47. }
  48. toJSON( meta ) {
  49. let data = this.getJSONNode( meta );
  50. if ( ! data ) {
  51. data = this.createJSONNode( meta );
  52. data.input = this.input.toJSON( meta ).uuid;
  53. data.method = this.method;
  54. }
  55. return data;
  56. }
  57. }
  58. ColorSpaceNode.Nodes = ( function () {
  59. // For a discussion of what this is, please read this: http://lousodrome.net/blog/light/2013/05/26/gamma-correct-and-hdr-rendering-in-a-32-bits-buffer/
  60. const LinearToLinear = new FunctionNode( /* glsl */`
  61. vec4 LinearToLinear( in vec4 value ) {
  62. return value;
  63. }`
  64. );
  65. const GammaToLinear = new FunctionNode( /* glsl */`
  66. vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
  67. return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
  68. }`
  69. );
  70. const LinearToGamma = new FunctionNode( /* glsl */`
  71. vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
  72. return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
  73. }`
  74. );
  75. const sRGBToLinear = new FunctionNode( /* glsl */`
  76. vec4 sRGBToLinear( in vec4 value ) {
  77. return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.w );
  78. }`
  79. );
  80. const LinearTosRGB = new FunctionNode( /* glsl */`
  81. vec4 LinearTosRGB( in vec4 value ) {
  82. 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 );
  83. }`
  84. );
  85. const RGBEToLinear = new FunctionNode( /* glsl */`
  86. vec4 RGBEToLinear( in vec4 value ) {
  87. return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
  88. }`
  89. );
  90. const LinearToRGBE = new FunctionNode( /* glsl */`
  91. vec4 LinearToRGBE( in vec4 value ) {
  92. float maxComponent = max( max( value.r, value.g ), value.b );
  93. float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
  94. return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
  95. }`
  96. );
  97. return {
  98. LinearToLinear: LinearToLinear,
  99. GammaToLinear: GammaToLinear,
  100. LinearToGamma: LinearToGamma,
  101. sRGBToLinear: sRGBToLinear,
  102. LinearTosRGB: LinearTosRGB,
  103. RGBEToLinear: RGBEToLinear,
  104. LinearToRGBE: LinearToRGBE
  105. };
  106. } )();
  107. ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
  108. ColorSpaceNode.GAMMA_TO_LINEAR = 'GammaToLinear';
  109. ColorSpaceNode.LINEAR_TO_GAMMA = 'LinearToGamma';
  110. ColorSpaceNode.SRGB_TO_LINEAR = 'sRGBToLinear';
  111. ColorSpaceNode.LINEAR_TO_SRGB = 'LinearTosRGB';
  112. ColorSpaceNode.RGBE_TO_LINEAR = 'RGBEToLinear';
  113. ColorSpaceNode.LINEAR_TO_RGBE = 'LinearToRGBE';
  114. ColorSpaceNode.getEncodingComponents = function ( encoding ) {
  115. switch ( encoding ) {
  116. case LinearEncoding:
  117. return [ 'Linear' ];
  118. case sRGBEncoding:
  119. return [ 'sRGB' ];
  120. case RGBEEncoding:
  121. return [ 'RGBE' ];
  122. case GammaEncoding:
  123. return [ 'Gamma', new ExpressionNode( 'float( GAMMA_FACTOR )', 'f' ) ];
  124. }
  125. };
  126. ColorSpaceNode.prototype.nodeType = 'ColorSpace';
  127. ColorSpaceNode.prototype.hashProperties = [ 'method' ];
  128. export { ColorSpaceNode };