webgpu_instance_mesh.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Instance Mesh</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Instance Mesh
  12. </div>
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three-nodes/": "./jsm/nodes/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  26. import { normalWorld } from 'three-nodes/Nodes.js';
  27. import WebGPU from './jsm/capabilities/WebGPU.js';
  28. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  29. let camera, scene, renderer, stats;
  30. let mesh;
  31. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  32. const count = Math.pow( amount, 3 );
  33. const dummy = new THREE.Object3D();
  34. init().then( animate ).catch( error );
  35. async function init() {
  36. if ( WebGPU.isAvailable() === false ) {
  37. document.body.appendChild( WebGPU.getErrorMessage() );
  38. throw new Error( 'No WebGPU support' );
  39. }
  40. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  41. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  42. camera.lookAt( 0, 0, 0 );
  43. scene = new THREE.Scene();
  44. const material = new THREE.MeshBasicMaterial();
  45. material.colorNode = normalWorld;
  46. const loader = new THREE.BufferGeometryLoader();
  47. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  48. geometry.computeVertexNormals();
  49. geometry.scale( 0.5, 0.5, 0.5 );
  50. mesh = new THREE.InstancedMesh( geometry, material, count );
  51. scene.add( mesh );
  52. //
  53. const gui = new GUI();
  54. gui.add( mesh, 'count', 0, count );
  55. } );
  56. //
  57. renderer = new WebGPURenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. document.body.appendChild( renderer.domElement );
  61. //
  62. stats = new Stats();
  63. document.body.appendChild( stats.dom );
  64. //
  65. window.addEventListener( 'resize', onWindowResize );
  66. return renderer.init();
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. //
  74. function animate() {
  75. requestAnimationFrame( animate );
  76. render();
  77. stats.update();
  78. }
  79. function render() {
  80. if ( mesh ) {
  81. const time = Date.now() * 0.001;
  82. mesh.rotation.x = Math.sin( time / 4 );
  83. mesh.rotation.y = Math.sin( time / 2 );
  84. let i = 0;
  85. const offset = ( amount - 1 ) / 2;
  86. for ( let x = 0; x < amount; x ++ ) {
  87. for ( let y = 0; y < amount; y ++ ) {
  88. for ( let z = 0; z < amount; z ++ ) {
  89. dummy.position.set( offset - x, offset - y, offset - z );
  90. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  91. dummy.rotation.z = dummy.rotation.y * 2;
  92. dummy.updateMatrix();
  93. mesh.setMatrixAt( i ++, dummy.matrix );
  94. }
  95. }
  96. }
  97. }
  98. renderer.render( scene, camera );
  99. }
  100. function error( error ) {
  101. console.error( error );
  102. }
  103. </script>
  104. </body>
  105. </html>