webgl_morphtargets_horse.html 3.5 KB

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