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

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