1
0

LUT3dlLoader.js 3.7 KB

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