webgpu_instancing_morph.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. <meta property="og:title" content="three.js webgpu - instancing morph target animations">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_instancing_morph.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_instancing_morph.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Instancing Morph</span>
  18. </div>
  19. <small>
  20. Rendering instances which are individually animated via morph targets.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. let camera, scene, renderer, mesh, mixer, dummy;
  38. const offset = 5000;
  39. const timeOffsets = new Float32Array( 1024 );
  40. for ( let i = 0; i < 1024; i ++ ) {
  41. timeOffsets[ i ] = Math.random() * 3;
  42. }
  43. const timer = new THREE.Timer();
  44. timer.connect( document );
  45. init();
  46. function init() {
  47. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0x99DDFF );
  50. scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );
  51. //
  52. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  53. light.position.set( 200, 1000, 50 );
  54. light.shadow.mapSize.width = 2048;
  55. light.shadow.mapSize.height = 2048;
  56. light.castShadow = true;
  57. light.shadow.camera.left = - 5000;
  58. light.shadow.camera.right = 5000;
  59. light.shadow.camera.top = 5000;
  60. light.shadow.camera.bottom = - 5000;
  61. light.shadow.camera.far = 2000;
  62. light.shadow.camera.updateProjectionMatrix();
  63. scene.add( light );
  64. const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );
  65. scene.add( hemi );
  66. const ground = new THREE.Mesh(
  67. new THREE.PlaneGeometry( 1000000, 1000000 ),
  68. new THREE.MeshStandardMaterial( { color: 0x669933 } )
  69. );
  70. ground.rotation.x = - Math.PI / 2;
  71. ground.receiveShadow = true;
  72. scene.add( ground );
  73. const loader = new GLTFLoader();
  74. loader.load( 'models/gltf/Horse.glb', function ( glb ) {
  75. dummy = glb.scene.children[ 0 ];
  76. mesh = new THREE.InstancedMesh( dummy.geometry, new THREE.MeshStandardNodeMaterial( {
  77. flatShading: true,
  78. } ), 1024 );
  79. mesh.castShadow = true;
  80. for ( let x = 0, i = 0; x < 32; x ++ ) {
  81. for ( let y = 0; y < 32; y ++ ) {
  82. dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );
  83. dummy.updateMatrix();
  84. mesh.setMatrixAt( i, dummy.matrix );
  85. mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );
  86. i ++;
  87. }
  88. }
  89. scene.add( mesh );
  90. mixer = new THREE.AnimationMixer( glb.scene );
  91. const action = mixer.clipAction( glb.animations[ 0 ] );
  92. action.play();
  93. } );
  94. // renderer
  95. renderer = new THREE.WebGPURenderer( { antialias: true } );
  96. renderer.setAnimationLoop( animate );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.shadowMap.enabled = true;
  100. renderer.inspector = new Inspector();
  101. document.body.appendChild( renderer.domElement );
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. //
  110. function animate() {
  111. timer.update();
  112. const time = timer.getElapsed();
  113. const r = 3000;
  114. camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r );
  115. camera.lookAt( 0, 0, 0 );
  116. if ( mesh ) {
  117. for ( let i = 0; i < 1024; i ++ ) {
  118. mixer.setTime( time + timeOffsets[ i ] );
  119. mesh.setMorphAt( i, dummy );
  120. }
  121. mesh.morphTexture.needsUpdate = true;
  122. }
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>
粤ICP备19079148号