webgpu_postprocessing_3dlut.html 5.5 KB

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