ColorSpaceNode.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import {
  2. GammaEncoding,
  3. LinearEncoding,
  4. RGBEEncoding,
  5. RGBDEncoding,
  6. sRGBEncoding
  7. } from '../../../../build/three.module.js';
  8. import { TempNode } from '../core/TempNode.js';
  9. import { FloatNode } from '../inputs/FloatNode.js';
  10. import { FunctionNode } from '../core/FunctionNode.js';
  11. import { ExpressionNode } from '../core/ExpressionNode.js';
  12. class ColorSpaceNode extends TempNode {
  13. constructor( input, method ) {
  14. super( 'v4' );
  15. this.input = input;
  16. this.method = method || ColorSpaceNode.LINEAR_TO_LINEAR;
  17. }
  18. generate( builder, output ) {
  19. const input = this.input.build( builder, 'v4' );
  20. const outputType = this.getType( builder );
  21. const methodNode = ColorSpaceNode.Nodes[ this.method ];
  22. const method = builder.include( methodNode );
  23. if ( method === ColorSpaceNode.LINEAR_TO_LINEAR ) {
  24. return builder.format( input, outputType, output );
  25. } else {
  26. if ( methodNode.inputs.length === 2 ) {
  27. const factor = this.factor.build( builder, 'f' );
  28. return builder.format( method + '( ' + input + ', ' + factor + ' )', outputType, output );
  29. } else {
  30. return builder.format( method + '( ' + input + ' )', outputType, output );
  31. }
  32. }
  33. }
  34. fromEncoding( encoding ) {
  35. const components = ColorSpaceNode.getEncodingComponents( encoding );
  36. this.method = 'LinearTo' + components[ 0 ];
  37. this.factor = components[ 1 ];
  38. }
  39. fromDecoding( encoding ) {
  40. const components = ColorSpaceNode.getEncodingComponents( encoding );
  41. this.method = components[ 0 ] + 'ToLinear';
  42. this.factor = components[ 1 ];
  43. }
  44. copy( source ) {
  45. super.copy( source );
  46. this.input = source.input;
  47. this.method = source.method;
  48. return this;
  49. }
  50. toJSON( meta ) {
  51. let data = this.getJSONNode( meta );
  52. if ( ! data ) {
  53. data = this.createJSONNode( meta );
  54. data.input = this.input.toJSON( meta ).uuid;
  55. data.method = this.method;
  56. }
  57. return data;
  58. }
  59. }
  60. ColorSpaceNode.Nodes = ( function () {
  61. // 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/
  62. const LinearToLinear = new FunctionNode( /* glsl */`
  63. vec4 LinearToLinear( in vec4 value ) {
  64. return value;
  65. }`
  66. );
  67. const GammaToLinear = new FunctionNode( /* glsl */`
  68. vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
  69. return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
  70. }`
  71. );
  72. const LinearToGamma = new FunctionNode( /* glsl */`
  73. vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
  74. return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
  75. }`
  76. );
  77. const sRGBToLinear = new FunctionNode( /* glsl */`
  78. vec4 sRGBToLinear( in vec4 value ) {
  79. 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 );
  80. }`
  81. );
  82. const LinearTosRGB = new FunctionNode( /* glsl */`
  83. vec4 LinearTosRGB( in vec4 value ) {
  84. 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 );
  85. }`
  86. );
  87. const RGBEToLinear = new FunctionNode( /* glsl */`
  88. vec4 RGBEToLinear( in vec4 value ) {
  89. return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
  90. }`
  91. );
  92. const LinearToRGBE = new FunctionNode( /* glsl */`
  93. vec4 LinearToRGBE( in vec4 value ) {
  94. float maxComponent = max( max( value.r, value.g ), value.b );
  95. float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
  96. return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
  97. }`
  98. );
  99. // reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
  100. const RGBDToLinear = new FunctionNode( /* glsl */`
  101. vec3 RGBDToLinear( in vec4 value, in float maxRange ) {
  102. return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
  103. }`
  104. );
  105. const LinearToRGBD = new FunctionNode( /* glsl */`
  106. vec3 LinearToRGBD( in vec4 value, in float maxRange ) {
  107. float maxRGB = max( value.x, max( value.g, value.b ) );
  108. float D = max( maxRange / maxRGB, 1.0 );
  109. D = clamp( floor( D ) / 255.0, 0.0, 1.0 );
  110. return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
  111. }`
  112. );
  113. return {
  114. LinearToLinear: LinearToLinear,
  115. GammaToLinear: GammaToLinear,
  116. LinearToGamma: LinearToGamma,
  117. sRGBToLinear: sRGBToLinear,
  118. LinearTosRGB: LinearTosRGB,
  119. RGBEToLinear: RGBEToLinear,
  120. LinearToRGBE: LinearToRGBE,
  121. RGBDToLinear: RGBDToLinear,
  122. LinearToRGBD: LinearToRGBD
  123. };
  124. } )();
  125. ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
  126. ColorSpaceNode.GAMMA_TO_LINEAR = 'GammaToLinear';
  127. ColorSpaceNode.LINEAR_TO_GAMMA = 'LinearToGamma';
  128. ColorSpaceNode.SRGB_TO_LINEAR = 'sRGBToLinear';
  129. ColorSpaceNode.LINEAR_TO_SRGB = 'LinearTosRGB';
  130. ColorSpaceNode.RGBE_TO_LINEAR = 'RGBEToLinear';
  131. ColorSpaceNode.LINEAR_TO_RGBE = 'LinearToRGBE';
  132. ColorSpaceNode.RGBD_TO_LINEAR = 'RGBDToLinear';
  133. ColorSpaceNode.LINEAR_TO_RGBD = 'LinearToRGBD';
  134. ColorSpaceNode.getEncodingComponents = function ( encoding ) {
  135. switch ( encoding ) {
  136. case LinearEncoding:
  137. return [ 'Linear' ];
  138. case sRGBEncoding:
  139. return [ 'sRGB' ];
  140. case RGBEEncoding:
  141. return [ 'RGBE' ];
  142. case RGBDEncoding:
  143. return [ 'RGBD', new FloatNode( 256.0 ).setReadonly( true ) ];
  144. case GammaEncoding:
  145. return [ 'Gamma', new ExpressionNode( 'float( GAMMA_FACTOR )', 'f' ) ];
  146. }
  147. };
  148. ColorSpaceNode.prototype.nodeType = 'ColorSpace';
  149. ColorSpaceNode.prototype.hashProperties = [ 'method' ];
  150. export { ColorSpaceNode };