webgpu_multisampled_renderbuffers.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - multisampled renderbuffers</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <meta property="og:title" content="three.js webgpu - multisampled renderbuffers">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_multisampled_renderbuffers.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_multisampled_renderbuffers.jpg">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Multisampled Renderbuffers</span>
  17. </div>
  18. <small>
  19. Using multisampled renderbuffers for anti-aliasing.
  20. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { texture } from 'three/tsl';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. let camera, scene, renderer;
  37. const mouse = new THREE.Vector2();
  38. let quadMesh, renderTarget;
  39. let box, box2;
  40. const dpr = 1;
  41. const params = {
  42. animated: true,
  43. multisampling: true
  44. };
  45. const mat4 = new THREE.Matrix4();
  46. const count = 50;
  47. const fullRadius = 20; // Radius of the sphere
  48. const halfRadius = 10; // Radius of the sphere
  49. const positions = new Array( count ).fill().map( ( _, i ) => {
  50. const radius = ( i % 2 === 0 ) ? fullRadius : halfRadius;
  51. const phi = Math.acos( 2 * Math.random() - 1 ) - Math.PI / 2; // phi: latitude, range -π/2 to π/2
  52. const theta = 2 * Math.PI * Math.random(); // theta: longitude, range 0 to 2π
  53. return new THREE.Vector3(
  54. radius * Math.cos( phi ) * Math.cos( theta ), // x
  55. radius * Math.sin( phi ), // y
  56. radius * Math.cos( phi ) * Math.sin( theta ) // z
  57. );
  58. } );
  59. init();
  60. initGUI();
  61. function initGUI() {
  62. const gui = renderer.inspector.createParameters( 'Settings' );
  63. gui.add( params, 'multisampling' );
  64. gui.add( params, 'animated' );
  65. }
  66. function init() {
  67. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 50 );
  68. camera.position.z = 3;
  69. scene = new THREE.Scene();
  70. scene.background = new THREE.Color( 0x111111 );
  71. // textured mesh
  72. const geometryBox = new THREE.BoxGeometry( 7, 7, 7, 12, 12, 12 );
  73. const materialBox = new THREE.MeshBasicNodeMaterial();
  74. const materialBoxInner = new THREE.MeshBasicNodeMaterial( { color: 0xff0000 } );
  75. materialBox.wireframe = true;
  76. //
  77. box = new THREE.InstancedMesh( geometryBox, materialBox, count );
  78. box2 = new THREE.InstancedMesh( geometryBox, materialBoxInner, count );
  79. for ( let i = 0; i < count; i ++ ) {
  80. box.setMatrixAt( i, mat4.identity().setPosition( positions[ i ] ) );
  81. box2.setMatrixAt( i, mat4.multiplyScalar( 0.996 ).setPosition( positions[ i ] ) );
  82. }
  83. scene.add( box, box2 );
  84. //
  85. renderer = new THREE.WebGPURenderer( { antialias: true } );
  86. renderer.inspector = new Inspector();
  87. renderer.setPixelRatio( dpr );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setAnimationLoop( animate );
  90. document.body.appendChild( renderer.domElement );
  91. renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr, {
  92. samples: params.multisampling ? 4 : 1,
  93. depthBuffer: true,
  94. } );
  95. window.addEventListener( 'mousemove', onWindowMouseMove );
  96. window.addEventListener( 'resize', onWindowResize );
  97. // FX
  98. // modulate the final color based on the mouse position
  99. const materialFX = new THREE.MeshBasicNodeMaterial();
  100. materialFX.colorNode = texture( renderTarget.texture ).rgb;
  101. quadMesh = new THREE.QuadMesh( materialFX );
  102. }
  103. function onWindowMouseMove( e ) {
  104. mouse.x = e.offsetX / window.innerWidth;
  105. mouse.y = e.offsetY / window.innerHeight;
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
  112. }
  113. function animate() {
  114. if ( params.animated ) {
  115. box.rotation.x += 0.001;
  116. box.rotation.y += 0.002;
  117. box2.rotation.x += 0.001;
  118. box2.rotation.y += 0.002;
  119. }
  120. renderTarget.samples = params.multisampling ? 4 : 1;
  121. renderer.setRenderTarget( renderTarget );
  122. renderer.render( scene, camera );
  123. renderer.setRenderTarget( null );
  124. quadMesh.render( renderer );
  125. }
  126. </script>
  127. </body>
  128. </html>
粤ICP备19079148号