misc_animation_keys.html 4.0 KB

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