webgl_morphtargets_horse.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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="http://mirada.com/">mirada</a> from <a href="http://ro.me">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. camera.target = new THREE.Vector3( 0, 150, 0 );
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0xf0f0f0 );
  44. //
  45. const light1 = new THREE.DirectionalLight( 0xefefff, 1.5 );
  46. light1.position.set( 1, 1, 1 ).normalize();
  47. scene.add( light1 );
  48. const light2 = new THREE.DirectionalLight( 0xffefef, 1.5 );
  49. light2.position.set( - 1, - 1, - 1 ).normalize();
  50. scene.add( light2 );
  51. const loader = new GLTFLoader();
  52. loader.load( "models/gltf/Horse.glb", function ( gltf ) {
  53. mesh = gltf.scene.children[ 0 ];
  54. mesh.scale.set( 1.5, 1.5, 1.5 );
  55. scene.add( mesh );
  56. mixer = new THREE.AnimationMixer( mesh );
  57. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  58. } );
  59. //
  60. renderer = new THREE.WebGLRenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.outputEncoding = THREE.sRGBEncoding;
  64. container.appendChild( renderer.domElement );
  65. //
  66. stats = new Stats();
  67. container.appendChild( stats.dom );
  68. //
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. //
  77. function animate() {
  78. requestAnimationFrame( animate );
  79. render();
  80. stats.update();
  81. }
  82. function render() {
  83. theta += 0.1;
  84. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  85. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  86. camera.lookAt( camera.target );
  87. if ( mixer ) {
  88. const time = Date.now();
  89. mixer.update( ( time - prevTime ) * 0.001 );
  90. prevTime = time;
  91. }
  92. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>