1
0

webgpu_postprocessing_dof_basic.html 6.9 KB

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