webgpu_postprocessing_smaa.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing smaa</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - post-processing SMAA
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass } from 'three/tsl';
  25. import { smaa } from 'three/addons/tsl/display/SMAANode.js';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. let camera, scene, renderer, postProcessing, stats;
  29. const params = {
  30. enabled: true,
  31. autoRotate: true
  32. };
  33. init();
  34. function init() {
  35. renderer = new THREE.WebGPURenderer();
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. renderer.setAnimationLoop( animate );
  39. document.body.appendChild( renderer.domElement );
  40. stats = new Stats();
  41. document.body.appendChild( stats.dom );
  42. //
  43. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.z = 300;
  45. scene = new THREE.Scene();
  46. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  47. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  48. const mesh1 = new THREE.Mesh( geometry, material1 );
  49. mesh1.position.x = - 100;
  50. scene.add( mesh1 );
  51. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  52. texture.colorSpace = THREE.SRGBColorSpace;
  53. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  54. const mesh2 = new THREE.Mesh( geometry, material2 );
  55. mesh2.position.x = 100;
  56. scene.add( mesh2 );
  57. // post processing
  58. postProcessing = new THREE.PostProcessing( renderer );
  59. const scenePass = pass( scene, camera );
  60. const smaaPass = smaa( scenePass );
  61. postProcessing.outputNode = smaaPass;
  62. //
  63. window.addEventListener( 'resize', onWindowResize );
  64. const gui = new GUI();
  65. const smaaFolder = gui.addFolder( 'SMAA' );
  66. smaaFolder.add( params, 'enabled' ).onChange( ( value ) => {
  67. if ( value === true ) {
  68. postProcessing.outputNode = smaaPass;
  69. } else {
  70. postProcessing.outputNode = scenePass;
  71. }
  72. postProcessing.needsUpdate = true;
  73. } );
  74. const sceneFolder = gui.addFolder( 'Scene' );
  75. sceneFolder.add( params, 'autoRotate' );
  76. }
  77. function onWindowResize() {
  78. const width = window.innerWidth;
  79. const height = window.innerHeight;
  80. camera.aspect = width / height;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( width, height );
  83. }
  84. function animate() {
  85. stats.begin();
  86. if ( params.autoRotate === true ) {
  87. for ( let i = 0; i < scene.children.length; i ++ ) {
  88. const child = scene.children[ i ];
  89. child.rotation.x += 0.005;
  90. child.rotation.y += 0.01;
  91. }
  92. }
  93. postProcessing.render();
  94. stats.end();
  95. }
  96. </script>
  97. </body>
  98. </html>
粤ICP备19079148号