LUT3dlLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // http://download.autodesk.com/us/systemdocs/help/2011/lustre/index.html?url=./files/WSc4e151a45a3b785a24c3d9a411df9298473-7ffd.htm,topicNumber=d0e9492
  2. import {
  3. Loader,
  4. FileLoader,
  5. DataTexture,
  6. DataTexture3D,
  7. RGBFormat,
  8. UnsignedByteType,
  9. ClampToEdgeWrapping,
  10. LinearFilter,
  11. } from '../../../build/three.module.js';
  12. export class LUT3dlLoader extends Loader {
  13. load( url, onLoad, onProgress, onError ) {
  14. const loader = new FileLoader( this.manager );
  15. loader.setPath( this.path );
  16. loader.setResponseType( 'text' );
  17. loader.load( url, text => {
  18. try {
  19. onLoad( this.parse( text ) );
  20. } catch ( e ) {
  21. if ( onError ) {
  22. onError( e );
  23. } else {
  24. console.error( e );
  25. }
  26. this.manager.itemError( url );
  27. }
  28. }, onProgress, onError );
  29. }
  30. parse( str ) {
  31. // remove empty lines and comment lints
  32. str = str
  33. .replace( /^#.*?(\n|\r)/gm, '' )
  34. .replace( /^\s*?(\n|\r)/gm, '' )
  35. .trim();
  36. const lines = str.split( /[\n\r]+/g );
  37. // first line is the positions on the grid that are provided by the LUT
  38. const gridLines = lines[ 0 ].trim().split( /\s+/g ).map( e => parseFloat( e ) );
  39. const gridStep = gridLines[ 1 ] - gridLines[ 0 ];
  40. const size = gridLines.length;
  41. for ( let i = 1, l = gridLines.length; i < l; i ++ ) {
  42. if ( gridStep !== ( gridLines[ i ] - gridLines[ i - 1 ] ) ) {
  43. throw new Error( 'LUT3dlLoader: Inconsistent grid size not supported.' );
  44. }
  45. }
  46. const dataArray = new Array( size * size * size * 3 );
  47. let index = 0;
  48. let maxOutputValue = 0.0;
  49. for ( let i = 1, l = lines.length; i < l; i ++ ) {
  50. const line = lines[ i ].trim();
  51. const split = line.split( /\s/g );
  52. const r = parseFloat( split[ 0 ] );
  53. const g = parseFloat( split[ 1 ] );
  54. const b = parseFloat( split[ 2 ] );
  55. maxOutputValue = Math.max( maxOutputValue, r, g, b );
  56. const bLayer = index % size;
  57. const gLayer = Math.floor( index / size ) % size;
  58. const rLayer = Math.floor( index / ( size * size ) ) % size;
  59. // b grows first, then g, then r
  60. const pixelIndex = bLayer * size * size + gLayer * size + rLayer;
  61. dataArray[ 3 * pixelIndex + 0 ] = r;
  62. dataArray[ 3 * pixelIndex + 1 ] = g;
  63. dataArray[ 3 * pixelIndex + 2 ] = b;
  64. index += 1;
  65. }
  66. // Find the apparent bit depth of the stored RGB values and scale the
  67. // values to [ 0, 255 ].
  68. const bits = Math.ceil( Math.log2( maxOutputValue ) );
  69. const maxBitValue = Math.pow( 2.0, bits );
  70. for ( let i = 0, l = dataArray.length; i < l; i ++ ) {
  71. const val = dataArray[ i ];
  72. dataArray[ i ] = 255 * val / maxBitValue;
  73. }
  74. const data = new Uint8Array( dataArray );
  75. const texture = new DataTexture();
  76. texture.image.data = data;
  77. texture.image.width = size;
  78. texture.image.height = size * size;
  79. texture.format = RGBFormat;
  80. texture.type = UnsignedByteType;
  81. texture.magFilter = LinearFilter;
  82. texture.minFilter = LinearFilter;
  83. texture.wrapS = ClampToEdgeWrapping;
  84. texture.wrapT = ClampToEdgeWrapping;
  85. texture.generateMipmaps = false;
  86. const texture3D = new DataTexture3D();
  87. texture3D.image.data = data;
  88. texture3D.image.width = size;
  89. texture3D.image.height = size;
  90. texture3D.image.depth = size;
  91. texture3D.format = RGBFormat;
  92. texture3D.type = UnsignedByteType;
  93. texture3D.magFilter = LinearFilter;
  94. texture3D.minFilter = LinearFilter;
  95. texture3D.wrapS = ClampToEdgeWrapping;
  96. texture3D.wrapT = ClampToEdgeWrapping;
  97. texture3D.wrapR = ClampToEdgeWrapping;
  98. texture3D.generateMipmaps = false;
  99. return {
  100. size,
  101. texture,
  102. texture3D,
  103. };
  104. }
  105. }