ColorSpaceNode.js 6.5 KB

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