2
0

misc_animation_groups.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <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="info">
  29. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - animation - groups
  30. </div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/WebGL.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( WEBGL.isWebGLAvailable() === false ) {
  36. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  37. }
  38. var stats, clock;
  39. var scene, camera, renderer, mixer;
  40. init();
  41. animate();
  42. function init() {
  43. scene = new THREE.Scene();
  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. // all objects of this animation group share a common animation state
  49. var animationGroup = new THREE.AnimationObjectGroup();
  50. //
  51. var geometry = new THREE.BoxBufferGeometry( 5, 5, 5 );
  52. var material = new THREE.MeshBasicMaterial( { transparent: true } );
  53. //
  54. for ( var i = 0; i < 5; i ++ ) {
  55. for ( var j = 0; j < 5; j ++ ) {
  56. var mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.x = 32 - ( 16 * i );
  58. mesh.position.y = 0;
  59. mesh.position.z = 32 - ( 16 * j );
  60. scene.add( mesh );
  61. animationGroup.add( mesh );
  62. }
  63. }
  64. // create some keyframe tracks
  65. var xAxis = new THREE.Vector3( 1, 0, 0 );
  66. var qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
  67. var qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
  68. 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 ] );
  69. var colorKF = new THREE.ColorKeyframeTrack( '.material.color', [ 0, 1, 2 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ], THREE.InterpolateDiscrete );
  70. var opacityKF = new THREE.NumberKeyframeTrack( '.material.opacity', [ 0, 1, 2 ], [ 1, 0, 1 ] );
  71. // create clip
  72. var clip = new THREE.AnimationClip( 'default', 3, [ quaternionKF, colorKF, opacityKF ] );
  73. // apply the animation group to the mixer as the root object
  74. mixer = new THREE.AnimationMixer( animationGroup );
  75. var clipAction = mixer.clipAction( clip );
  76. clipAction.play();
  77. //
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. document.body.appendChild( renderer.domElement );
  82. //
  83. stats = new Stats();
  84. document.body.appendChild( stats.dom );
  85. //
  86. clock = new THREE.Clock();
  87. //
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function animate() {
  96. requestAnimationFrame( animate );
  97. render();
  98. }
  99. function render() {
  100. var delta = clock.getDelta();
  101. if ( mixer ) {
  102. mixer.update( delta );
  103. }
  104. renderer.render( scene, camera );
  105. stats.update();
  106. }
  107. </script>
  108. </body>
  109. </html>