misc_animation_keys.html 3.9 KB

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