webgl_animation_keyframes.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. <meta property="og:title" content="three.js webgl - animation - keyframes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_animation_keyframes.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_animation_keyframes.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #e2e0e0;
  15. color: #000;
  16. }
  17. a {
  18. color: #2983ff;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="container"></div>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - animation - keyframes<br/>
  26. Model: <a href="https://artstation.com/artwork/1AGwX" target="_blank" rel="noopener">Littlest Tokyo</a> by
  27. <a href="https://artstation.com/glenatron" target="_blank" rel="noopener">Glen Fox</a>, CC Attribution.
  28. </div>
  29. <script type="importmap">
  30. {
  31. "imports": {
  32. "three": "../build/three.module.js",
  33. "three/addons/": "./jsm/"
  34. }
  35. }
  36. </script>
  37. <script type="module">
  38. import * as THREE from 'three';
  39. import Stats from 'three/addons/libs/stats.module.js';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { Sky } from 'three/addons/objects/Sky.js';
  42. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  43. import { DRACOLoader, DRACO_GLTF_CONFIG } from 'three/addons/loaders/DRACOLoader.js';
  44. let mixer;
  45. const timer = new THREE.Timer();
  46. timer.connect( document );
  47. const container = document.getElementById( 'container' );
  48. const stats = new Stats();
  49. container.appendChild( stats.dom );
  50. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  54. container.appendChild( renderer.domElement );
  55. const scene = new THREE.Scene();
  56. // Sky
  57. const sky = new Sky();
  58. sky.scale.setScalar( 10000 );
  59. scene.add( sky );
  60. const uniforms = sky.material.uniforms;
  61. uniforms[ 'turbidity' ].value = 0;
  62. uniforms[ 'rayleigh' ].value = 3;
  63. uniforms[ 'mieDirectionalG' ].value = 0.7;
  64. uniforms[ 'cloudElevation' ].value = 1;
  65. uniforms[ 'sunPosition' ].value.set( - 0.8, 0.19, 0.56 ); // elevation: 11, azimuth: -55
  66. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  67. const environment = pmremGenerator.fromScene( sky ).texture;
  68. scene.environment = environment;
  69. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  70. camera.position.set( 5, 2, 8 );
  71. const controls = new OrbitControls( camera, renderer.domElement );
  72. controls.enableDamping = true;
  73. controls.target.set( 0, 0.7, 0 );
  74. controls.update();
  75. const dracoLoader = new DRACOLoader();
  76. dracoLoader.setDecoderPath( DRACO_GLTF_CONFIG );
  77. const loader = new GLTFLoader();
  78. loader.setDRACOLoader( dracoLoader );
  79. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  80. const model = gltf.scene;
  81. model.position.set( 1, 1, 0 );
  82. model.scale.set( 0.01, 0.01, 0.01 );
  83. scene.add( model );
  84. mixer = new THREE.AnimationMixer( model );
  85. mixer.clipAction( gltf.animations[ 0 ] ).play();
  86. renderer.setAnimationLoop( animate );
  87. }, undefined, function ( e ) {
  88. console.error( e );
  89. } );
  90. window.onresize = function () {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. };
  95. function animate() {
  96. timer.update();
  97. const delta = timer.getDelta();
  98. mixer.update( delta );
  99. controls.update();
  100. stats.update();
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>
粤ICP备19079148号