2
0

LUT3dlLoader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // http://download.autodesk.com/us/systemdocs/help/2011/lustre/index.html?url=./files/WSc4e151a45a3b785a24c3d9a411df9298473-7ffd.htm,topicNumber=d0e9492
  2. // https://community.foundry.com/discuss/topic/103636/format-spec-for-3dl?mode=Post&postID=895258
  3. import {
  4. ClampToEdgeWrapping,
  5. DataTexture,
  6. Data3DTexture,
  7. FileLoader,
  8. FloatType,
  9. LinearFilter,
  10. Loader,
  11. RGBAFormat,
  12. UnsignedByteType,
  13. } from 'three';
  14. export class LUT3dlLoader extends Loader {
  15. constructor( manager ) {
  16. super( manager );
  17. this.type = UnsignedByteType;
  18. }
  19. setType( type ) {
  20. if ( type !== UnsignedByteType && type !== FloatType ) {
  21. throw new Error( 'LUT3dlLoader: Unsupported type' );
  22. }
  23. this.type = type;
  24. return this;
  25. }
  26. load( url, onLoad, onProgress, onError ) {
  27. const loader = new FileLoader( this.manager );
  28. loader.setPath( this.path );
  29. loader.setResponseType( 'text' );
  30. loader.load( url, text => {
  31. try {
  32. onLoad( this.parse( text ) );
  33. } catch ( e ) {
  34. if ( onError ) {
  35. onError( e );
  36. } else {
  37. console.error( e );
  38. }
  39. this.manager.itemError( url );
  40. }
  41. }, onProgress, onError );
  42. }
  43. parse( input ) {
  44. const regExpGridInfo = /^[\d ]+$/m;
  45. const regExpDataPoints = /^([\d.e+-]+) +([\d.e+-]+) +([\d.e+-]+) *$/gm;
  46. // The first line describes the positions of values on the LUT grid.
  47. let result = regExpGridInfo.exec( input );
  48. if ( result === null ) {
  49. throw new Error( 'LUT3dlLoader: Missing grid information' );
  50. }
  51. const gridLines = result[ 0 ].trim().split( /\s+/g ).map( Number );
  52. const gridStep = gridLines[ 1 ] - gridLines[ 0 ];
  53. const size = gridLines.length;
  54. const sizeSq = size ** 2;
  55. for ( let i = 1, l = gridLines.length; i < l; ++ i ) {
  56. if ( gridStep !== ( gridLines[ i ] - gridLines[ i - 1 ] ) ) {
  57. throw new Error( 'LUT3dlLoader: Inconsistent grid size' );
  58. }
  59. }
  60. const dataFloat = new Float32Array( size ** 3 * 4 );
  61. let maxValue = 0.0;
  62. let index = 0;
  63. while ( ( result = regExpDataPoints.exec( input ) ) !== null ) {
  64. const r = Number( result[ 1 ] );
  65. const g = Number( result[ 2 ] );
  66. const b = Number( result[ 3 ] );
  67. maxValue = Math.max( maxValue, r, g, b );
  68. const bLayer = index % size;
  69. const gLayer = Math.floor( index / size ) % size;
  70. const rLayer = Math.floor( index / ( sizeSq ) ) % size;
  71. // b grows first, then g, then r.
  72. const d4 = ( bLayer * sizeSq + gLayer * size + rLayer ) * 4;
  73. dataFloat[ d4 + 0 ] = r;
  74. dataFloat[ d4 + 1 ] = g;
  75. dataFloat[ d4 + 2 ] = b;
  76. ++ index;
  77. }
  78. // Determine the bit depth to scale the values to [0.0, 1.0].
  79. const bits = Math.ceil( Math.log2( maxValue ) );
  80. const maxBitValue = Math.pow( 2, bits );
  81. const data = this.type === UnsignedByteType ? new Uint8Array( dataFloat.length ) : dataFloat;
  82. const scale = this.type === UnsignedByteType ? 255 : 1;
  83. for ( let i = 0, l = data.length; i < l; i += 4 ) {
  84. const i1 = i + 1;
  85. const i2 = i + 2;
  86. const i3 = i + 3;
  87. // Note: data is dataFloat when type is FloatType.
  88. data[ i ] = dataFloat[ i ] / maxBitValue * scale;
  89. data[ i1 ] = dataFloat[ i1 ] / maxBitValue * scale;
  90. data[ i2 ] = dataFloat[ i2 ] / maxBitValue * scale;
  91. data[ i3 ] = scale;
  92. }
  93. const texture = new DataTexture();
  94. texture.image.data = data;
  95. texture.image.width = size;
  96. texture.image.height = size * size;
  97. texture.format = RGBAFormat;
  98. texture.type = this.type;
  99. texture.magFilter = LinearFilter;
  100. texture.minFilter = LinearFilter;
  101. texture.wrapS = ClampToEdgeWrapping;
  102. texture.wrapT = ClampToEdgeWrapping;
  103. texture.generateMipmaps = false;
  104. texture.needsUpdate = true;
  105. const texture3D = new Data3DTexture();
  106. texture3D.image.data = data;
  107. texture3D.image.width = size;
  108. texture3D.image.height = size;
  109. texture3D.image.depth = size;
  110. texture3D.format = RGBAFormat;
  111. texture3D.type = this.type;
  112. texture3D.magFilter = LinearFilter;
  113. texture3D.minFilter = LinearFilter;
  114. texture3D.wrapS = ClampToEdgeWrapping;
  115. texture3D.wrapT = ClampToEdgeWrapping;
  116. texture3D.wrapR = ClampToEdgeWrapping;
  117. texture3D.generateMipmaps = false;
  118. texture3D.needsUpdate = true;
  119. return {
  120. size,
  121. texture,
  122. texture3D,
  123. };
  124. }
  125. }