webgpu_postprocessing_3dlut.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, lut3D, renderOutput } from 'three/tsl';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  31. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  32. import { LUTCubeLoader } from 'three/addons/loaders/LUTCubeLoader.js';
  33. import { LUT3dlLoader } from 'three/addons/loaders/LUT3dlLoader.js';
  34. import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. const params = {
  37. lut: 'Bourbon 64.CUBE',
  38. intensity: 1
  39. };
  40. const lutMap = {
  41. 'Bourbon 64.CUBE': null,
  42. 'Chemical 168.CUBE': null,
  43. 'Clayton 33.CUBE': null,
  44. 'Cubicle 99.CUBE': null,
  45. 'Remy 24.CUBE': null,
  46. 'Presetpro-Cinematic.3dl': null,
  47. 'NeutralLUT': null,
  48. 'B&WLUT': null,
  49. 'NightLUT': null
  50. };
  51. let gui;
  52. let camera, scene, renderer;
  53. let postProcessing, lutPass;
  54. init();
  55. async function init() {
  56. const container = document.createElement( 'div' );
  57. document.body.appendChild( container );
  58. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  59. camera.position.set( - 1.8, 0.6, 2.7 );
  60. scene = new THREE.Scene();
  61. new RGBELoader()
  62. .setPath( 'textures/equirectangular/' )
  63. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  64. texture.mapping = THREE.EquirectangularReflectionMapping;
  65. scene.background = texture;
  66. scene.environment = texture;
  67. // model
  68. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  69. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  70. scene.add( gltf.scene );
  71. } );
  72. } );
  73. const lutCubeLoader = new LUTCubeLoader();
  74. const lutImageLoader = new LUTImageLoader();
  75. const lut3dlLoader = new LUT3dlLoader();
  76. for ( const name in lutMap ) {
  77. if ( /\.CUBE$/i.test( name ) ) {
  78. lutMap[ name ] = lutCubeLoader.loadAsync( 'luts/' + name );
  79. } else if ( /\LUT$/i.test( name ) ) {
  80. lutMap[ name ] = lutImageLoader.loadAsync( `luts/${name}.png` );
  81. } else {
  82. lutMap[ name ] = lut3dlLoader.loadAsync( 'luts/' + name );
  83. }
  84. }
  85. const pendings = Object.values( lutMap );
  86. await Promise.all( pendings );
  87. for ( const name in lutMap ) {
  88. lutMap[ name ] = await lutMap[ name ];
  89. }
  90. renderer = new THREE.WebGPURenderer();
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.setAnimationLoop( animate );
  94. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  95. container.appendChild( renderer.domElement );
  96. // post processing
  97. postProcessing = new THREE.PostProcessing( renderer );
  98. // ignore default output color transform ( toneMapping and outputColorSpace )
  99. // use renderOutput() for control the sequence
  100. postProcessing.outputColorTransform = false;
  101. // scene pass
  102. const scenePass = pass( scene, camera );
  103. const outputPass = renderOutput( scenePass );
  104. const lut = lutMap[ params.lut ];
  105. lutPass = lut3D( outputPass, texture3D( lut.texture3D ), lut.texture3D.image.width, uniform( 1 ) );
  106. postProcessing.outputNode = lutPass;
  107. //
  108. const controls = new OrbitControls( camera, renderer.domElement );
  109. controls.minDistance = 2;
  110. controls.maxDistance = 10;
  111. controls.target.set( 0, 0, - 0.2 );
  112. controls.update();
  113. gui = new GUI();
  114. gui.add( params, 'lut', Object.keys( lutMap ) );
  115. gui.add( params, 'intensity' ).min( 0 ).max( 1 );
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. function animate() {
  125. lutPass.intensityNode.value = params.intensity;
  126. if ( lutMap[ params.lut ] ) {
  127. const lut = lutMap[ params.lut ];
  128. lutPass.lutNode.value = lut.texture3D;
  129. lutPass.size.value = lut.texture3D.image.width;
  130. }
  131. postProcessing.render();
  132. }
  133. </script>
  134. </body>
  135. </html>