webgpu_instance_mesh.html 3.7 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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Instancing</span>
  14. </div>
  15. <small>
  16. Instanced rendering via InstancedMesh.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { mix, range, normalWorld, oscSine, time } from 'three/tsl';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. let camera, scene, renderer;
  34. let mesh;
  35. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  36. const count = Math.pow( amount, 3 );
  37. const dummy = new THREE.Object3D();
  38. init();
  39. function init() {
  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. // random colors between instances from 0x000000 to 0xFFFFFF
  46. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  47. material.colorNode = mix( normalWorld, randomColors, oscSine( time.mul( .1 ) ) );
  48. const loader = new THREE.BufferGeometryLoader();
  49. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  50. geometry.computeVertexNormals();
  51. geometry.scale( 0.5, 0.5, 0.5 );
  52. mesh = new THREE.InstancedMesh( geometry, material, count );
  53. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  54. scene.add( mesh );
  55. //
  56. const gui = renderer.inspector.createParameters( 'Settings' );
  57. gui.add( mesh, 'count', 1, count, 1 ).name( 'instance count' );
  58. } );
  59. //
  60. renderer = new THREE.WebGPURenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. renderer.inspector = new Inspector();
  65. document.body.appendChild( renderer.domElement );
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. }
  74. //
  75. function animate() {
  76. render();
  77. }
  78. async function render() {
  79. if ( mesh ) {
  80. const time = Date.now() * 0.001;
  81. mesh.rotation.x = Math.sin( time / 4 );
  82. mesh.rotation.y = Math.sin( time / 2 );
  83. let i = 0;
  84. const offset = ( amount - 1 ) / 2;
  85. for ( let x = 0; x < amount; x ++ ) {
  86. for ( let y = 0; y < amount; y ++ ) {
  87. for ( let z = 0; z < amount; z ++ ) {
  88. dummy.position.set( offset - x, offset - y, offset - z );
  89. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  90. dummy.rotation.z = dummy.rotation.y * 2;
  91. dummy.updateMatrix();
  92. mesh.setMatrixAt( i ++, dummy.matrix );
  93. }
  94. }
  95. }
  96. }
  97. await renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>
粤ICP备19079148号