LUTPass.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { ShaderPass } from './ShaderPass.js';
  2. const LUTShader = {
  3. name: 'LUTShader',
  4. uniforms: {
  5. lut: { value: null },
  6. lutSize: { value: 0 },
  7. tDiffuse: { value: null },
  8. intensity: { value: 1.0 },
  9. },
  10. vertexShader: /* glsl */`
  11. varying vec2 vUv;
  12. void main() {
  13. vUv = uv;
  14. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  15. }
  16. `,
  17. fragmentShader: /* glsl */`
  18. uniform float lutSize;
  19. precision highp sampler3D;
  20. uniform sampler3D lut;
  21. varying vec2 vUv;
  22. uniform float intensity;
  23. uniform sampler2D tDiffuse;
  24. void main() {
  25. vec4 val = texture2D( tDiffuse, vUv );
  26. vec4 lutVal;
  27. // pull the sample in by half a pixel so the sample begins
  28. // at the center of the edge pixels.
  29. float pixelWidth = 1.0 / lutSize;
  30. float halfPixelWidth = 0.5 / lutSize;
  31. vec3 uvw = vec3( halfPixelWidth ) + val.rgb * ( 1.0 - pixelWidth );
  32. lutVal = vec4( texture( lut, uvw ).rgb, val.a );
  33. gl_FragColor = vec4( mix( val, lutVal, intensity ) );
  34. }
  35. `,
  36. };
  37. class LUTPass extends ShaderPass {
  38. set lut( v ) {
  39. const material = this.material;
  40. if ( v !== this.lut ) {
  41. material.uniforms.lut.value = null;
  42. if ( v ) {
  43. material.uniforms.lutSize.value = v.image.width;
  44. material.uniforms.lut.value = v;
  45. }
  46. }
  47. }
  48. get lut() {
  49. return this.material.uniforms.lut.value;
  50. }
  51. set intensity( v ) {
  52. this.material.uniforms.intensity.value = v;
  53. }
  54. get intensity() {
  55. return this.material.uniforms.intensity.value;
  56. }
  57. constructor( options = {} ) {
  58. super( LUTShader );
  59. this.lut = options.lut || null;
  60. this.intensity = 'intensity' in options ? options.intensity : 1;
  61. }
  62. }
  63. export { LUTPass };