1
0

webgpu_instance_mesh.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. <meta property="og:title" content="three.js webgpu - instance mesh">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_instance_mesh.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_instance_mesh.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Instancing</span>
  18. </div>
  19. <small>
  20. Instanced rendering via InstancedMesh.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { mix, range, normalWorld, oscSine, time } from 'three/tsl';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. let camera, scene, renderer;
  38. let mesh;
  39. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  40. const count = Math.pow( amount, 3 );
  41. const dummy = new THREE.Object3D();
  42. init();
  43. function init() {
  44. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  45. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  46. camera.lookAt( 0, 0, 0 );
  47. scene = new THREE.Scene();
  48. const material = new THREE.MeshBasicMaterial();
  49. // random colors between instances from 0x000000 to 0xFFFFFF
  50. const randomColors = range( new THREE.Color( 0x000000 ), new THREE.Color( 0xFFFFFF ) );
  51. material.colorNode = mix( normalWorld, randomColors, oscSine( time.mul( .1 ) ) );
  52. const loader = new THREE.BufferGeometryLoader();
  53. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  54. geometry.computeVertexNormals();
  55. geometry.scale( 0.5, 0.5, 0.5 );
  56. mesh = new THREE.InstancedMesh( geometry, material, count );
  57. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  58. scene.add( mesh );
  59. //
  60. const gui = renderer.inspector.createParameters( 'Settings' );
  61. gui.add( mesh, 'count', 1, count, 1 ).name( 'instance count' );
  62. } );
  63. //
  64. renderer = new THREE.WebGPURenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. renderer.inspector = new Inspector();
  69. document.body.appendChild( renderer.domElement );
  70. //
  71. window.addEventListener( 'resize', onWindowResize );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. //
  79. function animate() {
  80. render();
  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>
粤ICP备19079148号