1
0

webgpu_postprocessing_ssaa.html 5.4 KB

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