misc_animation_keys.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. <style>
  8. body {
  9. color: #fff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #f00;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank">three.js</a> webgl - animation - basic use
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var stats, clock;
  38. var scene, camera, renderer, mixer;
  39. init();
  40. animate();
  41. function init() {
  42. scene = new THREE.Scene();
  43. var container = document.getElementById( 'container' );
  44. //
  45. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.set( 50, 50, 100 );
  47. camera.lookAt( scene.position );
  48. //
  49. var axisHelper = new THREE.AxisHelper( 10 );
  50. scene.add( axisHelper );
  51. //
  52. var geometry = new THREE.BoxBufferGeometry( 5, 5, 5 );
  53. var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
  54. var mesh = new THREE.Mesh( geometry, material );
  55. scene.add( mesh );
  56. // create a keyframe track (i.e. a timed sequence of keyframes) for each animated property
  57. // Note: the keyframe track type should correspond to the type of the property being animated
  58. // POSITION
  59. var positionKF = new THREE.VectorKeyframeTrack('.position', [0, 1, 2], [0, 0, 0, 30, 0, 0, 0, 0, 0]);
  60. // SCALE
  61. var scaleKF = new THREE.VectorKeyframeTrack('.scale', [0, 1, 2], [1, 1, 1, 2, 2, 2, 1, 1, 1]);
  62. // ROTATION
  63. // Rotation should be performed using quaternions, using a QuaternionKeyframeTrack
  64. // Interpolating Euler angles (.rotation property) can be problematic and is currently not supported
  65. // rotation about x axis
  66. var xAxis = new THREE.Vector3( 1, 0, 0 );
  67. var qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
  68. var qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
  69. 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 ]);
  70. // COLOR - Not yet implemented!
  71. // var colorKF = new THREE.ColorKeyframeTrack('.material.color', [0, 1, 2], [0xffff00, 0xffffff, 0xffff00])
  72. // create an animation sequence with the tracks
  73. // If a negative time value is passed, the duration will be calculated from the times of the passed tracks array
  74. var clip = new THREE.AnimationClip( 'Action', -1, [scaleKF, positionKF, quaternionKF] );
  75. // setup the AnimationMixer and play the animation sequence
  76. mixer = new THREE.AnimationMixer( mesh );
  77. // create a ClipAction and set it to play
  78. var clipAction = mixer.clipAction( clip );
  79. clipAction.play();
  80. //
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.setClearColor( 0x555555 );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. container.appendChild( renderer.domElement );
  86. //
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. //
  90. clock = new THREE.Clock()
  91. //
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. };
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. };
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. render();
  102. };
  103. function render() {
  104. var delta = clock.getDelta();
  105. if ( mixer ) {
  106. mixer.update( delta );
  107. }
  108. renderer.render( scene, camera );
  109. stats.update();
  110. };
  111. </script>
  112. </body>
  113. </html>