webgl_postprocessing_3dlut.html 5.0 KB
Newer Older
G
Garrett Johnson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - 3d luts</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>

	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 3D LUTs<br />
			Battle Damaged Sci-fi Helmet by
			<a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br />
G
Garrett Johnson 已提交
15 16
			<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 />
			LUTs from <a href="https://www.rocketstock.com/free-after-effects-templates/35-free-luts-for-color-grading-videos/">RocketStock</a>
G
Garrett Johnson 已提交
17 18 19 20 21 22 23 24 25 26
		</div>

		<script type="module">
			import * as THREE from '../build/three.module.js';

			import { OrbitControls } from './jsm/controls/OrbitControls.js';
			import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
			import { RGBELoader } from './jsm/loaders/RGBELoader.js';
			import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
			import { RenderPass } from './jsm/postprocessing/RenderPass.js';
G
Garrett Johnson 已提交
27
			import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
G
Garrett Johnson 已提交
28 29
			import { LUTPass } from './jsm/postprocessing/LUTPass.js';
			import { LUTCubeLoader } from './jsm/loaders/LUTCubeLoader.js';
G
Garrett Johnson 已提交
30
			import { GammaCorrectionShader } from './jsm/shaders/GammaCorrectionShader.js';
G
Garrett Johnson 已提交
31 32 33 34
			import { GUI } from './jsm/libs/dat.gui.module.js';

			const params = {
				enabled: true,
G
Garrett Johnson 已提交
35 36
				lut: 'Bourbon 64.CUBE',
				intensity: 1,
37
				use2DLut: false,
G
Garrett Johnson 已提交
38 39 40 41 42 43
			};

			const lutMap = {
				'Bourbon 64.CUBE': null,
				'Chemical 168.CUBE': null,
				'Clayton 33.CUBE': null,
G
Garrett Johnson 已提交
44 45
				'Cubicle 99.CUBE': null,
				'Remy 24.CUBE': null,
G
Garrett Johnson 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
			};

			let gui;
			let camera, scene, renderer;
			let composer, lutPass;

			init();
			render();

			function init() {

				const container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
				camera.position.set( - 1.8, 0.6, 2.7 );

				scene = new THREE.Scene();

				new RGBELoader()
66
					.setDataType( THREE.FloatType )
G
Garrett Johnson 已提交
67 68 69
					.setPath( 'textures/equirectangular/' )
					.load( 'royal_esplanade_1k.hdr', function ( texture ) {

70
						texture.mapping = THREE.EquirectangularReflectionMapping;
G
Garrett Johnson 已提交
71

72 73
						scene.background = texture;
						scene.environment = texture;
G
Garrett Johnson 已提交
74 75 76 77 78 79 80 81 82 83 84 85

						// model

						const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
						loader.load( 'DamagedHelmet.gltf', function ( gltf ) {

							scene.add( gltf.scene );

						} );

					} );

G
Garrett Johnson 已提交
86 87 88 89 90 91 92 93 94 95 96
				Object.keys( lutMap ).forEach( name => {

					new LUTCubeLoader()
						.load( 'luts/' + name, function ( result ) {

							lutMap[ name ] = result;

						} );

				} );

G
Garrett Johnson 已提交
97
				renderer = new THREE.WebGLRenderer();
G
Garrett Johnson 已提交
98 99 100 101 102
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.toneMapping = THREE.ACESFilmicToneMapping;
				renderer.toneMappingExposure = 1;
				container.appendChild( renderer.domElement );
G
Garrett Johnson 已提交
103

G
Garrett Johnson 已提交
104 105 106 107 108 109
				const target = new THREE.WebGLRenderTarget( {
					minFilter: THREE.LinearFilter,
					magFilter: THREE.LinearFilter,
					format: THREE.RGBAFormat,
					encoding: THREE.sRGBEncoding
				} );
G
Garrett Johnson 已提交
110

G
Garrett Johnson 已提交
111
				composer = new EffectComposer( renderer, target );
G
Garrett Johnson 已提交
112 113 114
				composer.setPixelRatio( window.devicePixelRatio );
				composer.setSize( window.innerWidth, window.innerHeight );
				composer.addPass( new RenderPass( scene, camera ) );
G
Garrett Johnson 已提交
115
				composer.addPass( new ShaderPass( GammaCorrectionShader ) );
G
Garrett Johnson 已提交
116

G
Garrett Johnson 已提交
117 118
				lutPass = new LUTPass();
				composer.addPass( lutPass );
G
Garrett Johnson 已提交
119 120 121 122 123 124 125 126 127 128 129

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 2;
				controls.maxDistance = 10;
				controls.target.set( 0, 0, - 0.2 );
				controls.update();

				gui = new GUI();
				gui.width = 350;
				gui.add( params, 'enabled' );
				gui.add( params, 'lut', Object.keys( lutMap ) );
G
Garrett Johnson 已提交
130
				gui.add( params, 'intensity' ).min( 0 ).max( 1 );
G
Garrett Johnson 已提交
131 132 133

				if ( renderer.capabilities.isWebGL2 ) {

134
					gui.add( params, 'use2DLut' );
G
Garrett Johnson 已提交
135 136 137 138 139 140

				} else {

					params.use2DLut = true;

				}
G
Garrett Johnson 已提交
141

142
				window.addEventListener( 'resize', onWindowResize );
G
Garrett Johnson 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );
				composer.setSize( window.innerWidth, window.innerHeight );

				render();

			}

			//

			function render() {

G
Garrett Johnson 已提交
162 163
				requestAnimationFrame( render );

G
Garrett Johnson 已提交
164 165 166 167 168 169 170 171 172
				lutPass.enabled = params.enabled && Boolean( lutMap[ params.lut ] );
				lutPass.intensity = params.intensity;
				if ( lutMap[ params.lut ] ) {

					const lut = lutMap[ params.lut ];
					lutPass.lut = params.use2DLut ? lut.texture : lut.texture3D;

				}

G
Garrett Johnson 已提交
173 174 175 176 177 178 179 180
				composer.render();

			}

		</script>

	</body>
</html>