webgpu_postprocessing_ssaa.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing ssaa</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 ssaa">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_ssaa.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_ssaa.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>SSAA</span>
  18. </div>
  19. <small>Unbiased Manual Supersampling Anti-Aliasing (SSAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>.</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 { ssaaPass } from 'three/addons/tsl/display/SSAAPassNode.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let scene, mesh, renderer, renderPipeline;
  36. let camera, ssaaRenderPass;
  37. let timer;
  38. const params = {
  39. sampleLevel: 3,
  40. camera: 'perspective',
  41. clearColor: 'black',
  42. clearAlpha: 1.0,
  43. viewOffsetX: 0,
  44. autoRotate: true
  45. };
  46. init();
  47. function init() {
  48. const width = window.innerWidth;
  49. const height = window.innerHeight;
  50. renderer = new THREE.WebGPURenderer();
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.setAnimationLoop( animate );
  54. renderer.inspector = new Inspector();
  55. document.body.appendChild( renderer.domElement );
  56. timer = new THREE.Timer();
  57. timer.connect( document );
  58. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  59. camera.position.z = 7;
  60. camera.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
  61. scene = new THREE.Scene();
  62. const group = new THREE.Group();
  63. scene.add( group );
  64. const light = new THREE.PointLight( 0xefffef, 500 );
  65. light.position.z = 10;
  66. light.position.y = - 10;
  67. light.position.x = - 10;
  68. scene.add( light );
  69. const light2 = new THREE.PointLight( 0xffefef, 500 );
  70. light2.position.z = 10;
  71. light2.position.x = - 10;
  72. light2.position.y = 10;
  73. scene.add( light2 );
  74. const light3 = new THREE.PointLight( 0xefefff, 500 );
  75. light3.position.z = 10;
  76. light3.position.x = 10;
  77. light3.position.y = - 10;
  78. scene.add( light3 );
  79. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  80. scene.add( light4 );
  81. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  82. const material = new THREE.MeshStandardMaterial();
  83. mesh = new THREE.InstancedMesh( geometry, material, 120 );
  84. const dummy = new THREE.Mesh();
  85. const color = new THREE.Color();
  86. for ( let i = 0; i < mesh.count; i ++ ) {
  87. dummy.position.x = Math.random() * 4 - 2;
  88. dummy.position.y = Math.random() * 4 - 2;
  89. dummy.position.z = Math.random() * 4 - 2;
  90. dummy.rotation.x = Math.random();
  91. dummy.rotation.y = Math.random();
  92. dummy.rotation.z = Math.random();
  93. dummy.scale.setScalar( Math.random() * 0.2 + 0.05 );
  94. dummy.updateMatrix();
  95. color.setHSL( Math.random(), 1.0, 0.3 );
  96. mesh.setMatrixAt( i, dummy.matrix );
  97. mesh.setColorAt( i, color );
  98. }
  99. scene.add( mesh );
  100. // postprocessing
  101. renderPipeline = new THREE.RenderPipeline( renderer );
  102. ssaaRenderPass = ssaaPass( scene, camera );
  103. const scenePassColor = ssaaRenderPass.getTextureNode();
  104. renderPipeline.outputNode = scenePassColor;
  105. window.addEventListener( 'resize', onWindowResize );
  106. // GUI
  107. const gui = renderer.inspector.createParameters( 'Settings' );
  108. gui.add( params, 'sampleLevel', {
  109. 'Level 0: 1 Sample': 0,
  110. 'Level 1: 2 Samples': 1,
  111. 'Level 2: 4 Samples': 2,
  112. 'Level 3: 8 Samples': 3,
  113. 'Level 4: 16 Samples': 4,
  114. 'Level 5: 32 Samples': 5
  115. } );
  116. gui.add( params, 'clearColor', [ 'black', 'white', 'blue', 'green', 'red' ] );
  117. gui.add( params, 'clearAlpha', 0, 1 );
  118. gui.add( params, 'viewOffsetX', - 100, 100 );
  119. gui.add( params, 'autoRotate' );
  120. }
  121. function onWindowResize() {
  122. const width = window.innerWidth;
  123. const height = window.innerHeight;
  124. camera.aspect = width / height;
  125. camera.setViewOffset( width, height, params.viewOffsetX, 0, width, height );
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( width, height );
  128. }
  129. function animate() {
  130. timer.update();
  131. if ( params.autoRotate ) {
  132. const delta = timer.getDelta();
  133. mesh.rotation.x += delta * 0.25;
  134. mesh.rotation.y += delta * 0.5;
  135. }
  136. let newColor;
  137. switch ( params.clearColor ) {
  138. case 'blue': newColor = 0x0000ff; break;
  139. case 'red': newColor = 0xff0000; break;
  140. case 'green': newColor = 0x00ff00; break;
  141. case 'white': newColor = 0xffffff; break;
  142. case 'black': newColor = 0x000000; break;
  143. }
  144. renderer.setClearColor( newColor, params.clearAlpha );
  145. ssaaRenderPass.sampleLevel = params.sampleLevel;
  146. camera.view.offsetX = params.viewOffsetX;
  147. renderPipeline.render();
  148. }
  149. </script>
  150. </body>
  151. </html>
粤ICP备19079148号