1
0

webgpu_instancing_morph.html 4.5 KB

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