webgl_morphtargets_horse.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. var container, stats;
  28. var camera, scene, renderer;
  29. var mesh, mixer;
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. //
  36. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.y = 300;
  38. camera.target = new THREE.Vector3( 0, 150, 0 );
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0xf0f0f0 );
  41. //
  42. var light = new THREE.DirectionalLight( 0xefefff, 1.5 );
  43. light.position.set( 1, 1, 1 ).normalize();
  44. scene.add( light );
  45. var light = new THREE.DirectionalLight( 0xffefef, 1.5 );
  46. light.position.set( - 1, - 1, - 1 ).normalize();
  47. scene.add( light );
  48. var loader = new GLTFLoader();
  49. loader.load( "models/gltf/Horse.glb", function ( gltf ) {
  50. mesh = gltf.scene.children[ 0 ];
  51. mesh.scale.set( 1.5, 1.5, 1.5 );
  52. scene.add( mesh );
  53. mixer = new THREE.AnimationMixer( mesh );
  54. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  55. } );
  56. //
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.outputEncoding = THREE.sRGBEncoding;
  61. container.appendChild( renderer.domElement );
  62. //
  63. stats = new Stats();
  64. container.appendChild( stats.dom );
  65. //
  66. window.addEventListener( 'resize', onWindowResize, false );
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. //
  74. function animate() {
  75. requestAnimationFrame( animate );
  76. render();
  77. stats.update();
  78. }
  79. var radius = 600;
  80. var theta = 0;
  81. var prevTime = Date.now();
  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. var 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>