1
0

webgpu_postprocessing_ssaa.html 5.4 KB

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