1
0

webgpu_instance_mesh.html 3.6 KB

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