1
0

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