webgl_morphtargets_horse.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from './jsm/libs/stats.module.js';
  36. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  37. let container, stats;
  38. let camera, scene, renderer;
  39. let mesh, mixer;
  40. const radius = 600;
  41. let theta = 0;
  42. let prevTime = Date.now();
  43. init();
  44. animate();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. //
  49. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  50. camera.position.y = 300;
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0xf0f0f0 );
  53. //
  54. const light1 = new THREE.DirectionalLight( 0xefefff, 1.5 );
  55. light1.position.set( 1, 1, 1 ).normalize();
  56. scene.add( light1 );
  57. const light2 = new THREE.DirectionalLight( 0xffefef, 1.5 );
  58. light2.position.set( - 1, - 1, - 1 ).normalize();
  59. scene.add( light2 );
  60. const loader = new GLTFLoader();
  61. loader.load( 'models/gltf/Horse.glb', function ( gltf ) {
  62. mesh = gltf.scene.children[ 0 ];
  63. mesh.scale.set( 1.5, 1.5, 1.5 );
  64. scene.add( mesh );
  65. mixer = new THREE.AnimationMixer( mesh );
  66. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  67. } );
  68. //
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.outputEncoding = THREE.sRGBEncoding;
  73. container.appendChild( renderer.domElement );
  74. //
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. //
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. render();
  89. stats.update();
  90. }
  91. function render() {
  92. theta += 0.1;
  93. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  94. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  95. camera.lookAt( 0, 150, 0 );
  96. if ( mixer ) {
  97. const time = Date.now();
  98. mixer.update( ( time - prevTime ) * 0.001 );
  99. prevTime = time;
  100. }
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>