LUT3dlLoader.js 3.8 KB

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