webgl_morphtargets_horse.html 3.1 KB

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