webgpu_postprocessing_3dlut.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 3d luts</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 3D LUTs<br />
  12. Battle Damaged Sci-fi Helmet by
  13. <a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
  14. <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> from <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a><br />
  15. LUTs from <a href="https://www.rocketstock.com/free-after-effects-templates/35-free-luts-for-color-grading-videos/">RocketStock</a>, <a href="https://www.freepresets.com/product/free-luts-cinematic/">FreePresets.com</a>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.webgpu.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { pass, texture3D, uniform, renderOutput } from 'three/tsl';
  29. import { lut3D } from 'three/addons/tsl/display/Lut3DNode.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  32. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  33. import { LUTCubeLoader } from 'three/addons/loaders/LUTCubeLoader.js';
  34. import { LUT3dlLoader } from 'three/addons/loaders/LUT3dlLoader.js';
  35. import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. const params = {
  38. lut: 'Bourbon 64.CUBE',
  39. intensity: 1
  40. };
  41. const lutMap = {
  42. 'Bourbon 64.CUBE': null,
  43. 'Chemical 168.CUBE': null,
  44. 'Clayton 33.CUBE': null,
  45. 'Cubicle 99.CUBE': null,
  46. 'Remy 24.CUBE': null,
  47. 'Presetpro-Cinematic.3dl': null,
  48. 'NeutralLUT': null,
  49. 'B&WLUT': null,
  50. 'NightLUT': null
  51. };
  52. let gui;
  53. let camera, scene, renderer;
  54. let postProcessing, lutPass;
  55. init();
  56. async function init() {
  57. const container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  60. camera.position.set( - 1.8, 0.6, 2.7 );
  61. scene = new THREE.Scene();
  62. new RGBELoader()
  63. .setPath( 'textures/equirectangular/' )
  64. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  65. texture.mapping = THREE.EquirectangularReflectionMapping;
  66. scene.background = texture;
  67. scene.environment = texture;
  68. // model
  69. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  70. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  71. scene.add( gltf.scene );
  72. } );
  73. } );
  74. const lutCubeLoader = new LUTCubeLoader();
  75. const lutImageLoader = new LUTImageLoader();
  76. const lut3dlLoader = new LUT3dlLoader();
  77. for ( const name in lutMap ) {
  78. if ( /\.CUBE$/i.test( name ) ) {
  79. lutMap[ name ] = lutCubeLoader.loadAsync( 'luts/' + name );
  80. } else if ( /\LUT$/i.test( name ) ) {
  81. lutMap[ name ] = lutImageLoader.loadAsync( `luts/${name}.png` );
  82. } else {
  83. lutMap[ name ] = lut3dlLoader.loadAsync( 'luts/' + name );
  84. }
  85. }
  86. const pendings = Object.values( lutMap );
  87. await Promise.all( pendings );
  88. for ( const name in lutMap ) {
  89. lutMap[ name ] = await lutMap[ name ];
  90. }
  91. renderer = new THREE.WebGPURenderer();
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setAnimationLoop( animate );
  95. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  96. container.appendChild( renderer.domElement );
  97. // post processing
  98. postProcessing = new THREE.PostProcessing( renderer );
  99. // ignore default output color transform ( toneMapping and outputColorSpace )
  100. // use renderOutput() for control the sequence
  101. postProcessing.outputColorTransform = false;
  102. // scene pass
  103. const scenePass = pass( scene, camera );
  104. const outputPass = renderOutput( scenePass );
  105. const lut = lutMap[ params.lut ];
  106. lutPass = lut3D( outputPass, texture3D( lut.texture3D ), lut.texture3D.image.width, uniform( 1 ) );
  107. postProcessing.outputNode = lutPass;
  108. //
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.minDistance = 2;
  111. controls.maxDistance = 10;
  112. controls.target.set( 0, 0, - 0.2 );
  113. controls.update();
  114. gui = new GUI();
  115. gui.add( params, 'lut', Object.keys( lutMap ) );
  116. gui.add( params, 'intensity' ).min( 0 ).max( 1 );
  117. window.addEventListener( 'resize', onWindowResize );
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. //
  125. function animate() {
  126. lutPass.intensityNode.value = params.intensity;
  127. if ( lutMap[ params.lut ] ) {
  128. const lut = lutMap[ params.lut ];
  129. lutPass.lutNode.value = lut.texture3D;
  130. lutPass.size.value = lut.texture3D.image.width;
  131. }
  132. postProcessing.render();
  133. }
  134. </script>
  135. </body>
  136. </html>