LUT3dlLoader.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 );
  25. // first line is the positions on the grid that are provided by the LUT
  26. const gridLines = lines[ 0 ].trim().split( /\s+/g ).map( e => parseFloat( e ) );
  27. const gridStep = gridLines[ 1 ] - gridLines[ 0 ];
  28. const size = gridLines.length;
  29. for ( let i = 1, l = gridLines.length; i < l; i ++ ) {
  30. if ( gridStep !== gridLines[ i ] - gridLines[ i - 1 ] ) {
  31. throw new Error( 'LUT3dlLoader: Inconsistent grid size not supported.' );
  32. }
  33. }
  34. const dataArray = new Array( size * size * size * 4 );
  35. let index = 0;
  36. let maxOutputValue = 0.0;
  37. for ( let i = 1, l = lines.length; i < l; i ++ ) {
  38. const line = lines[ i ].trim();
  39. const split = line.split( /\s/g );
  40. const r = parseFloat( split[ 0 ] );
  41. const g = parseFloat( split[ 1 ] );
  42. const b = parseFloat( split[ 2 ] );
  43. maxOutputValue = Math.max( maxOutputValue, r, g, b );
  44. const bLayer = index % size;
  45. const gLayer = Math.floor( index / size ) % size;
  46. const rLayer = Math.floor( index / ( size * size ) ) % size;
  47. // b grows first, then g, then r
  48. const pixelIndex = bLayer * size * size + gLayer * size + rLayer;
  49. dataArray[ 4 * pixelIndex + 0 ] = r;
  50. dataArray[ 4 * pixelIndex + 1 ] = g;
  51. dataArray[ 4 * pixelIndex + 2 ] = b;
  52. dataArray[ 4 * pixelIndex + 3 ] = 1.0;
  53. index += 1;
  54. }
  55. // Find the apparent bit depth of the stored RGB values and map the
  56. // values to [ 0, 255 ].
  57. const bits = Math.ceil( Math.log2( maxOutputValue ) );
  58. const maxBitValue = Math.pow( 2.0, bits );
  59. for ( let i = 0, l = dataArray.length; i < l; i += 4 ) {
  60. const r = dataArray[ i + 0 ];
  61. const g = dataArray[ i + 1 ];
  62. const b = dataArray[ i + 2 ];
  63. dataArray[ i + 0 ] = 255 * r / maxBitValue; // r
  64. dataArray[ i + 1 ] = 255 * g / maxBitValue; // g
  65. dataArray[ i + 2 ] = 255 * b / maxBitValue; // b
  66. }
  67. const data = new Uint8Array( dataArray );
  68. const texture = new THREE.DataTexture();
  69. texture.image.data = data;
  70. texture.image.width = size;
  71. texture.image.height = size * size;
  72. texture.format = THREE.RGBAFormat;
  73. texture.type = THREE.UnsignedByteType;
  74. texture.magFilter = THREE.LinearFilter;
  75. texture.minFilter = THREE.LinearFilter;
  76. texture.wrapS = THREE.ClampToEdgeWrapping;
  77. texture.wrapT = THREE.ClampToEdgeWrapping;
  78. texture.generateMipmaps = false;
  79. texture.needsUpdate = true;
  80. const texture3D = new THREE.Data3DTexture();
  81. texture3D.image.data = data;
  82. texture3D.image.width = size;
  83. texture3D.image.height = size;
  84. texture3D.image.depth = size;
  85. texture3D.format = THREE.RGBAFormat;
  86. texture3D.type = THREE.UnsignedByteType;
  87. texture3D.magFilter = THREE.LinearFilter;
  88. texture3D.minFilter = THREE.LinearFilter;
  89. texture3D.wrapS = THREE.ClampToEdgeWrapping;
  90. texture3D.wrapT = THREE.ClampToEdgeWrapping;
  91. texture3D.wrapR = THREE.ClampToEdgeWrapping;
  92. texture3D.generateMipmaps = false;
  93. texture3D.needsUpdate = true;
  94. return {
  95. size,
  96. texture,
  97. texture3D
  98. };
  99. }
  100. }
  101. THREE.LUT3dlLoader = LUT3dlLoader;
  102. } )();