2
0

webgl_animation_keyframes.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  30. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  31. let mixer;
  32. const clock = new THREE.Clock();
  33. const container = document.getElementById( 'container' );
  34. const stats = new Stats();
  35. container.appendChild( stats.dom );
  36. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.outputEncoding = THREE.sRGBEncoding;
  40. container.appendChild( renderer.domElement );
  41. const scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xbfe3dd );
  43. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  44. camera.position.set( 5, 2, 8 );
  45. const controls = new OrbitControls( camera, renderer.domElement );
  46. controls.target.set( 0, 0.5, 0 );
  47. controls.update();
  48. controls.enablePan = false;
  49. controls.enableDamping = true;
  50. scene.add( new THREE.HemisphereLight( 0xffffff, 0x000000, 0.4 ) );
  51. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  52. dirLight.position.set( 5, 2, 8 );
  53. scene.add( dirLight );
  54. // envmap
  55. const path = 'textures/cube/Park2/';
  56. const format = '.jpg';
  57. const envMap = new THREE.CubeTextureLoader().load( [
  58. path + 'posx' + format, path + 'negx' + format,
  59. path + 'posy' + format, path + 'negy' + format,
  60. path + 'posz' + format, path + 'negz' + format
  61. ] );
  62. const dracoLoader = new DRACOLoader();
  63. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  64. const loader = new GLTFLoader();
  65. loader.setDRACOLoader( dracoLoader );
  66. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  67. const model = gltf.scene;
  68. model.position.set( 1, 1, 0 );
  69. model.scale.set( 0.01, 0.01, 0.01 );
  70. model.traverse( function ( child ) {
  71. if ( child.isMesh ) child.material.envMap = envMap;
  72. } );
  73. scene.add( model );
  74. mixer = new THREE.AnimationMixer( model );
  75. mixer.clipAction( gltf.animations[ 0 ] ).play();
  76. animate();
  77. }, undefined, function ( e ) {
  78. console.error( e );
  79. } );
  80. window.onresize = function () {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. };
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. const delta = clock.getDelta();
  88. mixer.update( delta );
  89. controls.update();
  90. stats.update();
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>