webgpu_postprocessing_dof.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing dof</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">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_dof.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_dof.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</span>
  18. </div>
  19. <small>High-Quality Depth-of-Field effect.</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 { cubeTexture, positionWorld, oscSine, time, pass, uniform } from 'three/tsl';
  34. import { dof } from 'three/addons/tsl/display/DepthOfFieldNode.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. //
  38. let camera, scene, renderer, mesh, controls;
  39. let width = window.innerWidth;
  40. let height = window.innerHeight;
  41. let renderPipeline;
  42. init();
  43. function init() {
  44. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3500 );
  45. camera.position.z = 200;
  46. scene = new THREE.Scene();
  47. const path = 'textures/cube/SwedishRoyalCastle/';
  48. const format = '.jpg';
  49. const urls = [
  50. path + 'px' + format, path + 'nx' + format,
  51. path + 'py' + format, path + 'ny' + format,
  52. path + 'pz' + format, path + 'nz' + format
  53. ];
  54. const xgrid = 14, ygrid = 9, zgrid = 14;
  55. const count = xgrid * ygrid * zgrid;
  56. const textureCube = new THREE.CubeTextureLoader().load( urls );
  57. const cubeTextureNode = cubeTexture( textureCube );
  58. const oscPos = oscSine( positionWorld.div( 1000 /* scene distance */ ).add( time.mul( .2 ) ) );
  59. const geometry = new THREE.SphereGeometry( 60, 20, 10 );
  60. const material = new THREE.MeshBasicNodeMaterial();
  61. material.colorNode = cubeTextureNode.mul( oscPos );
  62. mesh = new THREE.InstancedMesh( geometry, material, count );
  63. scene.add( mesh );
  64. const matrix = new THREE.Matrix4();
  65. let index = 0;
  66. for ( let i = 0; i < xgrid; i ++ ) {
  67. for ( let j = 0; j < ygrid; j ++ ) {
  68. for ( let k = 0; k < zgrid; k ++ ) {
  69. const x = 200 * ( i - xgrid / 2 );
  70. const y = 200 * ( j - ygrid / 2 );
  71. const z = 200 * ( k - zgrid / 2 );
  72. mesh.setMatrixAt( index, matrix.identity().setPosition( x, y, z ) );
  73. index ++;
  74. }
  75. }
  76. }
  77. // renderer
  78. renderer = new THREE.WebGPURenderer();
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( animate );
  82. renderer.inspector = new Inspector();
  83. document.body.appendChild( renderer.domElement );
  84. const effectController = {
  85. focusDistance: uniform( 500 ),
  86. focalLength: uniform( 200 ),
  87. bokehScale: uniform( 10 )
  88. };
  89. // post processing
  90. renderPipeline = new THREE.RenderPipeline( renderer );
  91. const scenePass = pass( scene, camera );
  92. const scenePassColor = scenePass.getTextureNode().toInspector( 'Color' );
  93. const scenePassViewZ = scenePass.getViewZNode();
  94. const dofPass = dof( scenePassColor, scenePassViewZ, effectController.focusDistance, effectController.focalLength, effectController.bokehScale );
  95. renderPipeline.outputNode = dofPass;
  96. // controls
  97. controls = new OrbitControls( camera, renderer.domElement );
  98. controls.enableDamping = true;
  99. window.addEventListener( 'resize', onWindowResize );
  100. // gui
  101. const gui = renderer.inspector.createParameters( 'Settings' );
  102. gui.add( effectController.focusDistance, 'value', 10.0, 3000.0 ).name( 'focus distance' );
  103. gui.add( effectController.focalLength, 'value', 50, 750 ).name( 'focal length' );
  104. gui.add( effectController.bokehScale, 'value', 1, 20 ).name( 'bokeh scale' );
  105. }
  106. function onWindowResize() {
  107. width = window.innerWidth;
  108. height = window.innerHeight;
  109. camera.aspect = width / height;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( width, height );
  112. }
  113. function animate() {
  114. controls.update();
  115. renderPipeline.render();
  116. }
  117. </script>
  118. </body>
  119. </html>
粤ICP备19079148号