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. Data3DTexture,
  8. UnsignedByteType,
  9. ClampToEdgeWrapping,
  10. LinearFilter,
  11. } from 'three';
  12. export class LUTCubeLoader extends Loader {
  13. load( url, onLoad, onProgress, onError ) {
  14. const loader = new FileLoader( this.manager );
  15. loader.setPath( this.path );
  16. loader.setResponseType( 'text' );
  17. loader.load( url, text => {
  18. try {
  19. onLoad( this.parse( text ) );
  20. } catch ( e ) {
  21. if ( onError ) {
  22. onError( e );
  23. } else {
  24. console.error( e );
  25. }
  26. this.manager.itemError( url );
  27. }
  28. }, onProgress, onError );
  29. }
  30. parse( str ) {
  31. // Remove empty lines and comments
  32. str = str
  33. .replace( /^#.*?(\n|\r)/gm, '' )
  34. .replace( /^\s*?(\n|\r)/gm, '' )
  35. .trim();
  36. let title = null;
  37. let size = null;
  38. const domainMin = new Vector3( 0, 0, 0 );
  39. const domainMax = new Vector3( 1, 1, 1 );
  40. const lines = str.split( /[\n\r]+/g );
  41. let data = null;
  42. let currIndex = 0;
  43. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  44. const line = lines[ i ].trim();
  45. const split = line.split( /\s/g );
  46. switch ( split[ 0 ] ) {
  47. case 'TITLE':
  48. title = line.substring( 7, line.length - 1 );
  49. break;
  50. case 'LUT_3D_SIZE':
  51. // TODO: A .CUBE LUT file specifies floating point values and could be represented with
  52. // more precision than can be captured with Uint8Array.
  53. const sizeToken = split[ 1 ];
  54. size = parseFloat( sizeToken );
  55. data = new Uint8Array( size * size * size * 4 );
  56. break;
  57. case 'DOMAIN_MIN':
  58. domainMin.x = parseFloat( split[ 1 ] );
  59. domainMin.y = parseFloat( split[ 2 ] );
  60. domainMin.z = parseFloat( split[ 3 ] );
  61. break;
  62. case 'DOMAIN_MAX':
  63. domainMax.x = parseFloat( split[ 1 ] );
  64. domainMax.y = parseFloat( split[ 2 ] );
  65. domainMax.z = parseFloat( split[ 3 ] );
  66. break;
  67. default:
  68. const r = parseFloat( split[ 0 ] );
  69. const g = parseFloat( split[ 1 ] );
  70. const b = parseFloat( split[ 2 ] );
  71. if (
  72. r > 1.0 || r < 0.0 ||
  73. g > 1.0 || g < 0.0 ||
  74. b > 1.0 || b < 0.0
  75. ) {
  76. throw new Error( 'LUTCubeLoader : Non normalized values not supported.' );
  77. }
  78. data[ currIndex + 0 ] = r * 255;
  79. data[ currIndex + 1 ] = g * 255;
  80. data[ currIndex + 2 ] = b * 255;
  81. data[ currIndex + 3 ] = 255;
  82. currIndex += 4;
  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.type = UnsignedByteType;
  90. texture.magFilter = LinearFilter;
  91. texture.minFilter = LinearFilter;
  92. texture.wrapS = ClampToEdgeWrapping;
  93. texture.wrapT = ClampToEdgeWrapping;
  94. texture.generateMipmaps = false;
  95. texture.needsUpdate = true;
  96. const texture3D = new Data3DTexture();
  97. texture3D.image.data = data;
  98. texture3D.image.width = size;
  99. texture3D.image.height = size;
  100. texture3D.image.depth = size;
  101. texture3D.type = UnsignedByteType;
  102. texture3D.magFilter = LinearFilter;
  103. texture3D.minFilter = LinearFilter;
  104. texture3D.wrapS = ClampToEdgeWrapping;
  105. texture3D.wrapT = ClampToEdgeWrapping;
  106. texture3D.wrapR = ClampToEdgeWrapping;
  107. texture3D.generateMipmaps = false;
  108. texture3D.needsUpdate = true;
  109. return {
  110. title,
  111. size,
  112. domainMin,
  113. domainMax,
  114. texture,
  115. texture3D,
  116. };
  117. }
  118. }