webgpu_instancing_morph.html 4.2 KB

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