misc_animation_keys.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - basic</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 - basic">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_animation_keys.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_animation_keys.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - animation - basic use
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. let stats, timer;
  29. let scene, camera, renderer, mixer;
  30. init();
  31. function init() {
  32. scene = new THREE.Scene();
  33. //
  34. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  35. camera.position.set( 25, 25, 50 );
  36. camera.lookAt( scene.position );
  37. //
  38. const axesHelper = new THREE.AxesHelper( 10 );
  39. scene.add( axesHelper );
  40. //
  41. const geometry = new THREE.BoxGeometry( 5, 5, 5 );
  42. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, transparent: true } );
  43. const mesh = new THREE.Mesh( geometry, material );
  44. scene.add( mesh );
  45. // create a keyframe track (i.e. a timed sequence of keyframes) for each animated property
  46. // Note: the keyframe track type should correspond to the type of the property being animated
  47. // POSITION
  48. const positionKF = new THREE.VectorKeyframeTrack( '.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] );
  49. // SCALE
  50. const scaleKF = new THREE.VectorKeyframeTrack( '.scale', [ 0, 1, 2 ], [ 1, 1, 1, 2, 2, 2, 1, 1, 1 ] );
  51. // ROTATION
  52. // Rotation should be performed using quaternions, using a THREE.QuaternionKeyframeTrack
  53. // Interpolating Euler angles (.rotation property) can be problematic and is currently not supported
  54. // set up rotation about x axis
  55. const xAxis = new THREE.Vector3( 1, 0, 0 );
  56. const qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
  57. const qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
  58. const quaternionKF = new THREE.QuaternionKeyframeTrack( '.quaternion', [ 0, 1, 2 ], [ qInitial.x, qInitial.y, qInitial.z, qInitial.w, qFinal.x, qFinal.y, qFinal.z, qFinal.w, qInitial.x, qInitial.y, qInitial.z, qInitial.w ] );
  59. // COLOR
  60. const colorKF = new THREE.ColorKeyframeTrack( '.material.color', [ 0, 1, 2 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ], THREE.InterpolateDiscrete );
  61. // OPACITY
  62. const opacityKF = new THREE.NumberKeyframeTrack( '.material.opacity', [ 0, 1, 2 ], [ 1, 0, 1 ] );
  63. // create an animation sequence with the tracks
  64. // If a negative time value is passed, the duration will be calculated from the times of the passed tracks array
  65. const clip = new THREE.AnimationClip( 'Action', 3, [ scaleKF, positionKF, quaternionKF, colorKF, opacityKF ] );
  66. // setup the THREE.AnimationMixer
  67. mixer = new THREE.AnimationMixer( mesh );
  68. // create a ClipAction and set it to play
  69. const clipAction = mixer.clipAction( clip );
  70. clipAction.play();
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.setAnimationLoop( animate );
  76. document.body.appendChild( renderer.domElement );
  77. //
  78. stats = new Stats();
  79. document.body.appendChild( stats.dom );
  80. //
  81. timer = new THREE.Timer();
  82. timer.connect( document );
  83. //
  84. window.addEventListener( 'resize', onWindowResize );
  85. }
  86. function onWindowResize() {
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function animate() {
  92. timer.update();
  93. const delta = timer.getDelta();
  94. if ( mixer ) {
  95. mixer.update( delta );
  96. }
  97. renderer.render( scene, camera );
  98. stats.update();
  99. }
  100. </script>
  101. </body>
  102. </html>
粤ICP备19079148号