webgpu_postprocessing_3dlut.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. <meta property="og:title" content="three.js webgpu - 3d luts">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_3dlut.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_3dlut.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>3D LUTs</span>
  18. </div>
  19. <small>
  20. 3D Lookup-Tables for color grading, based on <a href="https://threejs-journey.com/lessons/coffee-smoke-shader" target="_blank" rel="noopener">Three.js Journey</a> lesson.<br />
  21. Perlin noise texture from <a href="http://kitfox.com/projects/perlinNoiseMaker/" target="_blank" rel="noopener">Perlin Noise Maker</a>,
  22. 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>.
  23. </small>
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.webgpu.js",
  29. "three/webgpu": "../build/three.webgpu.js",
  30. "three/tsl": "../build/three.tsl.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three/webgpu';
  37. import { mix, mul, oneMinus, positionLocal, smoothstep, texture, time, rotateUV, Fn, uv, vec2, vec3, vec4, pass, texture3D, uniform, renderOutput } from 'three/tsl';
  38. import { lut3D } from 'three/addons/tsl/display/Lut3DNode.js';
  39. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  40. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  41. import { LUTCubeLoader } from 'three/addons/loaders/LUTCubeLoader.js';
  42. import { LUT3dlLoader } from 'three/addons/loaders/LUT3dlLoader.js';
  43. import { LUTImageLoader } from 'three/addons/loaders/LUTImageLoader.js';
  44. import { Inspector } from 'three/addons/inspector/Inspector.js';
  45. const params = {
  46. lut: 'Bourbon 64.CUBE',
  47. intensity: 1
  48. };
  49. const lutMap = {
  50. 'Bourbon 64.CUBE': null,
  51. 'Chemical 168.CUBE': null,
  52. 'Clayton 33.CUBE': null,
  53. 'Cubicle 99.CUBE': null,
  54. 'Remy 24.CUBE': null,
  55. 'Presetpro-Cinematic.3dl': null,
  56. 'NeutralLUT': null,
  57. 'B&WLUT': null,
  58. 'NightLUT': null
  59. };
  60. let camera, scene, renderer, renderPipeline, controls, lutPass;
  61. init();
  62. async function init() {
  63. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  64. camera.position.set( 8, 10, 12 );
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0x000000 );
  67. // Loaders
  68. const gltfLoader = new GLTFLoader();
  69. const textureLoader = new THREE.TextureLoader();
  70. // LUTs
  71. const lutCubeLoader = new LUTCubeLoader();
  72. const lutImageLoader = new LUTImageLoader();
  73. const lut3dlLoader = new LUT3dlLoader();
  74. for ( const name in lutMap ) {
  75. if ( /\.CUBE$/i.test( name ) ) {
  76. lutMap[ name ] = lutCubeLoader.loadAsync( 'luts/' + name );
  77. } else if ( /\LUT$/i.test( name ) ) {
  78. lutMap[ name ] = lutImageLoader.loadAsync( `luts/${name}.png` );
  79. } else {
  80. lutMap[ name ] = lut3dlLoader.loadAsync( 'luts/' + name );
  81. }
  82. }
  83. const pendings = Object.values( lutMap );
  84. await Promise.all( pendings );
  85. for ( const name in lutMap ) {
  86. lutMap[ name ] = await lutMap[ name ];
  87. }
  88. // baked model
  89. gltfLoader.load(
  90. './models/gltf/coffeeMug.glb',
  91. ( gltf ) => {
  92. gltf.scene.getObjectByName( 'baked' ).material.map.anisotropy = 8;
  93. scene.add( gltf.scene );
  94. }
  95. );
  96. // geometry
  97. const smokeGeometry = new THREE.PlaneGeometry( 1, 1, 16, 64 );
  98. smokeGeometry.translate( 0, 0.5, 0 );
  99. smokeGeometry.scale( 1.5, 6, 1.5 );
  100. // texture
  101. const noiseTexture = textureLoader.load( './textures/noises/perlin/128x128.png' );
  102. noiseTexture.wrapS = THREE.RepeatWrapping;
  103. noiseTexture.wrapT = THREE.RepeatWrapping;
  104. // material
  105. const smokeMaterial = new THREE.MeshBasicNodeMaterial( { transparent: true, side: THREE.DoubleSide, depthWrite: false } );
  106. // position
  107. smokeMaterial.positionNode = Fn( () => {
  108. // twist
  109. const twistNoiseUv = vec2( 0.5, uv().y.mul( 0.2 ).sub( time.mul( 0.005 ) ).mod( 1 ) );
  110. const twist = texture( noiseTexture, twistNoiseUv ).r.mul( 10 );
  111. positionLocal.xz.assign( rotateUV( positionLocal.xz, twist, vec2( 0 ) ) );
  112. // wind
  113. const windOffset = vec2(
  114. texture( noiseTexture, vec2( 0.25, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  115. texture( noiseTexture, vec2( 0.75, time.mul( 0.01 ) ).mod( 1 ) ).r.sub( 0.5 ),
  116. ).mul( uv().y.pow( 2 ).mul( 10 ) );
  117. positionLocal.addAssign( windOffset );
  118. return positionLocal;
  119. } )();
  120. // color
  121. smokeMaterial.colorNode = Fn( () => {
  122. // alpha
  123. const alphaNoiseUv = uv().mul( vec2( 0.5, 0.3 ) ).add( vec2( 0, time.mul( 0.03 ).negate() ) );
  124. const alpha = mul(
  125. // pattern
  126. texture( noiseTexture, alphaNoiseUv ).r.smoothstep( 0.4, 1 ),
  127. // edges fade
  128. smoothstep( 0, 0.1, uv().x ),
  129. smoothstep( 0, 0.1, oneMinus( uv().x ) ),
  130. smoothstep( 0, 0.1, uv().y ),
  131. smoothstep( 0, 0.1, oneMinus( uv().y ) )
  132. );
  133. // color
  134. const finalColor = mix( vec3( 0.6, 0.3, 0.2 ), vec3( 1, 1, 1 ), alpha.pow( 3 ) );
  135. return vec4( finalColor, alpha );
  136. } )();
  137. // mesh
  138. const smoke = new THREE.Mesh( smokeGeometry, smokeMaterial );
  139. smoke.position.y = 1.83;
  140. scene.add( smoke );
  141. // renderer
  142. renderer = new THREE.WebGPURenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. renderer.setAnimationLoop( animate );
  146. renderer.inspector = new Inspector();
  147. document.body.appendChild( renderer.domElement );
  148. // post processing
  149. renderPipeline = new THREE.RenderPipeline( renderer );
  150. // ignore default output color transform ( toneMapping and outputColorSpace )
  151. // use renderOutput() for control the sequence
  152. renderPipeline.outputColorTransform = false;
  153. // scene pass
  154. const scenePass = pass( scene, camera );
  155. const outputPass = renderOutput( scenePass );
  156. const lut = lutMap[ params.lut ];
  157. lutPass = lut3D( outputPass, texture3D( lut.texture3D ), lut.texture3D.image.width, uniform( 1 ) );
  158. renderPipeline.outputNode = lutPass;
  159. // controls
  160. controls = new OrbitControls( camera, renderer.domElement );
  161. controls.enableDamping = true;
  162. controls.minDistance = 0.1;
  163. controls.maxDistance = 50;
  164. controls.target.y = 3;
  165. // gui
  166. const gui = renderer.inspector.createParameters( 'Settings' );
  167. gui.add( params, 'lut', Object.keys( lutMap ) );
  168. gui.add( params, 'intensity', 0, 1 );
  169. window.addEventListener( 'resize', onWindowResize );
  170. }
  171. function onWindowResize() {
  172. camera.aspect = window.innerWidth / window.innerHeight;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( window.innerWidth, window.innerHeight );
  175. }
  176. async function animate() {
  177. controls.update();
  178. lutPass.intensityNode.value = params.intensity;
  179. if ( lutMap[ params.lut ] ) {
  180. const lut = lutMap[ params.lut ];
  181. lutPass.lutNode.value = lut.texture3D;
  182. lutPass.size.value = lut.texture3D.image.width;
  183. }
  184. renderPipeline.render();
  185. }
  186. </script>
  187. </body>
  188. </html>
粤ICP备19079148号