webgpu_instance_mesh.html 3.5 KB

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