webgl_instancing_morph.html 4.0 KB

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