webgpu_postprocessing_dof.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. <link type="text/css" rel="stylesheet" href="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>DoF</span>
  14. </div>
  15. <small>High-Quality Depth-of-Field effect.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { cubeTexture, positionWorld, oscSine, time, pass, uniform } from 'three/tsl';
  30. import { dof } from 'three/addons/tsl/display/DepthOfFieldNode.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. //
  34. let camera, scene, renderer, mesh, controls;
  35. let width = window.innerWidth;
  36. let height = window.innerHeight;
  37. let postProcessing;
  38. init();
  39. function init() {
  40. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3500 );
  41. camera.position.z = 200;
  42. scene = new THREE.Scene();
  43. const path = 'textures/cube/SwedishRoyalCastle/';
  44. const format = '.jpg';
  45. const urls = [
  46. path + 'px' + format, path + 'nx' + format,
  47. path + 'py' + format, path + 'ny' + format,
  48. path + 'pz' + format, path + 'nz' + format
  49. ];
  50. const xgrid = 14, ygrid = 9, zgrid = 14;
  51. const count = xgrid * ygrid * zgrid;
  52. const textureCube = new THREE.CubeTextureLoader().load( urls );
  53. const cubeTextureNode = cubeTexture( textureCube );
  54. const oscPos = oscSine( positionWorld.div( 1000 /* scene distance */ ).add( time.mul( .2 ) ) );
  55. const geometry = new THREE.SphereGeometry( 60, 20, 10 );
  56. const material = new THREE.MeshBasicNodeMaterial();
  57. material.colorNode = cubeTextureNode.mul( oscPos );
  58. mesh = new THREE.InstancedMesh( geometry, material, count );
  59. scene.add( mesh );
  60. const matrix = new THREE.Matrix4();
  61. let index = 0;
  62. for ( let i = 0; i < xgrid; i ++ ) {
  63. for ( let j = 0; j < ygrid; j ++ ) {
  64. for ( let k = 0; k < zgrid; k ++ ) {
  65. const x = 200 * ( i - xgrid / 2 );
  66. const y = 200 * ( j - ygrid / 2 );
  67. const z = 200 * ( k - zgrid / 2 );
  68. mesh.setMatrixAt( index, matrix.identity().setPosition( x, y, z ) );
  69. index ++;
  70. }
  71. }
  72. }
  73. // renderer
  74. renderer = new THREE.WebGPURenderer();
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.setAnimationLoop( animate );
  78. renderer.inspector = new Inspector();
  79. document.body.appendChild( renderer.domElement );
  80. const effectController = {
  81. focusDistance: uniform( 500 ),
  82. focalLength: uniform( 200 ),
  83. bokehScale: uniform( 10 )
  84. };
  85. // post processing
  86. postProcessing = new THREE.PostProcessing( renderer );
  87. const scenePass = pass( scene, camera );
  88. const scenePassColor = scenePass.getTextureNode().toInspector( 'Color' );
  89. const scenePassViewZ = scenePass.getViewZNode();
  90. const dofPass = dof( scenePassColor, scenePassViewZ, effectController.focusDistance, effectController.focalLength, effectController.bokehScale );
  91. postProcessing.outputNode = dofPass;
  92. // controls
  93. controls = new OrbitControls( camera, renderer.domElement );
  94. controls.enableDamping = true;
  95. window.addEventListener( 'resize', onWindowResize );
  96. // gui
  97. const gui = renderer.inspector.createParameters( 'Settings' );
  98. gui.add( effectController.focusDistance, 'value', 10.0, 3000.0 ).name( 'focus distance' );
  99. gui.add( effectController.focalLength, 'value', 50, 750 ).name( 'focal length' );
  100. gui.add( effectController.bokehScale, 'value', 1, 20 ).name( 'bokeh scale' );
  101. }
  102. function onWindowResize() {
  103. width = window.innerWidth;
  104. height = window.innerHeight;
  105. camera.aspect = width / height;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( width, height );
  108. }
  109. function animate() {
  110. controls.update();
  111. postProcessing.render();
  112. }
  113. </script>
  114. </body>
  115. </html>
粤ICP备19079148号