webgpu_multisampled_renderbuffers.html 4.3 KB

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