LUT3dlLoader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 * 4 );
  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[ 4 * pixelIndex + 0 ] = r;
  48. dataArray[ 4 * pixelIndex + 1 ] = g;
  49. dataArray[ 4 * pixelIndex + 2 ] = b;
  50. dataArray[ 4 * pixelIndex + 3 ] = 1.0;
  51. index += 1;
  52. } // Find the apparent bit depth of the stored RGB values and map the
  53. // values to [ 0, 255 ].
  54. const bits = Math.ceil( Math.log2( maxOutputValue ) );
  55. const maxBitValue = Math.pow( 2.0, bits );
  56. for ( let i = 0, l = dataArray.length; i < l; i += 4 ) {
  57. const r = dataArray[ i + 0 ];
  58. const g = dataArray[ i + 1 ];
  59. const b = dataArray[ i + 2 ];
  60. dataArray[ i + 0 ] = 255 * r / maxBitValue; // r
  61. dataArray[ i + 1 ] = 255 * g / maxBitValue; // g
  62. dataArray[ i + 2 ] = 255 * b / maxBitValue; // b
  63. }
  64. const data = new Uint8Array( dataArray );
  65. const texture = new THREE.DataTexture();
  66. texture.image.data = data;
  67. texture.image.width = size;
  68. texture.image.height = size * size;
  69. texture.format = THREE.RGBAFormat;
  70. texture.type = THREE.UnsignedByteType;
  71. texture.magFilter = THREE.LinearFilter;
  72. texture.minFilter = THREE.LinearFilter;
  73. texture.wrapS = THREE.ClampToEdgeWrapping;
  74. texture.wrapT = THREE.ClampToEdgeWrapping;
  75. texture.generateMipmaps = false;
  76. texture.needsUpdate = true;
  77. const texture3D = new THREE.Data3DTexture();
  78. texture3D.image.data = data;
  79. texture3D.image.width = size;
  80. texture3D.image.height = size;
  81. texture3D.image.depth = size;
  82. texture3D.format = THREE.RGBAFormat;
  83. texture3D.type = THREE.UnsignedByteType;
  84. texture3D.magFilter = THREE.LinearFilter;
  85. texture3D.minFilter = THREE.LinearFilter;
  86. texture3D.wrapS = THREE.ClampToEdgeWrapping;
  87. texture3D.wrapT = THREE.ClampToEdgeWrapping;
  88. texture3D.wrapR = THREE.ClampToEdgeWrapping;
  89. texture3D.generateMipmaps = false;
  90. texture3D.needsUpdate = true;
  91. return {
  92. size,
  93. texture,
  94. texture3D
  95. };
  96. }
  97. }
  98. THREE.LUT3dlLoader = LUT3dlLoader;
  99. } )();