1
0

webgpu_postprocessing_dof_basic.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 { boxBlur } from 'three/addons/tsl/display/boxBlur.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 blurSize = uniform( 2 ); // determines the kernel size of the blur
  92. const blurSpread = uniform( 4 ); // determines how far the blur is spread
  93. const minDistance = uniform( 1 ); // all positions at or below minDistance will be completely in focus.
  94. const maxDistance = uniform( 3 ); // all positions at or beyond maxDistance will be completely out of focus.
  95. // beauty and blur/out-of-focus pass
  96. const scenePass = pass( scene, camera );
  97. const scenePassColor = scenePass.getTextureNode();
  98. const scenePassViewZ = scenePass.getViewZNode();
  99. const scenePassBlurred = boxBlur( scenePassColor, { size: blurSize, separation: blurSpread } );
  100. // simple DOF from https://lettier.github.io/3d-game-shaders-for-beginners/depth-of-field.html
  101. const blur = smoothstep( minDistance, maxDistance, scenePassViewZ.sub( focusPointView.z ).abs() );
  102. const dofPass = mix( scenePassColor, scenePassBlurred, blur );
  103. const outputPass = renderOutput( dofPass );
  104. const fxaaPass = fxaa( outputPass );
  105. postProcessing.outputNode = fxaaPass;
  106. // GUI
  107. const gui = new GUI();
  108. gui.add( minDistance, 'value', 0, 3 ).name( 'min distance' );
  109. gui.add( maxDistance, 'value', 0, 5 ).name( 'max distance' );
  110. gui.add( blurSize, 'value', 1, 3, 1 ).name( 'blur size' );
  111. gui.add( blurSpread, 'value', 1, 7, 1 ).name( 'blur spread' );
  112. //
  113. controls.connect( renderer.domElement );
  114. renderer.domElement.addEventListener( 'pointerdown', onPointerDown );
  115. window.addEventListener( 'resize', onWindowResize );
  116. }
  117. function onPointerDown( event ) {
  118. pointerCoords.set(
  119. ( event.clientX / window.innerWidth ) * 2 - 1,
  120. - ( event.clientY / window.innerHeight ) * 2 + 1
  121. );
  122. raycaster.setFromCamera( pointerCoords, camera );
  123. const intersects = raycaster.intersectObject( model );
  124. if ( intersects.length > 0 ) {
  125. TWEEN.removeAll();
  126. new TWEEN.Tween( focusPoint )
  127. .to( intersects[ 0 ].point, 500 )
  128. .easing( TWEEN.Easing.Cubic.InOut )
  129. .start();
  130. }
  131. }
  132. function onWindowResize() {
  133. camera.aspect = window.innerWidth / window.innerHeight;
  134. camera.updateProjectionMatrix();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. }
  137. function animate() {
  138. TWEEN.update();
  139. controls.update();
  140. timer.update();
  141. mixer.update( timer.getDelta() );
  142. // since the focus point is expressed in view space, it must be updated on every
  143. // camera change. for simplicity, do this every frame.
  144. camera.updateMatrixWorld();
  145. focusPointView.value.copy( focusPoint ).applyMatrix4( camera.matrixWorldInverse );
  146. postProcessing.render();
  147. }
  148. </script>
  149. </body>
  150. </html>
粤ICP备19079148号