webgl_morphtargets_horse.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - horse</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. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - horse<br/>
  21. model by <a href="https://mirada.com/" target="_blank" rel="noopener">mirada</a> from <a href="http://www.ro.me" target="_blank" rel="noopener">rome</a>
  22. </div>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. let container, stats;
  39. let camera, scene, renderer;
  40. let mesh, mixer;
  41. const radius = 600;
  42. let theta = 0;
  43. let prevTime = Date.now();
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. //
  50. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.y = 300;
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xf0f0f0 );
  54. //
  55. const light1 = new THREE.DirectionalLight( 0xefefff, 5 );
  56. light1.position.set( 1, 1, 1 ).normalize();
  57. scene.add( light1 );
  58. const light2 = new THREE.DirectionalLight( 0xffefef, 5 );
  59. light2.position.set( - 1, - 1, - 1 ).normalize();
  60. scene.add( light2 );
  61. const loader = new GLTFLoader();
  62. loader.load( 'models/gltf/Horse.glb', function ( gltf ) {
  63. mesh = gltf.scene.children[ 0 ];
  64. mesh.scale.set( 1.5, 1.5, 1.5 );
  65. scene.add( mesh );
  66. mixer = new THREE.AnimationMixer( mesh );
  67. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  68. } );
  69. //
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.useLegacyLights = false;
  74. container.appendChild( renderer.domElement );
  75. //
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. //
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. render();
  90. stats.update();
  91. }
  92. function render() {
  93. theta += 0.1;
  94. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  95. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  96. camera.lookAt( 0, 150, 0 );
  97. if ( mixer ) {
  98. const time = Date.now();
  99. mixer.update( ( time - prevTime ) * 0.001 );
  100. prevTime = time;
  101. }
  102. renderer.render( scene, camera );
  103. }
  104. </script>
  105. </body>
  106. </html>