webgl_animation_keyframes.html 3.5 KB

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