LUTImageLoader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import {
  2. Loader,
  3. TextureLoader,
  4. Data3DTexture,
  5. RGBAFormat,
  6. UnsignedByteType,
  7. ClampToEdgeWrapping,
  8. LinearFilter,
  9. } from 'three';
  10. /**
  11. * A loader for loading LUT images.
  12. *
  13. * ```js
  14. * const loader = new LUTImageLoader();
  15. * const map = loader.loadAsync( 'luts/NeutralLUT.png' );
  16. * ```
  17. *
  18. * @augments Loader
  19. * @three_import import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js';
  20. */
  21. export class LUTImageLoader extends Loader {
  22. /**
  23. * Constructs a new LUT loader.
  24. *
  25. * @param {LoadingManager} [manager] - The loading manager.
  26. */
  27. constructor( manager ) {
  28. super( manager );
  29. /**
  30. * Whether to vertically flip the LUT or not.
  31. *
  32. * Depending on the LUT's origin, the texture has green at the bottom (e.g. for Unreal)
  33. * or green at the top (e.g. for Unity URP Color Lookup). If you're using lut image strips
  34. * from a Unity pipeline, then set this property to `true`.
  35. *
  36. * @type {boolean}
  37. * @default false
  38. */
  39. this.flip = false;
  40. }
  41. /**
  42. * Starts loading from the given URL and passes the loaded LUT
  43. * to the `onLoad()` callback.
  44. *
  45. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  46. * @param {function({size:number,texture3D:Data3DTexture})} onLoad - Executed when the loading process has been finished.
  47. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  48. * @param {onErrorCallback} onError - Executed when errors occur.
  49. */
  50. load( url, onLoad, onProgress, onError ) {
  51. const loader = new TextureLoader( this.manager );
  52. loader.setCrossOrigin( this.crossOrigin );
  53. loader.setPath( this.path );
  54. loader.load( url, texture => {
  55. try {
  56. let imageData;
  57. if ( texture.image.width < texture.image.height ) {
  58. imageData = this._getImageData( texture );
  59. } else {
  60. imageData = this._horz2Vert( texture );
  61. }
  62. onLoad( this.parse( imageData.data, Math.min( texture.image.width, texture.image.height ) ) );
  63. } catch ( e ) {
  64. if ( onError ) {
  65. onError( e );
  66. } else {
  67. console.error( e );
  68. }
  69. this.manager.itemError( url );
  70. }
  71. }, onProgress, onError );
  72. }
  73. /**
  74. * Parses the given LUT data and returns the resulting 3D data texture.
  75. *
  76. * @param {Uint8ClampedArray} dataArray - The raw LUT data.
  77. * @param {number} size - The LUT size.
  78. * @return {{size:number,texture3D:Data3DTexture}} An object representing the parsed LUT.
  79. */
  80. parse( dataArray, size ) {
  81. const data = new Uint8Array( dataArray );
  82. const texture3D = new Data3DTexture();
  83. texture3D.image.data = data;
  84. texture3D.image.width = size;
  85. texture3D.image.height = size;
  86. texture3D.image.depth = size;
  87. texture3D.format = RGBAFormat;
  88. texture3D.type = UnsignedByteType;
  89. texture3D.magFilter = LinearFilter;
  90. texture3D.minFilter = LinearFilter;
  91. texture3D.wrapS = ClampToEdgeWrapping;
  92. texture3D.wrapT = ClampToEdgeWrapping;
  93. texture3D.wrapR = ClampToEdgeWrapping;
  94. texture3D.generateMipmaps = false;
  95. texture3D.needsUpdate = true;
  96. return {
  97. size,
  98. texture3D,
  99. };
  100. }
  101. // internal
  102. _getImageData( texture ) {
  103. const width = texture.image.width;
  104. const height = texture.image.height;
  105. const canvas = document.createElement( 'canvas' );
  106. canvas.width = width;
  107. canvas.height = height;
  108. const context = canvas.getContext( '2d' );
  109. if ( this.flip === true ) {
  110. context.scale( 1, - 1 );
  111. context.translate( 0, - height );
  112. }
  113. context.drawImage( texture.image, 0, 0 );
  114. return context.getImageData( 0, 0, width, height );
  115. }
  116. _horz2Vert( texture ) {
  117. const width = texture.image.height;
  118. const height = texture.image.width;
  119. const canvas = document.createElement( 'canvas' );
  120. canvas.width = width;
  121. canvas.height = height;
  122. const context = canvas.getContext( '2d' );
  123. if ( this.flip === true ) {
  124. context.scale( 1, - 1 );
  125. context.translate( 0, - height );
  126. }
  127. for ( let i = 0; i < width; i ++ ) {
  128. const sy = i * width;
  129. const dy = ( this.flip ) ? height - i * width : i * width;
  130. context.drawImage( texture.image, sy, 0, width, width, 0, dy, width, width );
  131. }
  132. return context.getImageData( 0, 0, width, height );
  133. }
  134. }