webgl_morphtargets_horse.html 3.2 KB

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