webgl_postprocessing_3dlut.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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> by <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>
  16. </div>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  20. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  21. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  22. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  23. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  24. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  25. import { LUTPass } from './jsm/postprocessing/LUTPass.js';
  26. import { LUTCubeLoader } from './jsm/loaders/LUTCubeLoader.js';
  27. import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
  28. import { GUI } from './jsm/libs/dat.gui.module.js';
  29. const params = {
  30. enabled: true,
  31. lut: 'Bourbon 64.CUBE',
  32. intensity: 1,
  33. use2DLut: false,
  34. };
  35. const lutMap = {
  36. 'Bourbon 64.CUBE': null,
  37. 'Chemical 168.CUBE': null,
  38. 'Clayton 33.CUBE': null,
  39. 'Cubicle 99.CUBE': null,
  40. 'Remy 24.CUBE': null,
  41. };
  42. let gui;
  43. let camera, scene, renderer;
  44. let composer, lutPass;
  45. init();
  46. render();
  47. function init() {
  48. const container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  51. camera.position.set( - 1.8, 0.6, 2.7 );
  52. scene = new THREE.Scene();
  53. new RGBELoader()
  54. .setDataType( THREE.FloatType )
  55. .setPath( 'textures/equirectangular/' )
  56. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  57. texture.mapping = THREE.EquirectangularReflectionMapping;
  58. scene.background = texture;
  59. scene.environment = texture;
  60. // model
  61. const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
  62. loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
  63. scene.add( gltf.scene );
  64. } );
  65. } );
  66. Object.keys( lutMap ).forEach( name => {
  67. new LUTCubeLoader()
  68. .load( 'luts/' + name, function ( result ) {
  69. lutMap[ name ] = result;
  70. } );
  71. } );
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  76. renderer.toneMappingExposure = 1;
  77. container.appendChild( renderer.domElement );
  78. const target = new THREE.WebGLRenderTarget( {
  79. minFilter: THREE.LinearFilter,
  80. magFilter: THREE.LinearFilter,
  81. format: THREE.RGBAFormat,
  82. encoding: THREE.sRGBEncoding
  83. } );
  84. composer = new EffectComposer( renderer, target );
  85. composer.setPixelRatio( window.devicePixelRatio );
  86. composer.setSize( window.innerWidth, window.innerHeight );
  87. composer.addPass( new RenderPass( scene, camera ) );
  88. composer.addPass( new ShaderPass( GammaCorrectionShader ) );
  89. lutPass = new LUTPass();
  90. composer.addPass( lutPass );
  91. const controls = new OrbitControls( camera, renderer.domElement );
  92. controls.minDistance = 2;
  93. controls.maxDistance = 10;
  94. controls.target.set( 0, 0, - 0.2 );
  95. controls.update();
  96. gui = new GUI();
  97. gui.width = 350;
  98. gui.add( params, 'enabled' );
  99. gui.add( params, 'lut', Object.keys( lutMap ) );
  100. gui.add( params, 'intensity' ).min( 0 ).max( 1 );
  101. if ( renderer.capabilities.isWebGL2 ) {
  102. gui.add( params, 'use2DLut' );
  103. } else {
  104. params.use2DLut = true;
  105. }
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. composer.setSize( window.innerWidth, window.innerHeight );
  113. render();
  114. }
  115. //
  116. function render() {
  117. requestAnimationFrame( render );
  118. lutPass.enabled = params.enabled && Boolean( lutMap[ params.lut ] );
  119. lutPass.intensity = params.intensity;
  120. if ( lutMap[ params.lut ] ) {
  121. const lut = lutMap[ params.lut ];
  122. lutPass.lut = params.use2DLut ? lut.texture : lut.texture3D;
  123. }
  124. composer.render();
  125. }
  126. </script>
  127. </body>
  128. </html>