webgpu_postprocessing_3dlut.html 7.2 KB

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