postprocessing-adobe-lut-to-png-converter.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Three.js - postprocessing - 3DLUT w/loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. html, body {
  9. min-height: 100%;
  10. font-family: monospace;
  11. background: #222;
  12. color: white;
  13. }
  14. canvas {
  15. min-width: 512px;
  16. min-height: 64px;
  17. image-rendering: pixelated;
  18. }
  19. #cube {
  20. max-width: calc(100% - 20px);
  21. overflow: auto;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>Adobe LUT to PNG converter</h1>
  27. <p>Drag and drop a LUT file here</p>
  28. <div>size:<input id="size" type="number" value="8" min="2" max="64"/></div>
  29. <p id="result"></p>
  30. <p><button type="button">Save...</button></p>
  31. <div id="cube"><canvas id="c"></canvas></div>
  32. </body>
  33. <script type="module">
  34. import * as lutParser from './resources/lut-reader.js';
  35. import * as dragAndDrop from './resources/drag-and-drop.js';
  36. import * as THREE from '../../build/three.module.js';
  37. import {EffectComposer} from '../../examples/jsm/postprocessing/EffectComposer.js';
  38. import {RenderPass} from '../../examples/jsm/postprocessing/RenderPass.js';
  39. import {ShaderPass} from '../../examples/jsm/postprocessing/ShaderPass.js';
  40. function main() {
  41. const canvas = document.querySelector('#c');
  42. const renderer = new THREE.WebGLRenderer({canvas});
  43. const makeIdentityLutTexture = function() {
  44. const identityLUT = new Uint8Array([
  45. 0, 0, 0, 255, // black
  46. 255, 0, 0, 255, // red
  47. 0, 0, 255, 255, // blue
  48. 255, 0, 255, 255, // magenta
  49. 0, 255, 0, 255, // green
  50. 255, 255, 0, 255, // yellow
  51. 0, 255, 255, 255, // cyan
  52. 255, 255, 255, 255, // white
  53. ]);
  54. return function(filter) {
  55. const texture = new THREE.DataTexture(identityLUT, 4, 2, THREE.RGBAFormat);
  56. texture.minFilter = filter;
  57. texture.magFilter = filter;
  58. texture.needsUpdate = true;
  59. texture.flipY = false;
  60. return texture;
  61. };
  62. }();
  63. const sceneBG = new THREE.Scene();
  64. const cameraBG = new THREE.OrthographicCamera(-1, 1, 1, -1, -1, 1);
  65. const ctx = document.createElement('canvas').getContext('2d');
  66. function drawColorCubeImage(ctx, size) {
  67. const canvas = ctx.canvas;
  68. canvas.width = size * size;
  69. canvas.height = size;
  70. for (let zz = 0; zz < size; ++zz) {
  71. for (let yy = 0; yy < size; ++yy) {
  72. for (let xx = 0; xx < size; ++xx) {
  73. const r = Math.floor(xx / (size - 1) * 255);
  74. const g = Math.floor(yy / (size - 1) * 255);
  75. const b = Math.floor(zz / (size - 1) * 255);
  76. ctx.fillStyle = `rgb(${r},${g},${b})`;
  77. ctx.fillRect(zz * size + xx, yy, 1, 1);
  78. }
  79. }
  80. }
  81. }
  82. const idTexture = new THREE.CanvasTexture(ctx.canvas);
  83. idTexture.magFilter = THREE.NearestFilter;
  84. idTexture.minFilter = THREE.NearestFilter;
  85. {
  86. const planeGeo = new THREE.PlaneGeometry(2, 2);
  87. const planeMat = new THREE.MeshBasicMaterial({
  88. map: idTexture,
  89. depthTest: false,
  90. });
  91. sceneBG.add(new THREE.Mesh(planeGeo, planeMat));
  92. }
  93. const lutShader = {
  94. uniforms: {
  95. tDiffuse: { value: null },
  96. lutMap: { value: null },
  97. lutMapSize: { value: 1, },
  98. },
  99. vertexShader: `
  100. varying vec2 vUv;
  101. void main() {
  102. vUv = uv;
  103. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  104. }
  105. `,
  106. fragmentShader: `
  107. #include <common>
  108. #define FILTER_LUT true
  109. uniform sampler2D tDiffuse;
  110. uniform sampler2D lutMap;
  111. uniform float lutMapSize;
  112. varying vec2 vUv;
  113. vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size) {
  114. float sliceSize = 1.0 / size; // space of 1 slice
  115. float slicePixelSize = sliceSize / size; // space of 1 pixel
  116. float width = size - 1.0;
  117. float sliceInnerSize = slicePixelSize * width; // space of size pixels
  118. float zSlice0 = floor( texCoord.z * width);
  119. float zSlice1 = min( zSlice0 + 1.0, width);
  120. float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
  121. float yRange = (texCoord.y * width + 0.5) / size;
  122. float s0 = xOffset + (zSlice0 * sliceSize);
  123. #ifdef FILTER_LUT
  124. float s1 = xOffset + (zSlice1 * sliceSize);
  125. vec4 slice0Color = texture2D(tex, vec2(s0, yRange));
  126. vec4 slice1Color = texture2D(tex, vec2(s1, yRange));
  127. float zOffset = mod(texCoord.z * width, 1.0);
  128. return mix(slice0Color, slice1Color, zOffset);
  129. #else
  130. return texture2D(tex, vec2( s0, yRange));
  131. #endif
  132. }
  133. void main() {
  134. vec4 originalColor = texture2D(tDiffuse, vUv);
  135. gl_FragColor = sampleAs3DTexture(lutMap, originalColor.xyz, lutMapSize);
  136. }
  137. `,
  138. };
  139. const effectLUT = new ShaderPass(lutShader);
  140. effectLUT.renderToScreen = true;
  141. const renderBG = new RenderPass(sceneBG, cameraBG);
  142. const rtParameters = {
  143. minFilter: THREE.NearestFilter,
  144. magFilter: THREE.NearestFilter,
  145. format: THREE.RGBFormat,
  146. };
  147. const composer = new EffectComposer(renderer, new THREE.WebGLRenderTarget(1, 1, rtParameters));
  148. composer.addPass(renderBG);
  149. composer.addPass(effectLUT);
  150. let name = 'identity';
  151. const lutTexture = makeIdentityLutTexture(THREE.LinearFilter);
  152. effectLUT.uniforms.lutMap.value = lutTexture;
  153. effectLUT.uniforms.lutMapSize.value = 2;
  154. const sizeElem = document.querySelector('#size');
  155. sizeElem.addEventListener('change', render);
  156. function render() {
  157. const size = parseInt(sizeElem.value);
  158. renderer.setSize(size * size, size, false);
  159. composer.setSize(size * size, size);
  160. drawColorCubeImage(ctx, size);
  161. idTexture.needsUpdate = true;
  162. composer.render(0);
  163. }
  164. render();
  165. dragAndDrop.setup({msg: 'Drop LUT File here'});
  166. dragAndDrop.onDropFile(readLUTFile);
  167. function ext(s) {
  168. const period = s.lastIndexOf('.');
  169. return s.substr(period + 1);
  170. }
  171. function readLUTFile(file) {
  172. const reader = new FileReader();
  173. reader.onload = (e) => {
  174. const type = ext(file.name);
  175. const lut = lutParser.lutTo2D3Drgb8(lutParser.parse(e.target.result, type));
  176. effectLUT.uniforms.lutMapSize.value = lut.size;
  177. lutTexture.image.data = lut.data;
  178. lutTexture.image.width = lut.size * lut.size;
  179. lutTexture.image.height = lut.size;
  180. lutTexture.format = THREE.RGBFormat;
  181. lutTexture.needsUpdate = true;
  182. render();
  183. name = `${file.name || lut.name || 'untitled'}`;
  184. document.querySelector('#result').textContent = `loaded: ${name}`;
  185. };
  186. reader.readAsText(file);
  187. }
  188. const saveData = (function() {
  189. const a = document.createElement('a');
  190. document.body.appendChild(a);
  191. a.style.display = 'none';
  192. return function saveData(blob, fileName) {
  193. const url = window.URL.createObjectURL(blob);
  194. a.href = url;
  195. a.download = fileName;
  196. a.click();
  197. };
  198. }());
  199. document.querySelector('button').addEventListener('click', () => {
  200. render();
  201. renderer.domElement.toBlob((blob) => {
  202. saveData(blob, `${name}-s${renderer.domElement.height}.png`);
  203. });
  204. });
  205. }
  206. main();
  207. </script>
  208. </html>