webgl_animation_keyframes.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - keyframes</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: #bfe3dd;
  11. color: #000;
  12. }
  13. a {
  14. color: #2983ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - animation - keyframes<br/>
  22. Model: <a href="https://www.artstation.com/artwork/1AGwX" target="_blank" rel="noopener">Littlest Tokyo</a> by
  23. <a href="https://www.artstation.com/glenatron" target="_blank" rel="noopener">Glen Fox</a>, CC Attribution.
  24. </div>
  25. <script type="module">
  26. import * as THREE from '../build/three.module.js';
  27. import Stats from './jsm/libs/stats.module.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { RoomEnvironment } from './jsm/environments/RoomEnvironment.js';
  30. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  31. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  32. let mixer;
  33. const clock = new THREE.Clock();
  34. const container = document.getElementById( 'container' );
  35. const stats = new Stats();
  36. container.appendChild( stats.dom );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. renderer.outputEncoding = THREE.sRGBEncoding;
  41. container.appendChild( renderer.domElement );
  42. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  43. const scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xbfe3dd );
  45. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  46. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  47. camera.position.set( 5, 2, 8 );
  48. const controls = new OrbitControls( camera, renderer.domElement );
  49. controls.target.set( 0, 0.5, 0 );
  50. controls.update();
  51. controls.enablePan = false;
  52. controls.enableDamping = true;
  53. const dracoLoader = new DRACOLoader();
  54. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  55. const loader = new GLTFLoader();
  56. loader.setDRACOLoader( dracoLoader );
  57. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  58. const model = gltf.scene;
  59. model.position.set( 1, 1, 0 );
  60. model.scale.set( 0.01, 0.01, 0.01 );
  61. scene.add( model );
  62. mixer = new THREE.AnimationMixer( model );
  63. mixer.clipAction( gltf.animations[ 0 ] ).play();
  64. animate();
  65. }, undefined, function ( e ) {
  66. console.error( e );
  67. } );
  68. window.onresize = function () {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. };
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. const delta = clock.getDelta();
  76. mixer.update( delta );
  77. controls.update();
  78. stats.update();
  79. renderer.render( scene, camera );
  80. }
  81. </script>
  82. </body>
  83. </html>