1
0

webgpu_postprocessing_dof_basic.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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="main.css">
  8. <style>
  9. body {
  10. touch-action: none;
  11. color: #000000;
  12. }
  13. a {
  14. color: #2983ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - DOF - basic</a><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. Click on a position in the scene to focus it.<br />
  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 { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  39. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  40. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  41. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  42. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  43. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  44. import TWEEN from 'three/addons/libs/tween.module.js';
  45. let camera, scene, timer, renderer, model, mixer, raycaster, postProcessing;
  46. const pointerCoords = new THREE.Vector2();
  47. const focusPoint = new THREE.Vector3( 1, 1.75, - 0.4 );
  48. const focusPointView = uniform( vec3() );
  49. init();
  50. async function init() {
  51. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  52. camera.position.set( - 5, 4, 5 );
  53. camera.lookAt( 0, 1.5, 0 );
  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. hdrLoader.setDataType( THREE.HalfFloatType );
  71. const envMap = await hdrLoader.loadAsync( 'textures/equirectangular/spruit_sunrise_2k.hdr.jpg' );
  72. envMap.mapping = THREE.EquirectangularReflectionMapping;
  73. scene.environmentRotation.y = Math.PI * 0.5;
  74. scene.environment = envMap;
  75. // renderer
  76. renderer = new THREE.WebGPURenderer();
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.setAnimationLoop( animate );
  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 maxBlur = uniform( 2 ); // maximum amount of blur
  87. const minDistance = uniform( 1 ); // all positions at or below minDistance will be completely in focus.
  88. const maxDistance = uniform( 3 ); // all positions at or beyond maxDistance will be completely out of focus.
  89. // beauty and blur/out-of-focus pass
  90. const scenePass = pass( scene, camera );
  91. const scenePassViewZ = scenePass.getViewZNode();
  92. const scenePassBlurred = gaussianBlur( scenePass, maxBlur );
  93. // simple DOF from https://lettier.github.io/3d-game-shaders-for-beginners/depth-of-field.html
  94. const blur = smoothstep( minDistance, maxDistance, scenePassViewZ.sub( focusPointView.z ).abs() );
  95. const dofPass = mix( scenePass, scenePassBlurred, blur );
  96. const outputPass = renderOutput( dofPass );
  97. const fxaaPass = fxaa( outputPass );
  98. postProcessing.outputNode = fxaaPass;
  99. // GUI
  100. const gui = new GUI();
  101. gui.add( minDistance, 'value', 0, 3 ).name( 'min distance' );
  102. gui.add( maxDistance, 'value', 0, 5 ).name( 'max distance' );
  103. gui.add( maxBlur, 'value', 0, 5 ).name( 'max blur' );
  104. //
  105. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function onPointerDown( event ) {
  109. pointerCoords.set(
  110. ( event.clientX / window.innerWidth ) * 2 - 1,
  111. - ( event.clientY / window.innerHeight ) * 2 + 1
  112. );
  113. raycaster.setFromCamera( pointerCoords, camera );
  114. const intersects = raycaster.intersectObject( model );
  115. if ( intersects.length > 0 ) {
  116. TWEEN.removeAll();
  117. new TWEEN.Tween( focusPoint )
  118. .to( intersects[ 0 ].point, 500 )
  119. .easing( TWEEN.Easing.Cubic.InOut )
  120. .start();
  121. }
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. function animate() {
  129. TWEEN.update();
  130. timer.update();
  131. mixer.update( timer.getDelta() );
  132. // since the focus point is expressed in view space, it must be updated on every
  133. // camera change. for simplicity, do this every frame.
  134. camera.updateMatrixWorld();
  135. focusPointView.value.copy( focusPoint ).applyMatrix4( camera.matrixWorldInverse );
  136. postProcessing.render();
  137. }
  138. </script>
  139. </body>
  140. </html>
粤ICP备19079148号