webgpu_skinning_instancing.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Skinning Instancing
  12. </div>
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three-nodes/": "./jsm/nodes/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import * as Nodes from 'three-nodes/Nodes.js';
  25. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  26. import WebGPU from './jsm/capabilities/WebGPU.js';
  27. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  28. let camera, scene, renderer;
  29. let mixer, clock;
  30. init().then( animate ).catch( error );
  31. async function init() {
  32. if ( WebGPU.isAvailable() === false ) {
  33. document.body.appendChild( WebGPU.getErrorMessage() );
  34. throw new Error( 'No WebGPU support' );
  35. }
  36. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  37. camera.position.set( 100, 200, 300 );
  38. scene = new THREE.Scene();
  39. camera.lookAt( 0, 100, 0 );
  40. clock = new THREE.Clock();
  41. //lights
  42. const centerLight = new THREE.PointLight( 0xff9900, .8, 7000 );
  43. centerLight.position.y = 450;
  44. centerLight.position.z = - 200;
  45. scene.add( centerLight );
  46. const cameraLight = new THREE.PointLight( 0x0099ff, .7, 7000 );
  47. camera.add( cameraLight );
  48. scene.add( camera );
  49. const loader = new FBXLoader();
  50. loader.load( 'models/fbx/Samba Dancing.fbx', ( object ) => {
  51. mixer = new THREE.AnimationMixer( object );
  52. const action = mixer.clipAction( object.animations[ 0 ] );
  53. action.play();
  54. const instanceCount = 30;
  55. const dummy = new THREE.Object3D();
  56. object.traverse( ( child ) => {
  57. if ( child.isMesh ) {
  58. child.material = new Nodes.MeshStandardNodeMaterial();
  59. child.material.roughness = .1;
  60. child.isInstancedMesh = true;
  61. child.instanceMatrix = new THREE.InstancedBufferAttribute( new Float32Array( instanceCount * 16 ), 16 );
  62. child.count = instanceCount;
  63. for ( let i = 0; i < instanceCount; i ++ ) {
  64. dummy.position.x = - 200 + ( ( i % 5 ) * 70 );
  65. dummy.position.z = Math.floor( i / 5 ) * - 200;
  66. dummy.updateMatrix();
  67. dummy.matrix.toArray( child.instanceMatrix.array, i * 16 );
  68. }
  69. }
  70. } );
  71. scene.add( object );
  72. } );
  73. //renderer
  74. renderer = new WebGPURenderer();
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.outputEncoding = THREE.sRGBEncoding;
  78. renderer.toneMappingNode = new Nodes.ToneMappingNode( THREE.LinearToneMapping, 800 );
  79. document.body.appendChild( renderer.domElement );
  80. window.addEventListener( 'resize', onWindowResize );
  81. return renderer.init();
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. const delta = clock.getDelta();
  91. if ( mixer ) mixer.update( delta );
  92. renderer.render( scene, camera );
  93. }
  94. function error( error ) {
  95. console.error( error );
  96. }
  97. </script>
  98. </body>
  99. </html>