webgpu_postprocessing_dof.html 4.9 KB

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