webgpu_instance_mesh.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { mix, range, normalWorld, oscSine, timerLocal } from 'three/nodes';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  29. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  30. let camera, scene, renderer, stats;
  31. let mesh;
  32. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  33. const count = Math.pow( amount, 3 );
  34. const dummy = new THREE.Object3D();
  35. init();
  36. function init() {
  37. if ( WebGPU.isAvailable() === false ) {
  38. document.body.appendChild( WebGPU.getErrorMessage() );
  39. throw new Error( 'No WebGPU support' );
  40. }
  41. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  42. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  43. camera.lookAt( 0, 0, 0 );
  44. scene = new THREE.Scene();
  45. const material = new THREE.MeshBasicMaterial();
  46. // random colors between instances from 0x000000 to 0xFFFFFF
  47. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  48. material.colorNode = mix( normalWorld, randomColors, oscSine( timerLocal( .1 ) ) );
  49. const loader = new THREE.BufferGeometryLoader();
  50. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  51. geometry.computeVertexNormals();
  52. geometry.scale( 0.5, 0.5, 0.5 );
  53. mesh = new THREE.InstancedMesh( geometry, material, count );
  54. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  55. scene.add( mesh );
  56. //
  57. const gui = new GUI();
  58. gui.add( mesh, 'count', 0, count );
  59. } );
  60. //
  61. renderer = new WebGPURenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. document.body.appendChild( renderer.domElement );
  66. //
  67. stats = new Stats();
  68. document.body.appendChild( stats.dom );
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. //
  78. function animate() {
  79. render();
  80. stats.update();
  81. }
  82. async function render() {
  83. if ( mesh ) {
  84. const time = Date.now() * 0.001;
  85. mesh.rotation.x = Math.sin( time / 4 );
  86. mesh.rotation.y = Math.sin( time / 2 );
  87. let i = 0;
  88. const offset = ( amount - 1 ) / 2;
  89. for ( let x = 0; x < amount; x ++ ) {
  90. for ( let y = 0; y < amount; y ++ ) {
  91. for ( let z = 0; z < amount; z ++ ) {
  92. dummy.position.set( offset - x, offset - y, offset - z );
  93. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  94. dummy.rotation.z = dummy.rotation.y * 2;
  95. dummy.updateMatrix();
  96. mesh.setMatrixAt( i ++, dummy.matrix );
  97. }
  98. }
  99. }
  100. }
  101. await renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>