1
0

webgpu_postprocessing_dof_basic.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. background: #90D5FF;
  13. }
  14. a {
  15. color: #2983ff;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - DOF - basic</a><br />
  22. <a href="https://skfb.ly/opNFG" target="_blank" rel="noopener">Bath day</a> by
  23. <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 />
  24. Click on a position in the scene to focus it.<br />
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.webgpu.js",
  30. "three/webgpu": "../build/three.webgpu.js",
  31. "three/tsl": "../build/three.tsl.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three/webgpu';
  38. import { mix, pass, renderOutput, smoothstep, uniform, vec3 } from 'three/tsl';
  39. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  40. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  41. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  42. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  43. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  44. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  45. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  46. import TWEEN from 'three/addons/libs/tween.module.js';
  47. let camera, controls, scene, timer, renderer, model, mixer, raycaster, postProcessing;
  48. const pointerCoords = new THREE.Vector2();
  49. const focusPoint = new THREE.Vector3( 1, 1.75, - 0.4 );
  50. const focusPointView = uniform( vec3() );
  51. init();
  52. async function init() {
  53. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  54. camera.position.set( - 6, 5, 6 );
  55. controls = new OrbitControls( camera );
  56. controls.target.set( 0, 2, 0 );
  57. controls.enableDamping = true;
  58. controls.update();
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0x90D5FF );
  61. raycaster = new THREE.Raycaster();
  62. timer = new THREE.Timer();
  63. const dracoLoader = new DRACOLoader();
  64. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  65. const loader = new GLTFLoader();
  66. loader.setDRACOLoader( dracoLoader );
  67. const gltf = await loader.loadAsync( 'models/gltf/bath_day.glb' );
  68. model = gltf.scene;
  69. scene.add( model );
  70. mixer = new THREE.AnimationMixer( model );
  71. const action = mixer.clipAction( gltf.animations[ 0 ] );
  72. action.play();
  73. //
  74. const hdrLoader = new UltraHDRLoader();
  75. hdrLoader.setDataType( THREE.HalfFloatType );
  76. const envMap = await hdrLoader.loadAsync( 'textures/equirectangular/spruit_sunrise_2k.hdr.jpg' );
  77. envMap.mapping = THREE.EquirectangularReflectionMapping;
  78. scene.environmentRotation.y = Math.PI * 0.5;
  79. scene.environment = envMap;
  80. // renderer
  81. renderer = new THREE.WebGPURenderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.setAnimationLoop( animate );
  85. renderer.toneMapping = THREE.NeutralToneMapping;
  86. document.body.appendChild( renderer.domElement );
  87. // post processing
  88. postProcessing = new THREE.PostProcessing( renderer );
  89. postProcessing.outputColorTransform = false;
  90. // DOF uniforms
  91. const maxBlur = uniform( 2 ); // maximum amount of blur
  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 scenePassViewZ = scenePass.getViewZNode();
  97. const scenePassBlurred = gaussianBlur( scenePass, maxBlur );
  98. // simple DOF from https://lettier.github.io/3d-game-shaders-for-beginners/depth-of-field.html
  99. const blur = smoothstep( minDistance, maxDistance, scenePassViewZ.sub( focusPointView.z ).abs() );
  100. const dofPass = mix( scenePass, scenePassBlurred, blur );
  101. const outputPass = renderOutput( dofPass );
  102. const fxaaPass = fxaa( outputPass );
  103. postProcessing.outputNode = fxaaPass;
  104. // GUI
  105. const gui = new GUI();
  106. gui.add( minDistance, 'value', 0, 3 ).name( 'min distance' );
  107. gui.add( maxDistance, 'value', 0, 5 ).name( 'max distance' );
  108. gui.add( maxBlur, 'value', 0, 5 ).name( 'max blur' );
  109. //
  110. controls.connect( renderer.domElement );
  111. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  112. window.addEventListener( 'resize', onWindowResize );
  113. }
  114. function onPointerDown( event ) {
  115. pointerCoords.set(
  116. ( event.clientX / window.innerWidth ) * 2 - 1,
  117. - ( event.clientY / window.innerHeight ) * 2 + 1
  118. );
  119. raycaster.setFromCamera( pointerCoords, camera );
  120. const intersects = raycaster.intersectObject( model );
  121. if ( intersects.length > 0 ) {
  122. TWEEN.removeAll();
  123. new TWEEN.Tween( focusPoint )
  124. .to( intersects[ 0 ].point, 500 )
  125. .easing( TWEEN.Easing.Cubic.InOut )
  126. .start();
  127. }
  128. }
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function animate() {
  135. TWEEN.update();
  136. controls.update();
  137. timer.update();
  138. mixer.update( timer.getDelta() );
  139. // since the focus point is expressed in view space, it must be updated on every
  140. // camera change. for simplicity, do this every frame.
  141. camera.updateMatrixWorld();
  142. focusPointView.value.copy( focusPoint ).applyMatrix4( camera.matrixWorldInverse );
  143. postProcessing.render();
  144. }
  145. </script>
  146. </body>
  147. </html>
粤ICP备19079148号