webgpu_instancing_morph.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - instancing morph target animations</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. let camera, scene, renderer, stats, mesh, mixer, dummy;
  24. const offset = 5000;
  25. const timeOffsets = new Float32Array( 1024 );
  26. for ( let i = 0; i < 1024; i ++ ) {
  27. timeOffsets[ i ] = Math.random() * 3;
  28. }
  29. const clock = new THREE.Clock( true );
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x99DDFF );
  35. scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );
  36. //
  37. stats = new Stats();
  38. document.body.appendChild( stats.dom );
  39. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  40. light.position.set( 200, 1000, 50 );
  41. light.shadow.mapSize.width = 2048;
  42. light.shadow.mapSize.height = 2048;
  43. light.castShadow = true;
  44. light.shadow.camera.left = - 5000;
  45. light.shadow.camera.right = 5000;
  46. light.shadow.camera.top = 5000;
  47. light.shadow.camera.bottom = - 5000;
  48. light.shadow.camera.far = 2000;
  49. light.shadow.bias = - 0.01;
  50. light.shadow.camera.updateProjectionMatrix();
  51. scene.add( light );
  52. const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );
  53. scene.add( hemi );
  54. const ground = new THREE.Mesh(
  55. new THREE.PlaneGeometry( 1000000, 1000000 ),
  56. new THREE.MeshStandardMaterial( { color: 0x669933 } )
  57. );
  58. ground.rotation.x = - Math.PI / 2;
  59. ground.receiveShadow = true;
  60. scene.add( ground );
  61. const loader = new GLTFLoader();
  62. loader.load( 'models/gltf/Horse.glb', function ( glb ) {
  63. dummy = glb.scene.children[ 0 ];
  64. mesh = new THREE.InstancedMesh( dummy.geometry, new THREE.MeshStandardNodeMaterial( {
  65. flatShading: true,
  66. } ), 1024 );
  67. mesh.castShadow = true;
  68. for ( let x = 0, i = 0; x < 32; x ++ ) {
  69. for ( let y = 0; y < 32; y ++ ) {
  70. dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );
  71. dummy.updateMatrix();
  72. mesh.setMatrixAt( i, dummy.matrix );
  73. mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );
  74. i ++;
  75. }
  76. }
  77. scene.add( mesh );
  78. mixer = new THREE.AnimationMixer( glb.scene );
  79. const action = mixer.clipAction( glb.animations[ 0 ] );
  80. action.play();
  81. } );
  82. // renderer
  83. renderer = new THREE.WebGPURenderer( { antialias: true } );
  84. renderer.setAnimationLoop( animate );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. document.body.appendChild( renderer.domElement );
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. //
  96. function animate() {
  97. const time = clock.getElapsedTime();
  98. const r = 3000;
  99. camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r );
  100. camera.lookAt( 0, 0, 0 );
  101. if ( mesh ) {
  102. for ( let i = 0; i < 1024; i ++ ) {
  103. mixer.setTime( time + timeOffsets[ i ] );
  104. mesh.setMorphAt( i, dummy );
  105. }
  106. mesh.morphTexture.needsUpdate = true;
  107. }
  108. renderer.render( scene, camera );
  109. stats.update();
  110. }
  111. </script>
  112. </body>
  113. </html>