LUTCubeLoader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // https://wwwimages2.adobe.com/content/dam/acom/en/products/speedgrade/cc/pdfs/cube-lut-specification-1.0.pdf
  2. import {
  3. Loader,
  4. FileLoader,
  5. Vector3,
  6. DataTexture,
  7. DataTexture3D,
  8. RGBFormat,
  9. UnsignedByteType,
  10. ClampToEdgeWrapping,
  11. LinearFilter,
  12. } from '../../../build/three.module.js';
  13. export class LUTCubeLoader 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 comments
  33. str = str
  34. .replace( /^#.*?(\n|\r)/gm, '' )
  35. .replace( /^\s*?(\n|\r)/gm, '' )
  36. .trim();
  37. let title = null;
  38. let size = null;
  39. const domainMin = new Vector3( 0, 0, 0 );
  40. const domainMax = new Vector3( 1, 1, 1 );
  41. const lines = str.split( /[\n\r]+/g );
  42. let data = null;
  43. let currIndex = 0;
  44. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  45. const line = lines[ i ].trim();
  46. const split = line.split( /\s/g );
  47. switch ( split[ 0 ] ) {
  48. case 'TITLE':
  49. title = line.substring( 7, line.length - 1 );
  50. break;
  51. case 'LUT_3D_SIZE':
  52. // TODO: A .CUBE LUT file specifies floating point values and could be represented with
  53. // more precision than can be captured with Uint8Array.
  54. const sizeToken = split[ 1 ];
  55. size = parseFloat( sizeToken );
  56. data = new Uint8Array( size * size * size * 3 );
  57. break;
  58. case 'DOMAIN_MIN':
  59. domainMin.x = parseFloat( split[ 1 ] );
  60. domainMin.y = parseFloat( split[ 2 ] );
  61. domainMin.z = parseFloat( split[ 3 ] );
  62. break;
  63. case 'DOMAIN_MAX':
  64. domainMax.x = parseFloat( split[ 1 ] );
  65. domainMax.y = parseFloat( split[ 2 ] );
  66. domainMax.z = parseFloat( split[ 3 ] );
  67. break;
  68. default:
  69. const r = parseFloat( split[ 0 ] );
  70. const g = parseFloat( split[ 1 ] );
  71. const b = parseFloat( split[ 2 ] );
  72. if (
  73. r > 1.0 || r < 0.0 ||
  74. g > 1.0 || g < 0.0 ||
  75. b > 1.0 || b < 0.0
  76. ) {
  77. throw new Error( 'LUTCubeLoader : Non normalized values not supported.' );
  78. }
  79. data[ currIndex + 0 ] = r * 255;
  80. data[ currIndex + 1 ] = g * 255;
  81. data[ currIndex + 2 ] = b * 255;
  82. currIndex += 3;
  83. }
  84. }
  85. const texture = new DataTexture();
  86. texture.image.data = data;
  87. texture.image.width = size;
  88. texture.image.height = size * size;
  89. texture.format = RGBFormat;
  90. texture.type = UnsignedByteType;
  91. texture.magFilter = LinearFilter;
  92. texture.minFilter = LinearFilter;
  93. texture.wrapS = ClampToEdgeWrapping;
  94. texture.wrapT = ClampToEdgeWrapping;
  95. texture.generateMipmaps = false;
  96. const texture3D = new DataTexture3D();
  97. texture3D.image.data = data;
  98. texture3D.image.width = size;
  99. texture3D.image.height = size;
  100. texture3D.image.depth = size;
  101. texture3D.format = RGBFormat;
  102. texture3D.type = UnsignedByteType;
  103. texture3D.magFilter = LinearFilter;
  104. texture3D.minFilter = LinearFilter;
  105. texture3D.wrapS = ClampToEdgeWrapping;
  106. texture3D.wrapT = ClampToEdgeWrapping;
  107. texture3D.wrapR = ClampToEdgeWrapping;
  108. texture3D.generateMipmaps = false;
  109. return {
  110. title,
  111. size,
  112. domainMin,
  113. domainMax,
  114. texture,
  115. texture3D,
  116. };
  117. }
  118. }