webgpu_multisampled_renderbuffers.html 4.3 KB

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