LUTImageLoader.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. Loader,
  3. TextureLoader,
  4. DataTexture,
  5. Data3DTexture,
  6. RGBAFormat,
  7. UnsignedByteType,
  8. ClampToEdgeWrapping,
  9. LinearFilter,
  10. } from 'three';
  11. export class LUTImageLoader extends Loader {
  12. constructor( flipVertical = false ) {
  13. //The NeutralLUT.png has green at the bottom for Unreal ang green at the top for Unity URP Color Lookup
  14. //post-processing. If you're using lut image strips from a Unity pipeline then pass true to the constructor
  15. super();
  16. this.flip = flipVertical;
  17. }
  18. load( url, onLoad, onProgress, onError ) {
  19. const loader = new TextureLoader( this.manager );
  20. loader.setCrossOrigin( this.crossOrigin );
  21. loader.setPath( this.path );
  22. loader.load( url, texture => {
  23. try {
  24. let imageData;
  25. if ( texture.image.width < texture.image.height ) {
  26. imageData = this.getImageData( texture );
  27. } else {
  28. imageData = this.horz2Vert( texture );
  29. }
  30. onLoad( this.parse( imageData.data, Math.min( texture.image.width, texture.image.height ) ) );
  31. } catch ( e ) {
  32. if ( onError ) {
  33. onError( e );
  34. } else {
  35. console.error( e );
  36. }
  37. this.manager.itemError( url );
  38. }
  39. }, onProgress, onError );
  40. }
  41. getImageData( texture ) {
  42. const width = texture.image.width;
  43. const height = texture.image.height;
  44. const canvas = document.createElement( 'canvas' );
  45. canvas.width = width;
  46. canvas.height = height;
  47. const context = canvas.getContext( '2d' );
  48. if ( this.flip === true ) {
  49. context.scale( 1, - 1 );
  50. context.translate( 0, - height );
  51. }
  52. context.drawImage( texture.image, 0, 0 );
  53. return context.getImageData( 0, 0, width, height );
  54. }
  55. horz2Vert( texture ) {
  56. const width = texture.image.height;
  57. const height = texture.image.width;
  58. const canvas = document.createElement( 'canvas' );
  59. canvas.width = width;
  60. canvas.height = height;
  61. const context = canvas.getContext( '2d' );
  62. if ( this.flip === true ) {
  63. context.scale( 1, - 1 );
  64. context.translate( 0, - height );
  65. }
  66. for ( let i = 0; i < width; i ++ ) {
  67. const sy = i * width;
  68. const dy = ( this.flip ) ? height - i * width : i * width;
  69. context.drawImage( texture.image, sy, 0, width, width, 0, dy, width, width );
  70. }
  71. return context.getImageData( 0, 0, width, height );
  72. }
  73. parse( dataArray, size ) {
  74. const data = new Uint8Array( dataArray );
  75. const texture = new DataTexture();
  76. texture.image.data = data;
  77. texture.image.width = size;
  78. texture.image.height = size * size;
  79. texture.format = RGBAFormat;
  80. texture.type = UnsignedByteType;
  81. texture.magFilter = LinearFilter;
  82. texture.minFilter = LinearFilter;
  83. texture.wrapS = ClampToEdgeWrapping;
  84. texture.wrapT = ClampToEdgeWrapping;
  85. texture.generateMipmaps = false;
  86. texture.needsUpdate = true;
  87. const texture3D = new Data3DTexture();
  88. texture3D.image.data = data;
  89. texture3D.image.width = size;
  90. texture3D.image.height = size;
  91. texture3D.image.depth = size;
  92. texture3D.format = RGBAFormat;
  93. texture3D.type = UnsignedByteType;
  94. texture3D.magFilter = LinearFilter;
  95. texture3D.minFilter = LinearFilter;
  96. texture3D.wrapS = ClampToEdgeWrapping;
  97. texture3D.wrapT = ClampToEdgeWrapping;
  98. texture3D.wrapR = ClampToEdgeWrapping;
  99. texture3D.generateMipmaps = false;
  100. texture3D.needsUpdate = true;
  101. return {
  102. size,
  103. texture,
  104. texture3D,
  105. };
  106. }
  107. }