LUTPass.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. uniform sampler3D lut;
  20. varying vec2 vUv;
  21. uniform float intensity;
  22. uniform sampler2D tDiffuse;
  23. void main() {
  24. vec4 val = texture2D( tDiffuse, vUv );
  25. vec4 lutVal;
  26. // pull the sample in by half a pixel so the sample begins
  27. // at the center of the edge pixels.
  28. float pixelWidth = 1.0 / lutSize;
  29. float halfPixelWidth = 0.5 / lutSize;
  30. vec3 uvw = vec3( halfPixelWidth ) + val.rgb * ( 1.0 - pixelWidth );
  31. lutVal = vec4( texture( lut, uvw ).rgb, val.a );
  32. gl_FragColor = vec4( mix( val, lutVal, intensity ) );
  33. }
  34. `,
  35. };
  36. class LUTPass extends ShaderPass {
  37. set lut( v ) {
  38. const material = this.material;
  39. if ( v !== this.lut ) {
  40. material.uniforms.lut.value = null;
  41. if ( v ) {
  42. material.uniforms.lutSize.value = v.image.width;
  43. material.uniforms.lut.value = v;
  44. }
  45. }
  46. }
  47. get lut() {
  48. return this.material.uniforms.lut.value;
  49. }
  50. set intensity( v ) {
  51. this.material.uniforms.intensity.value = v;
  52. }
  53. get intensity() {
  54. return this.material.uniforms.intensity.value;
  55. }
  56. constructor( options = {} ) {
  57. super( LUTShader );
  58. this.lut = options.lut || null;
  59. this.intensity = 'intensity' in options ? options.intensity : 1;
  60. }
  61. }
  62. export { LUTPass };