misc_animation_groups.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - groups</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 - groups
  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. // all objects of this animation group share a common animation state
  27. var animationGroup = new THREE.AnimationObjectGroup();
  28. //
  29. var geometry = new THREE.BoxBufferGeometry( 5, 5, 5 );
  30. var material = new THREE.MeshBasicMaterial( { transparent: true } );
  31. //
  32. for ( var i = 0; i < 5; i ++ ) {
  33. for ( var j = 0; j < 5; j ++ ) {
  34. var mesh = new THREE.Mesh( geometry, material );
  35. mesh.position.x = 32 - ( 16 * i );
  36. mesh.position.y = 0;
  37. mesh.position.z = 32 - ( 16 * j );
  38. scene.add( mesh );
  39. animationGroup.add( mesh );
  40. }
  41. }
  42. // create some keyframe tracks
  43. var xAxis = new THREE.Vector3( 1, 0, 0 );
  44. var qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
  45. var qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
  46. 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 ] );
  47. var colorKF = new THREE.ColorKeyframeTrack( '.material.color', [ 0, 1, 2 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ], THREE.InterpolateDiscrete );
  48. var opacityKF = new THREE.NumberKeyframeTrack( '.material.opacity', [ 0, 1, 2 ], [ 1, 0, 1 ] );
  49. // create clip
  50. var clip = new THREE.AnimationClip( 'default', 3, [ quaternionKF, colorKF, opacityKF ] );
  51. // apply the animation group to the mixer as the root object
  52. mixer = new THREE.AnimationMixer( animationGroup );
  53. var clipAction = mixer.clipAction( clip );
  54. clipAction.play();
  55. //
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. document.body.appendChild( renderer.domElement );
  60. //
  61. stats = new Stats();
  62. document.body.appendChild( stats.dom );
  63. //
  64. clock = new THREE.Clock();
  65. //
  66. window.addEventListener( 'resize', onWindowResize, false );
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. render();
  76. }
  77. function render() {
  78. var delta = clock.getDelta();
  79. if ( mixer ) {
  80. mixer.update( delta );
  81. }
  82. renderer.render( scene, camera );
  83. stats.update();
  84. }
  85. </script>
  86. </body>
  87. </html>