LUTCubeLoader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ( function () {
  2. // https://wwwimages2.adobe.com/content/dam/acom/en/products/speedgrade/cc/pdfs/cube-lut-specification-1.0.pdf
  3. class LUTCubeLoader 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 comments
  23. str = str.replace( /^#.*?(\n|\r)/gm, '' ).replace( /^\s*?(\n|\r)/gm, '' ).trim();
  24. let title = null;
  25. let size = null;
  26. const domainMin = new THREE.Vector3( 0, 0, 0 );
  27. const domainMax = new THREE.Vector3( 1, 1, 1 );
  28. const lines = str.split( /[\n\r]+/g );
  29. let data = null;
  30. let currIndex = 0;
  31. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  32. const line = lines[ i ].trim();
  33. const split = line.split( /\s/g );
  34. switch ( split[ 0 ] ) {
  35. case 'TITLE':
  36. title = line.substring( 7, line.length - 1 );
  37. break;
  38. case 'LUT_3D_SIZE':
  39. // TODO: A .CUBE LUT file specifies floating point values and could be represented with
  40. // more precision than can be captured with Uint8Array.
  41. const sizeToken = split[ 1 ];
  42. size = parseFloat( sizeToken );
  43. data = new Uint8Array( size * size * size * 3 );
  44. break;
  45. case 'DOMAIN_MIN':
  46. domainMin.x = parseFloat( split[ 1 ] );
  47. domainMin.y = parseFloat( split[ 2 ] );
  48. domainMin.z = parseFloat( split[ 3 ] );
  49. break;
  50. case 'DOMAIN_MAX':
  51. domainMax.x = parseFloat( split[ 1 ] );
  52. domainMax.y = parseFloat( split[ 2 ] );
  53. domainMax.z = parseFloat( split[ 3 ] );
  54. break;
  55. default:
  56. const r = parseFloat( split[ 0 ] );
  57. const g = parseFloat( split[ 1 ] );
  58. const b = parseFloat( split[ 2 ] );
  59. if ( r > 1.0 || r < 0.0 || g > 1.0 || g < 0.0 || b > 1.0 || b < 0.0 ) {
  60. throw new Error( 'LUTCubeLoader : Non normalized values not supported.' );
  61. }
  62. data[ currIndex + 0 ] = r * 255;
  63. data[ currIndex + 1 ] = g * 255;
  64. data[ currIndex + 2 ] = b * 255;
  65. currIndex += 3;
  66. }
  67. }
  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.RGBFormat;
  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. const texture3D = new THREE.DataTexture3D();
  80. texture3D.image.data = data;
  81. texture3D.image.width = size;
  82. texture3D.image.height = size;
  83. texture3D.image.depth = size;
  84. texture3D.format = THREE.RGBFormat;
  85. texture3D.type = THREE.UnsignedByteType;
  86. texture3D.magFilter = THREE.LinearFilter;
  87. texture3D.minFilter = THREE.LinearFilter;
  88. texture3D.wrapS = THREE.ClampToEdgeWrapping;
  89. texture3D.wrapT = THREE.ClampToEdgeWrapping;
  90. texture3D.wrapR = THREE.ClampToEdgeWrapping;
  91. texture3D.generateMipmaps = false;
  92. return {
  93. title,
  94. size,
  95. domainMin,
  96. domainMax,
  97. texture,
  98. texture3D
  99. };
  100. }
  101. }
  102. THREE.LUTCubeLoader = LUTCubeLoader;
  103. } )();