1
0

webgl_instancing_morph.html 4.4 KB

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