webgpu_instance_mesh.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
  9. <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
  10. </head>
  11. <body>
  12. <div id="info">
  13. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Instance Mesh
  14. </div>
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three-nodes/": "./jsm/nodes/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  28. import { normalWorld } from 'three-nodes/Nodes.js';
  29. import WebGPU from './jsm/capabilities/WebGPU.js';
  30. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  31. let camera, scene, renderer, stats;
  32. let mesh;
  33. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  34. const count = Math.pow( amount, 3 );
  35. const dummy = new THREE.Object3D();
  36. init().then( animate ).catch( error );
  37. async function init() {
  38. if ( WebGPU.isAvailable() === false ) {
  39. document.body.appendChild( WebGPU.getErrorMessage() );
  40. throw new Error( 'No WebGPU support' );
  41. }
  42. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  43. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  44. camera.lookAt( 0, 0, 0 );
  45. scene = new THREE.Scene();
  46. const material = new THREE.MeshBasicMaterial();
  47. material.colorNode = normalWorld;
  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. scene.add( mesh );
  54. //
  55. const gui = new GUI();
  56. gui.add( mesh, 'count', 0, count );
  57. } );
  58. //
  59. renderer = new WebGPURenderer();
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. document.body.appendChild( renderer.domElement );
  63. //
  64. stats = new Stats();
  65. document.body.appendChild( stats.dom );
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. return renderer.init();
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. //
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. render();
  79. stats.update();
  80. }
  81. function render() {
  82. if ( mesh ) {
  83. const time = Date.now() * 0.001;
  84. mesh.rotation.x = Math.sin( time / 4 );
  85. mesh.rotation.y = Math.sin( time / 2 );
  86. let i = 0;
  87. const offset = ( amount - 1 ) / 2;
  88. for ( let x = 0; x < amount; x ++ ) {
  89. for ( let y = 0; y < amount; y ++ ) {
  90. for ( let z = 0; z < amount; z ++ ) {
  91. dummy.position.set( offset - x, offset - y, offset - z );
  92. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  93. dummy.rotation.z = dummy.rotation.y * 2;
  94. dummy.updateMatrix();
  95. mesh.setMatrixAt( i ++, dummy.matrix );
  96. }
  97. }
  98. }
  99. }
  100. renderer.render( scene, camera );
  101. }
  102. function error( error ) {
  103. console.error( error );
  104. }
  105. </script>
  106. </body>
  107. </html>