webgpu_postprocessing_dof.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 } 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. // post processing
  74. postProcessing = new THREE.PostProcessing( renderer );
  75. const scenePass = pass( scene, camera );
  76. const scenePassColor = scenePass.getTextureNode();
  77. const scenePassViewZ = scenePass.getViewZNode();
  78. const dofPass = scenePassColor.dof( scenePassViewZ );
  79. postProcessing.outputNode = dofPass;
  80. // controls
  81. renderer.domElement.style.touchAction = 'none';
  82. renderer.domElement.addEventListener( 'pointermove', onPointerMove );
  83. window.addEventListener( 'resize', onWindowResize );
  84. // stats
  85. stats = new Stats();
  86. document.body.appendChild( stats.dom );
  87. // gui
  88. const effectController = {
  89. focus: 500.0,
  90. aperture: 5,
  91. maxblur: 0.01
  92. };
  93. function updateEffect( ) {
  94. dofPass.focus.value = effectController.focus;
  95. dofPass.aperture.value = effectController.aperture * 0.00001;
  96. dofPass.maxblur.value = effectController.maxblur;
  97. }
  98. const gui = new GUI();
  99. gui.add( effectController, 'focus', 10.0, 3000.0, 10 ).onChange( updateEffect );
  100. gui.add( effectController, 'aperture', 0, 10, 0.1 ).onChange( updateEffect );
  101. gui.add( effectController, 'maxblur', 0.0, 0.01, 0.001 ).onChange( updateEffect );
  102. updateEffect();
  103. }
  104. function onPointerMove( event ) {
  105. if ( event.isPrimary === false ) return;
  106. mouseX = event.clientX - windowHalfX;
  107. mouseY = event.clientY - windowHalfY;
  108. }
  109. function onWindowResize() {
  110. windowHalfX = window.innerWidth / 2;
  111. windowHalfY = window.innerHeight / 2;
  112. width = window.innerWidth;
  113. height = window.innerHeight;
  114. camera.aspect = width / height;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( width, height );
  117. }
  118. function animate() {
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  124. camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
  125. camera.lookAt( scene.position );
  126. postProcessing.render();
  127. }
  128. </script>
  129. </body>
  130. </html>