misc_animation_groups.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from './jsm/libs/stats.module.js';
  26. let stats, clock;
  27. let scene, camera, renderer, mixer;
  28. init();
  29. animate();
  30. function init() {
  31. scene = new THREE.Scene();
  32. //
  33. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  34. camera.position.set( 50, 50, 100 );
  35. camera.lookAt( scene.position );
  36. // all objects of this animation group share a common animation state
  37. const animationGroup = new THREE.AnimationObjectGroup();
  38. //
  39. const geometry = new THREE.BoxGeometry( 5, 5, 5 );
  40. const material = new THREE.MeshBasicMaterial( { transparent: true } );
  41. //
  42. for ( let i = 0; i < 5; i ++ ) {
  43. for ( let j = 0; j < 5; j ++ ) {
  44. const mesh = new THREE.Mesh( geometry, material );
  45. mesh.position.x = 32 - ( 16 * i );
  46. mesh.position.y = 0;
  47. mesh.position.z = 32 - ( 16 * j );
  48. scene.add( mesh );
  49. animationGroup.add( mesh );
  50. }
  51. }
  52. // create some keyframe tracks
  53. const xAxis = new THREE.Vector3( 1, 0, 0 );
  54. const qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
  55. const qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
  56. const 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 ] );
  57. const colorKF = new THREE.ColorKeyframeTrack( '.material.color', [ 0, 1, 2 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ], THREE.InterpolateDiscrete );
  58. const opacityKF = new THREE.NumberKeyframeTrack( '.material.opacity', [ 0, 1, 2 ], [ 1, 0, 1 ] );
  59. // create clip
  60. const clip = new THREE.AnimationClip( 'default', 3, [ quaternionKF, colorKF, opacityKF ] );
  61. // apply the animation group to the mixer as the root object
  62. mixer = new THREE.AnimationMixer( animationGroup );
  63. const clipAction = mixer.clipAction( clip );
  64. clipAction.play();
  65. //
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. document.body.appendChild( renderer.domElement );
  70. //
  71. stats = new Stats();
  72. document.body.appendChild( stats.dom );
  73. //
  74. clock = new THREE.Clock();
  75. //
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. }
  87. function render() {
  88. const delta = clock.getDelta();
  89. if ( mixer ) {
  90. mixer.update( delta );
  91. }
  92. renderer.render( scene, camera );
  93. stats.update();
  94. }
  95. </script>
  96. </body>
  97. </html>