2
0

LUTPass.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { ShaderPass } from './ShaderPass.js';
  2. const LUTShader = {
  3. name: 'LUTShader',
  4. defines: {
  5. USE_3DTEXTURE: 1,
  6. },
  7. uniforms: {
  8. lut3d: { value: null },
  9. lut: { value: null },
  10. lutSize: { value: 0 },
  11. tDiffuse: { value: null },
  12. intensity: { value: 1.0 },
  13. },
  14. vertexShader: /* glsl */`
  15. varying vec2 vUv;
  16. void main() {
  17. vUv = uv;
  18. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  19. }
  20. `,
  21. fragmentShader: /* glsl */`
  22. uniform float lutSize;
  23. #if USE_3DTEXTURE
  24. precision highp sampler3D;
  25. uniform sampler3D lut3d;
  26. #else
  27. uniform sampler2D lut;
  28. vec3 lutLookup( sampler2D tex, float size, vec3 rgb ) {
  29. float sliceHeight = 1.0 / size;
  30. float yPixelHeight = 1.0 / ( size * size );
  31. // Get the slices on either side of the sample
  32. float slice = rgb.b * size;
  33. float interp = fract( slice );
  34. float slice0 = slice - interp;
  35. float centeredInterp = interp - 0.5;
  36. float slice1 = slice0 + sign( centeredInterp );
  37. // Pull y sample in by half a pixel in each direction to avoid color
  38. // bleeding from adjacent slices.
  39. float greenOffset = clamp( rgb.g * sliceHeight, yPixelHeight * 0.5, sliceHeight - yPixelHeight * 0.5 );
  40. vec2 uv0 = vec2(
  41. rgb.r,
  42. slice0 * sliceHeight + greenOffset
  43. );
  44. vec2 uv1 = vec2(
  45. rgb.r,
  46. slice1 * sliceHeight + greenOffset
  47. );
  48. vec3 sample0 = texture2D( tex, uv0 ).rgb;
  49. vec3 sample1 = texture2D( tex, uv1 ).rgb;
  50. return mix( sample0, sample1, abs( centeredInterp ) );
  51. }
  52. #endif
  53. varying vec2 vUv;
  54. uniform float intensity;
  55. uniform sampler2D tDiffuse;
  56. void main() {
  57. vec4 val = texture2D( tDiffuse, vUv );
  58. vec4 lutVal;
  59. // pull the sample in by half a pixel so the sample begins
  60. // at the center of the edge pixels.
  61. float pixelWidth = 1.0 / lutSize;
  62. float halfPixelWidth = 0.5 / lutSize;
  63. vec3 uvw = vec3( halfPixelWidth ) + val.rgb * ( 1.0 - pixelWidth );
  64. #if USE_3DTEXTURE
  65. lutVal = vec4( texture( lut3d, uvw ).rgb, val.a );
  66. #else
  67. lutVal = vec4( lutLookup( lut, lutSize, uvw ), val.a );
  68. #endif
  69. gl_FragColor = vec4( mix( val, lutVal, intensity ) );
  70. }
  71. `,
  72. };
  73. class LUTPass extends ShaderPass {
  74. set lut( v ) {
  75. const material = this.material;
  76. if ( v !== this.lut ) {
  77. material.uniforms.lut3d.value = null;
  78. material.uniforms.lut.value = null;
  79. if ( v ) {
  80. const is3dTextureDefine = v.isData3DTexture ? 1 : 0;
  81. if ( is3dTextureDefine !== material.defines.USE_3DTEXTURE ) {
  82. material.defines.USE_3DTEXTURE = is3dTextureDefine;
  83. material.needsUpdate = true;
  84. }
  85. material.uniforms.lutSize.value = v.image.width;
  86. if ( v.isData3DTexture ) {
  87. material.uniforms.lut3d.value = v;
  88. } else {
  89. material.uniforms.lut.value = v;
  90. }
  91. }
  92. }
  93. }
  94. get lut() {
  95. return this.material.uniforms.lut.value || this.material.uniforms.lut3d.value;
  96. }
  97. set intensity( v ) {
  98. this.material.uniforms.intensity.value = v;
  99. }
  100. get intensity() {
  101. return this.material.uniforms.intensity.value;
  102. }
  103. constructor( options = {} ) {
  104. super( LUTShader );
  105. this.lut = options.lut || null;
  106. this.intensity = 'intensity' in options ? options.intensity : 1;
  107. }
  108. }
  109. export { LUTPass };