webgpu_multisampled_renderbuffers.html 4.6 KB

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