webgpu_postprocessing_3dlut.html 7.4 KB

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