webgpu_skinning_instancing.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Skinning Instancing</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 - Skinning Instancing
  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 * as Nodes from 'three-nodes/Nodes.js';
  27. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  28. import WebGPU from './jsm/capabilities/WebGPU.js';
  29. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  30. let camera, scene, renderer;
  31. let mixer, clock;
  32. init().then( animate ).catch( error );
  33. async function init() {
  34. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU support' );
  37. }
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  39. camera.position.set( 100, 200, 300 );
  40. scene = new THREE.Scene();
  41. camera.lookAt( 0, 100, 0 );
  42. clock = new THREE.Clock();
  43. //lights
  44. const centerLight = new THREE.PointLight( 0xffffff, .8, 7000 );
  45. centerLight.position.y = 450;
  46. centerLight.position.z = - 200;
  47. scene.add( centerLight );
  48. const cameraLight = new THREE.PointLight( 0x0099ff, .7, 7000 );
  49. camera.add( cameraLight );
  50. scene.add( camera );
  51. const loader = new FBXLoader();
  52. loader.load( 'models/fbx/Samba Dancing.fbx', ( object ) => {
  53. mixer = new THREE.AnimationMixer( object );
  54. const action = mixer.clipAction( object.animations[ 0 ] );
  55. action.play();
  56. const instanceCount = 30;
  57. const dummy = new THREE.Object3D();
  58. object.traverse( ( child ) => {
  59. if ( child.isMesh ) {
  60. child.material = new Nodes.MeshStandardNodeMaterial();
  61. child.isInstancedMesh = true;
  62. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  63. child.count = instanceCount;
  64. for ( let i = 0; i < instanceCount; i ++ ) {
  65. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  66. dummy.position.z = Math.floor( i / 5 ) * - 200;
  67. dummy.updateMatrix();
  68. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  69. }
  70. }
  71. } );
  72. scene.add( object );
  73. } );
  74. //renderer
  75. renderer = new WebGPURenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. document.body.appendChild( renderer.domElement );
  79. window.addEventListener( 'resize', onWindowResize );
  80. return renderer.init();
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. const delta = clock.getDelta();
  90. if ( mixer ) mixer.update( delta );
  91. renderer.render( scene, camera );
  92. }
  93. function error( error ) {
  94. console.error( error );
  95. }
  96. </script>
  97. </body>
  98. </html>