webgpu_multisampled_renderbuffers.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.module.js",
  16. "three/addons/": "./jsm/",
  17. "three/nodes": "./jsm/nodes/Nodes.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { texture, MeshBasicNodeMaterial, MeshPhongNodeMaterial } from 'three/nodes';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. import WebGL from 'three/addons/capabilities/WebGL.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. import QuadMesh from 'three/addons/objects/QuadMesh.js';
  29. let camera, scene, renderer;
  30. const mouse = new THREE.Vector2();
  31. let quadMesh, renderTarget;
  32. let box, box2;
  33. const dpr = 1;
  34. const params = {
  35. animated: true,
  36. samples: 4
  37. };
  38. const mat4 = new THREE.Matrix4();
  39. const count = 50;
  40. const fullRadius = 20; // Radius of the sphere
  41. const halfRadius = 10; // Radius of the sphere
  42. const positions = new Array( count ).fill().map( ( _, i ) => {
  43. const radius = ( i % 2 === 0 ) ? fullRadius : halfRadius;
  44. const phi = Math.acos( 2 * Math.random() - 1 ) - Math.PI / 2; // phi: latitude, range -π/2 to π/2
  45. const theta = 2 * Math.PI * Math.random(); // theta: longitude, range 0 to 2π
  46. return new THREE.Vector3(
  47. radius * Math.cos( phi ) * Math.cos( theta ), // x
  48. radius * Math.sin( phi ), // y
  49. radius * Math.cos( phi ) * Math.sin( theta ) // z
  50. );
  51. } );
  52. initGUI();
  53. init();
  54. function initGUI() {
  55. const gui = new GUI();
  56. gui.add( params, 'samples', 0, 4 ).step( 1 );
  57. gui.add( params, 'animated', true );
  58. }
  59. function init() {
  60. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  61. document.body.appendChild( WebGPU.getErrorMessage() );
  62. throw new Error( 'No WebGPU or WebGL2 support' );
  63. }
  64. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 50 );
  65. camera.position.z = 3;
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0x111111 );
  68. // textured mesh
  69. const geometryBox = new THREE.BoxGeometry( 7, 7, 7, 12, 12, 12 );
  70. const materialBox = new MeshPhongNodeMaterial();
  71. const materialBoxInner = new MeshPhongNodeMaterial( { color: 0xff0000 } );
  72. materialBox.wireframe = true;
  73. //
  74. box = new THREE.InstancedMesh( geometryBox, materialBox, count );
  75. box2 = new THREE.InstancedMesh( geometryBox, materialBoxInner, count );
  76. for ( let i = 0; i < count; i ++ ) {
  77. box.setMatrixAt( i, mat4.identity().setPosition( positions[ i ] ) );
  78. box2.setMatrixAt( i, mat4.multiplyScalar( 0.996 ).setPosition( positions[ i ] ) );
  79. }
  80. scene.add( box, box2 );
  81. //
  82. renderer = new WebGPURenderer( { antialias: true } );
  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 MeshBasicNodeMaterial();
  96. materialFX.colorNode = texture( renderTarget.texture ).rgb;
  97. quadMesh = new 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>