webgl_morphtargets_horse.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. <meta property="og:title" content="three.js webgl - morph targets - horse">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_morphtargets_horse.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_morphtargets_horse.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #f0f0f0;
  15. color: #444;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - horse<br/>
  25. 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>
  26. </div>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../build/three.module.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three';
  37. import Stats from 'three/addons/libs/stats.module.js';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  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. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. //
  50. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.y = 300;
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xf0f0f0 );
  54. //
  55. const light1 = new THREE.DirectionalLight( 0xefefff, 5 );
  56. light1.position.set( 1, 1, 1 ).normalize();
  57. scene.add( light1 );
  58. const light2 = new THREE.DirectionalLight( 0xffefef, 5 );
  59. light2.position.set( - 1, - 1, - 1 ).normalize();
  60. scene.add( light2 );
  61. const loader = new GLTFLoader();
  62. loader.load( 'models/gltf/Horse.glb', function ( gltf ) {
  63. mesh = gltf.scene.children[ 0 ];
  64. mesh.scale.set( 1.5, 1.5, 1.5 );
  65. scene.add( mesh );
  66. mixer = new THREE.AnimationMixer( mesh );
  67. mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 1 ).play();
  68. } );
  69. //
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( animate );
  74. container.appendChild( renderer.domElement );
  75. //
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. //
  87. function animate() {
  88. render();
  89. stats.update();
  90. }
  91. function render() {
  92. theta += 0.1;
  93. camera.position.x = radius * Math.sin( THREE.MathUtils.degToRad( theta ) );
  94. camera.position.z = radius * Math.cos( THREE.MathUtils.degToRad( theta ) );
  95. camera.lookAt( 0, 150, 0 );
  96. if ( mixer ) {
  97. const time = Date.now();
  98. mixer.update( ( time - prevTime ) * 0.001 );
  99. prevTime = time;
  100. }
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>
粤ICP备19079148号