webgpu_postprocessing_dof_basic.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing dof - basic</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>DoF Basic</span>
  14. </div>
  15. <small>
  16. Performant Depth-of-Field effect with a simple box blur. Click on a position in the scene to focus it.<br />
  17. <a href="https://skfb.ly/opNFG" target="_blank" rel="noopener">Bath day</a> by
  18. <a href="https://sketchfab.com/stanst" target="_blank" rel="noopener">Stan.St</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br />
  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, pass, renderOutput, smoothstep, uniform, vec3 } from 'three/tsl';
  34. import { boxBlur } from 'three/addons/tsl/display/boxBlur.js';
  35. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  39. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. import TWEEN from 'three/addons/libs/tween.module.js';
  42. let camera, controls, scene, timer, renderer, model, mixer, raycaster, postProcessing;
  43. const pointerCoords = new THREE.Vector2();
  44. const focusPoint = new THREE.Vector3( 1, 1.75, - 0.4 );
  45. const focusPointView = uniform( vec3() );
  46. init();
  47. async function init() {
  48. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  49. camera.position.set( - 6, 5, 6 );
  50. controls = new OrbitControls( camera );
  51. controls.target.set( 0, 2, 0 );
  52. controls.enableDamping = true;
  53. controls.update();
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x90D5FF );
  56. raycaster = new THREE.Raycaster();
  57. timer = new THREE.Timer();
  58. const dracoLoader = new DRACOLoader();
  59. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  60. const loader = new GLTFLoader();
  61. loader.setDRACOLoader( dracoLoader );
  62. const gltf = await loader.loadAsync( 'models/gltf/bath_day.glb' );
  63. model = gltf.scene;
  64. scene.add( model );
  65. mixer = new THREE.AnimationMixer( model );
  66. const action = mixer.clipAction( gltf.animations[ 0 ] );
  67. action.play();
  68. //
  69. const hdrLoader = new UltraHDRLoader();
  70. const envMap = await hdrLoader.loadAsync( 'textures/equirectangular/spruit_sunrise_2k.hdr.jpg' );
  71. envMap.mapping = THREE.EquirectangularReflectionMapping;
  72. scene.environmentRotation.y = Math.PI * 0.5;
  73. scene.environment = envMap;
  74. // renderer
  75. renderer = new THREE.WebGPURenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.setAnimationLoop( animate );
  79. renderer.inspector = new Inspector();
  80. renderer.toneMapping = THREE.NeutralToneMapping;
  81. document.body.appendChild( renderer.domElement );
  82. // post processing
  83. postProcessing = new THREE.PostProcessing( renderer );
  84. postProcessing.outputColorTransform = false;
  85. // DOF uniforms
  86. const blurSize = uniform( 2 ); // determines the kernel size of the blur
  87. const blurSpread = uniform( 4 ); // determines how far the blur is spread
  88. const minDistance = uniform( 1 ); // all positions at or below minDistance will be completely in focus.
  89. const maxDistance = uniform( 3 ); // all positions at or beyond maxDistance will be completely out of focus.
  90. // beauty and blur/out-of-focus pass
  91. const scenePass = pass( scene, camera );
  92. const scenePassColor = scenePass.getTextureNode().toInspector( 'Color' );
  93. const scenePassViewZ = scenePass.getViewZNode();
  94. const scenePassBlurred = boxBlur( scenePassColor, { size: blurSize, separation: blurSpread } );
  95. // simple DOF from https://lettier.github.io/3d-game-shaders-for-beginners/depth-of-field.html
  96. const blur = smoothstep( minDistance, maxDistance, scenePassViewZ.sub( focusPointView.z ).abs() );
  97. const dofPass = mix( scenePassColor, scenePassBlurred, blur );
  98. const outputPass = renderOutput( dofPass );
  99. const fxaaPass = fxaa( outputPass );
  100. postProcessing.outputNode = fxaaPass;
  101. // GUI
  102. const gui = renderer.inspector.createParameters( 'Settings' );
  103. gui.add( minDistance, 'value', 0, 3 ).name( 'min distance' );
  104. gui.add( maxDistance, 'value', 0, 5 ).name( 'max distance' );
  105. gui.add( blurSize, 'value', 1, 3, 1 ).name( 'blur size' );
  106. gui.add( blurSpread, 'value', 1, 7, 1 ).name( 'blur spread' );
  107. //
  108. controls.connect( renderer.domElement );
  109. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onPointerDown( event ) {
  113. pointerCoords.set(
  114. ( event.clientX / window.innerWidth ) * 2 - 1,
  115. - ( event.clientY / window.innerHeight ) * 2 + 1
  116. );
  117. raycaster.setFromCamera( pointerCoords, camera );
  118. const intersects = raycaster.intersectObject( model );
  119. if ( intersects.length > 0 ) {
  120. TWEEN.removeAll();
  121. new TWEEN.Tween( focusPoint )
  122. .to( intersects[ 0 ].point, 500 )
  123. .easing( TWEEN.Easing.Cubic.InOut )
  124. .start();
  125. }
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. function animate() {
  133. TWEEN.update();
  134. controls.update();
  135. timer.update();
  136. mixer.update( timer.getDelta() );
  137. // since the focus point is expressed in view space, it must be updated on every
  138. // camera change. for simplicity, do this every frame.
  139. camera.updateMatrixWorld();
  140. focusPointView.value.copy( focusPoint ).applyMatrix4( camera.matrixWorldInverse );
  141. postProcessing.render();
  142. }
  143. </script>
  144. </body>
  145. </html>
粤ICP备19079148号