LUTImageLoader.js 4.0 KB

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