misc_animation_keys.html 4.1 KB

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