webgpu_postprocessing_smaa.html 3.4 KB

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