webgpu_postprocessing_dof.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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="main.css">
  8. </head>
  9. <body>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/webgpu": "../build/three.webgpu.js",
  15. "three/tsl": "../build/three.tsl.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import { cubeTexture, positionWorld, oscSine, time, pass, uniform } from 'three/tsl';
  23. import { dof } from 'three/addons/tsl/display/DepthOfFieldNode.js';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. //
  27. let camera, scene, renderer, mesh, stats;
  28. let mouseX = 0, mouseY = 0;
  29. let windowHalfX = window.innerWidth / 2;
  30. let windowHalfY = window.innerHeight / 2;
  31. let width = window.innerWidth;
  32. let height = window.innerHeight;
  33. let postProcessing;
  34. init();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3000 );
  37. camera.position.z = 200;
  38. scene = new THREE.Scene();
  39. const path = 'textures/cube/SwedishRoyalCastle/';
  40. const format = '.jpg';
  41. const urls = [
  42. path + 'px' + format, path + 'nx' + format,
  43. path + 'py' + format, path + 'ny' + format,
  44. path + 'pz' + format, path + 'nz' + format
  45. ];
  46. const xgrid = 14, ygrid = 9, zgrid = 14;
  47. const count = xgrid * ygrid * zgrid;
  48. const textureCube = new THREE.CubeTextureLoader().load( urls );
  49. const cubeTextureNode = cubeTexture( textureCube );
  50. const oscPos = oscSine( positionWorld.div( 1000 /* scene distance */ ).add( time.mul( .2 ) ) );
  51. const geometry = new THREE.SphereGeometry( 60, 20, 10 );
  52. const material = new THREE.MeshBasicNodeMaterial();
  53. material.colorNode = cubeTextureNode.mul( oscPos );
  54. mesh = new THREE.InstancedMesh( geometry, material, count );
  55. scene.add( mesh );
  56. const matrix = new THREE.Matrix4();
  57. let index = 0;
  58. for ( let i = 0; i < xgrid; i ++ ) {
  59. for ( let j = 0; j < ygrid; j ++ ) {
  60. for ( let k = 0; k < zgrid; k ++ ) {
  61. const x = 200 * ( i - xgrid / 2 );
  62. const y = 200 * ( j - ygrid / 2 );
  63. const z = 200 * ( k - zgrid / 2 );
  64. mesh.setMatrixAt( index, matrix.identity().setPosition( x, y, z ) );
  65. index ++;
  66. }
  67. }
  68. }
  69. // renderer
  70. renderer = new THREE.WebGPURenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( animate );
  74. document.body.appendChild( renderer.domElement );
  75. const effectController = {
  76. focus: uniform( 500.0 ),
  77. aperture: uniform( 5 ),
  78. maxblur: uniform( 0.01 )
  79. };
  80. // post processing
  81. postProcessing = new THREE.PostProcessing( renderer );
  82. const scenePass = pass( scene, camera );
  83. const scenePassColor = scenePass.getTextureNode();
  84. const scenePassViewZ = scenePass.getViewZNode();
  85. const dofPass = dof( scenePassColor, scenePassViewZ, effectController.focus, effectController.aperture.mul( 0.00001 ), effectController.maxblur );
  86. postProcessing.outputNode = dofPass;
  87. // controls
  88. renderer.domElement.style.touchAction = 'none';
  89. renderer.domElement.addEventListener( 'pointermove', onPointerMove );
  90. window.addEventListener( 'resize', onWindowResize );
  91. // stats
  92. stats = new Stats();
  93. document.body.appendChild( stats.dom );
  94. // gui
  95. const gui = new GUI();
  96. gui.add( effectController.focus, 'value', 10.0, 3000.0, 10 ).name( 'focus' );
  97. gui.add( effectController.aperture, 'value', 0, 10, 0.1 ).name( 'aperture' );
  98. gui.add( effectController.maxblur, 'value', 0.0, 0.01, 0.001 ).name( 'maxblur' );
  99. }
  100. function onPointerMove( event ) {
  101. if ( event.isPrimary === false ) return;
  102. mouseX = event.clientX - windowHalfX;
  103. mouseY = event.clientY - windowHalfY;
  104. }
  105. function onWindowResize() {
  106. windowHalfX = window.innerWidth / 2;
  107. windowHalfY = window.innerHeight / 2;
  108. width = window.innerWidth;
  109. height = window.innerHeight;
  110. camera.aspect = width / height;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( width, height );
  113. }
  114. function animate() {
  115. render();
  116. stats.update();
  117. }
  118. function render() {
  119. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  120. camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
  121. camera.lookAt( scene.position );
  122. postProcessing.render();
  123. }
  124. </script>
  125. </body>
  126. </html>
粤ICP备19079148号