LUT3dlLoader.js 3.5 KB

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