LUTPass.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { ShaderPass } from './ShaderPass.js';
  2. const LUTShader = {
  3. defines: {
  4. USE_3DTEXTURE: 1,
  5. },
  6. uniforms: {
  7. lut3d: { value: null },
  8. lut: { value: null },
  9. lutSize: { value: 0 },
  10. tDiffuse: { value: null },
  11. intensity: { value: 1.0 },
  12. },
  13. vertexShader: /* glsl */`
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }
  19. `,
  20. fragmentShader: /* glsl */`
  21. precision highp sampler3D;
  22. #if USE_3DTEXTURE
  23. uniform sampler3D lut3d;
  24. #else
  25. uniform sampler2D lut;
  26. uniform float lutSize;
  27. vec3 lutLookup( sampler2D tex, float size, vec3 rgb ) {
  28. // clamp the sample in by half a pixel to avoid interpolation
  29. // artifacts between slices laid out next to each other.
  30. float halfPixelWidth = 0.5 / size;
  31. rgb.rg = clamp( rgb.rg, halfPixelWidth, 1.0 - halfPixelWidth );
  32. // green offset into a LUT layer
  33. float gOffset = rgb.g / size;
  34. vec2 uv1 = vec2( rgb.r, gOffset );
  35. vec2 uv2 = vec2( rgb.r, gOffset );
  36. // adjust b slice offset
  37. float bNormalized = size * rgb.b;
  38. float bSlice = min( floor( size * rgb.b ), size - 1.0 );
  39. float bMix = ( bNormalized - bSlice ) / size;
  40. // get the first lut slice and then the one to interpolate to
  41. float b1 = bSlice / size;
  42. float b2 = ( bSlice + 1.0 ) / size;
  43. uv1.y += b1;
  44. uv2.y += b2;
  45. vec3 sample1 = texture2D( tex, uv1 ).rgb;
  46. vec3 sample2 = texture2D( tex, uv2 ).rgb;
  47. return mix( sample1, sample2, bMix );
  48. }
  49. #endif
  50. varying vec2 vUv;
  51. uniform float intensity;
  52. uniform sampler2D tDiffuse;
  53. void main() {
  54. vec4 val = texture2D( tDiffuse, vUv );
  55. vec4 lutVal;
  56. #if USE_3DTEXTURE
  57. lutVal = vec4( texture( lut3d, val.rgb ).rgb, val.a );
  58. #else
  59. lutVal = vec4( lutLookup( lut, lutSize, val.rgb ), val.a );
  60. #endif
  61. gl_FragColor = mix( val, lutVal, intensity );
  62. }
  63. `,
  64. };
  65. class LUTPass extends ShaderPass {
  66. set lut( v ) {
  67. const material = this.material;
  68. if ( v !== this.lut ) {
  69. material.uniforms.lut3d.value = null;
  70. material.uniforms.lut.value = null;
  71. if ( v ) {
  72. const is3dTextureDefine = v.isDataTexture3D ? 1 : 0;
  73. if ( is3dTextureDefine !== material.defines.USE_3DTEXTURE ) {
  74. material.defines.USE_3DTEXTURE = is3dTextureDefine;
  75. material.needsUpdate = true;
  76. }
  77. if ( v.isDataTexture3D ) {
  78. material.uniforms.lut3d.value = v;
  79. } else {
  80. material.uniforms.lut.value = v;
  81. material.uniforms.lutSize.value = v.image.width;
  82. }
  83. }
  84. }
  85. }
  86. get lut() {
  87. return this.material.uniforms.lut.value || this.material.uniforms.lut3d.value;
  88. }
  89. set intensity( v ) {
  90. this.material.uniforms.intensity.value = v;
  91. }
  92. get intensity() {
  93. return this.material.uniforms.intensity.value;
  94. }
  95. constructor( options = {} ) {
  96. super( LUTShader );
  97. this.lut = options.lut || null;
  98. this.intensity = 'intensity' in options ? options.intensity : 1;
  99. }
  100. }
  101. export { LUTPass };