webgpu_postprocessing_dof.html 4.6 KB

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