webgpu_postprocessing_ssaa.html 5.3 KB

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