LUTCubeLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. ClampToEdgeWrapping,
  3. Data3DTexture,
  4. FileLoader,
  5. LinearFilter,
  6. Loader,
  7. UnsignedByteType,
  8. Vector3,
  9. } from 'three';
  10. /**
  11. * A loader for the Cube LUT format.
  12. *
  13. * References:
  14. * - [Cube LUT Specification]{@link https://web.archive.org/web/20220220033515/https://wwwimages2.adobe.com/content/dam/acom/en/products/speedgrade/cc/pdfs/cube-lut-specification-1.0.pdf}
  15. *
  16. * ```js
  17. * const loader = new LUTCubeLoader();
  18. * const map = loader.loadAsync( 'luts/Bourbon 64.CUBE' );
  19. * ```
  20. *
  21. * @augments Loader
  22. */
  23. export class LUTCubeLoader extends Loader {
  24. /**
  25. * Constructs a new Cube LUT loader.
  26. *
  27. * @param {LoadingManager} [manager] - The loading manager.
  28. */
  29. constructor( manager ) {
  30. super( manager );
  31. /**
  32. * The texture type.
  33. *
  34. * @type {(UnsignedByteType|FloatType)}
  35. * @default UnsignedByteType
  36. */
  37. this.type = UnsignedByteType;
  38. }
  39. /**
  40. * Sets the texture type.
  41. *
  42. * @param {(UnsignedByteType|FloatType)} type - The texture type to set.
  43. * @return {LUTCubeLoader} A reference to this loader.
  44. */
  45. setType( type ) {
  46. this.type = type;
  47. return this;
  48. }
  49. /**
  50. * Starts loading from the given URL and passes the loaded Cube LUT asset
  51. * to the `onLoad()` callback.
  52. *
  53. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  54. * @param {function({title:string,size:number,domainMin:Vector3,domainMax:Vector3,texture3D:Data3DTexture})} onLoad - Executed when the loading process has been finished.
  55. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  56. * @param {onErrorCallback} onError - Executed when errors occur.
  57. */
  58. load( url, onLoad, onProgress, onError ) {
  59. const loader = new FileLoader( this.manager );
  60. loader.setPath( this.path );
  61. loader.setResponseType( 'text' );
  62. loader.load( url, text => {
  63. try {
  64. onLoad( this.parse( text ) );
  65. } catch ( e ) {
  66. if ( onError ) {
  67. onError( e );
  68. } else {
  69. console.error( e );
  70. }
  71. this.manager.itemError( url );
  72. }
  73. }, onProgress, onError );
  74. }
  75. /**
  76. * Parses the given Cube LUT data and returns the resulting 3D data texture.
  77. *
  78. * @param {string} input - The raw Cube LUT data as a string.
  79. * @return {{title:string,size:number,domainMin:Vector3,domainMax:Vector3,texture3D:Data3DTexture}} The parsed Cube LUT.
  80. */
  81. parse( input ) {
  82. const regExpTitle = /TITLE +"([^"]*)"/;
  83. const regExpSize = /LUT_3D_SIZE +(\d+)/;
  84. const regExpDomainMin = /DOMAIN_MIN +([\d.]+) +([\d.]+) +([\d.]+)/;
  85. const regExpDomainMax = /DOMAIN_MAX +([\d.]+) +([\d.]+) +([\d.]+)/;
  86. const regExpDataPoints = /^([\d.e+-]+) +([\d.e+-]+) +([\d.e+-]+) *$/gm;
  87. let result = regExpTitle.exec( input );
  88. const title = ( result !== null ) ? result[ 1 ] : null;
  89. result = regExpSize.exec( input );
  90. if ( result === null ) {
  91. throw new Error( 'LUTCubeLoader: Missing LUT_3D_SIZE information' );
  92. }
  93. const size = Number( result[ 1 ] );
  94. const length = size ** 3 * 4;
  95. const data = this.type === UnsignedByteType ? new Uint8Array( length ) : new Float32Array( length );
  96. const domainMin = new Vector3( 0, 0, 0 );
  97. const domainMax = new Vector3( 1, 1, 1 );
  98. result = regExpDomainMin.exec( input );
  99. if ( result !== null ) {
  100. domainMin.set( Number( result[ 1 ] ), Number( result[ 2 ] ), Number( result[ 3 ] ) );
  101. }
  102. result = regExpDomainMax.exec( input );
  103. if ( result !== null ) {
  104. domainMax.set( Number( result[ 1 ] ), Number( result[ 2 ] ), Number( result[ 3 ] ) );
  105. }
  106. if ( domainMin.x > domainMax.x || domainMin.y > domainMax.y || domainMin.z > domainMax.z ) {
  107. throw new Error( 'LUTCubeLoader: Invalid input domain' );
  108. }
  109. const scale = this.type === UnsignedByteType ? 255 : 1;
  110. let i = 0;
  111. while ( ( result = regExpDataPoints.exec( input ) ) !== null ) {
  112. data[ i ++ ] = Number( result[ 1 ] ) * scale;
  113. data[ i ++ ] = Number( result[ 2 ] ) * scale;
  114. data[ i ++ ] = Number( result[ 3 ] ) * scale;
  115. data[ i ++ ] = scale;
  116. }
  117. const texture3D = new Data3DTexture();
  118. texture3D.image.data = data;
  119. texture3D.image.width = size;
  120. texture3D.image.height = size;
  121. texture3D.image.depth = size;
  122. texture3D.type = this.type;
  123. texture3D.magFilter = LinearFilter;
  124. texture3D.minFilter = LinearFilter;
  125. texture3D.wrapS = ClampToEdgeWrapping;
  126. texture3D.wrapT = ClampToEdgeWrapping;
  127. texture3D.wrapR = ClampToEdgeWrapping;
  128. texture3D.generateMipmaps = false;
  129. texture3D.needsUpdate = true;
  130. return {
  131. title,
  132. size,
  133. domainMin,
  134. domainMax,
  135. texture3D,
  136. };
  137. }
  138. }
粤ICP备19079148号