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: #e2e0e0;
  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://artstation.com/artwork/1AGwX" target="_blank" rel="noopener">Littlest Tokyo</a> by
  23. <a href="https://artstation.com/glenatron" target="_blank" rel="noopener">Glen Fox</a>, CC Attribution.
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { Sky } from 'three/addons/objects/Sky.js';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  39. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  40. let mixer;
  41. const clock = new THREE.Clock();
  42. const container = document.getElementById( 'container' );
  43. const stats = new Stats();
  44. container.appendChild( stats.dom );
  45. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  49. container.appendChild( renderer.domElement );
  50. const scene = new THREE.Scene();
  51. // Sky
  52. const sky = new Sky();
  53. const uniforms = sky.material.uniforms;
  54. uniforms[ 'turbidity' ].value = 0;
  55. uniforms[ 'rayleigh' ].value = 3;
  56. uniforms[ 'mieDirectionalG' ].value = 0.7;
  57. uniforms[ 'cloudElevation' ].value = 1;
  58. uniforms[ 'sunPosition' ].value.set( - 0.8, 0.19, 0.56 ); // elevation: 11, azimuth: -55
  59. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  60. const environment = pmremGenerator.fromScene( sky ).texture;
  61. scene.background = environment;
  62. scene.environment = environment;
  63. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  64. camera.position.set( 5, 2, 8 );
  65. const controls = new OrbitControls( camera, renderer.domElement );
  66. controls.enableDamping = true;
  67. controls.target.set( 0, 0.7, 0 );
  68. controls.update();
  69. const dracoLoader = new DRACOLoader();
  70. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  71. const loader = new GLTFLoader();
  72. loader.setDRACOLoader( dracoLoader );
  73. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  74. const model = gltf.scene;
  75. model.position.set( 1, 1, 0 );
  76. model.scale.set( 0.01, 0.01, 0.01 );
  77. scene.add( model );
  78. mixer = new THREE.AnimationMixer( model );
  79. mixer.clipAction( gltf.animations[ 0 ] ).play();
  80. renderer.setAnimationLoop( animate );
  81. }, undefined, function ( e ) {
  82. console.error( e );
  83. } );
  84. window.onresize = function () {
  85. camera.aspect = window.innerWidth / window.innerHeight;
  86. camera.updateProjectionMatrix();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. };
  89. function animate() {
  90. const delta = clock.getDelta();
  91. mixer.update( delta );
  92. controls.update();
  93. stats.update();
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>
粤ICP备19079148号