webgpu_postprocessing_smaa.html 3.9 KB

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