LUT3dlLoader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {
  2. ClampToEdgeWrapping,
  3. Data3DTexture,
  4. FileLoader,
  5. LinearFilter,
  6. Loader,
  7. RGBAFormat,
  8. UnsignedByteType,
  9. } from 'three';
  10. /**
  11. * A loader for the 3DL LUT format.
  12. *
  13. * References:
  14. * - [3D LUTs]{@link http://download.autodesk.com/us/systemdocs/help/2011/lustre/index.html?url=./files/WSc4e151a45a3b785a24c3d9a411df9298473-7ffd.htm,topicNumber=d0e9492}
  15. * - [Format Spec for .3dl]{@link https://community.foundry.com/discuss/topic/103636/format-spec-for-3dl?mode=Post&postID=895258}
  16. *
  17. * ```js
  18. * const loader = new LUT3dlLoader();
  19. * const map = loader.loadAsync( 'luts/Presetpro-Cinematic.3dl' );
  20. * ```
  21. *
  22. * @augments Loader
  23. */
  24. export class LUT3dlLoader extends Loader {
  25. /**
  26. * Constructs a new 3DL LUT loader.
  27. *
  28. * @param {LoadingManager} [manager] - The loading manager.
  29. */
  30. constructor( manager ) {
  31. super( manager );
  32. /**
  33. * The texture type.
  34. *
  35. * @type {(UnsignedByteType|FloatType)}
  36. * @default UnsignedByteType
  37. */
  38. this.type = UnsignedByteType;
  39. }
  40. /**
  41. * Sets the texture type.
  42. *
  43. * @param {(UnsignedByteType|FloatType)} type - The texture type to set.
  44. * @return {LUT3dlLoader} A reference to this loader.
  45. */
  46. setType( type ) {
  47. this.type = type;
  48. return this;
  49. }
  50. /**
  51. * Starts loading from the given URL and passes the loaded 3DL LUT asset
  52. * to the `onLoad()` callback.
  53. *
  54. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  55. * @param {function({size:number,texture3D:Data3DTexture})} onLoad - Executed when the loading process has been finished.
  56. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  57. * @param {onErrorCallback} onError - Executed when errors occur.
  58. */
  59. load( url, onLoad, onProgress, onError ) {
  60. const loader = new FileLoader( this.manager );
  61. loader.setPath( this.path );
  62. loader.setResponseType( 'text' );
  63. loader.load( url, text => {
  64. try {
  65. onLoad( this.parse( text ) );
  66. } catch ( e ) {
  67. if ( onError ) {
  68. onError( e );
  69. } else {
  70. console.error( e );
  71. }
  72. this.manager.itemError( url );
  73. }
  74. }, onProgress, onError );
  75. }
  76. /**
  77. * Parses the given 3DL LUT data and returns the resulting 3D data texture.
  78. *
  79. * @param {string} input - The raw 3DL LUT data as a string.
  80. * @return {{size:number,texture3D:Data3DTexture}} The parsed 3DL LUT.
  81. */
  82. parse( input ) {
  83. const regExpGridInfo = /^[\d ]+$/m;
  84. const regExpDataPoints = /^([\d.e+-]+) +([\d.e+-]+) +([\d.e+-]+) *$/gm;
  85. // The first line describes the positions of values on the LUT grid.
  86. let result = regExpGridInfo.exec( input );
  87. if ( result === null ) {
  88. throw new Error( 'LUT3dlLoader: Missing grid information' );
  89. }
  90. const gridLines = result[ 0 ].trim().split( /\s+/g ).map( Number );
  91. const gridStep = gridLines[ 1 ] - gridLines[ 0 ];
  92. const size = gridLines.length;
  93. const sizeSq = size ** 2;
  94. for ( let i = 1, l = gridLines.length; i < l; ++ i ) {
  95. if ( gridStep !== ( gridLines[ i ] - gridLines[ i - 1 ] ) ) {
  96. throw new Error( 'LUT3dlLoader: Inconsistent grid size' );
  97. }
  98. }
  99. const dataFloat = new Float32Array( size ** 3 * 4 );
  100. let maxValue = 0.0;
  101. let index = 0;
  102. while ( ( result = regExpDataPoints.exec( input ) ) !== null ) {
  103. const r = Number( result[ 1 ] );
  104. const g = Number( result[ 2 ] );
  105. const b = Number( result[ 3 ] );
  106. maxValue = Math.max( maxValue, r, g, b );
  107. const bLayer = index % size;
  108. const gLayer = Math.floor( index / size ) % size;
  109. const rLayer = Math.floor( index / ( sizeSq ) ) % size;
  110. // b grows first, then g, then r.
  111. const d4 = ( bLayer * sizeSq + gLayer * size + rLayer ) * 4;
  112. dataFloat[ d4 + 0 ] = r;
  113. dataFloat[ d4 + 1 ] = g;
  114. dataFloat[ d4 + 2 ] = b;
  115. ++ index;
  116. }
  117. // Determine the bit depth to scale the values to [0.0, 1.0].
  118. const bits = Math.ceil( Math.log2( maxValue ) );
  119. const maxBitValue = Math.pow( 2, bits );
  120. const data = this.type === UnsignedByteType ? new Uint8Array( dataFloat.length ) : dataFloat;
  121. const scale = this.type === UnsignedByteType ? 255 : 1;
  122. for ( let i = 0, l = data.length; i < l; i += 4 ) {
  123. const i1 = i + 1;
  124. const i2 = i + 2;
  125. const i3 = i + 3;
  126. // Note: data is dataFloat when type is FloatType.
  127. data[ i ] = dataFloat[ i ] / maxBitValue * scale;
  128. data[ i1 ] = dataFloat[ i1 ] / maxBitValue * scale;
  129. data[ i2 ] = dataFloat[ i2 ] / maxBitValue * scale;
  130. data[ i3 ] = scale;
  131. }
  132. const texture3D = new Data3DTexture();
  133. texture3D.image.data = data;
  134. texture3D.image.width = size;
  135. texture3D.image.height = size;
  136. texture3D.image.depth = size;
  137. texture3D.format = RGBAFormat;
  138. texture3D.type = this.type;
  139. texture3D.magFilter = LinearFilter;
  140. texture3D.minFilter = LinearFilter;
  141. texture3D.wrapS = ClampToEdgeWrapping;
  142. texture3D.wrapT = ClampToEdgeWrapping;
  143. texture3D.wrapR = ClampToEdgeWrapping;
  144. texture3D.generateMipmaps = false;
  145. texture3D.needsUpdate = true;
  146. return {
  147. size,
  148. texture3D,
  149. };
  150. }
  151. }
粤ICP备19079148号