webgl_morphtargets_horse.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - horse</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. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/libs/stats.min.js"></script>
  19. <script src="js/AnimationClipCreator.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. var mesh, mixer;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - morph targets - horse. model by <a href="http://mirada.com/">mirada</a> from <a href="http://ro.me">rome</a>';
  35. container.appendChild( info );
  36. //
  37. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  38. camera.position.y = 300;
  39. camera.target = new THREE.Vector3( 0, 150, 0 );
  40. scene = new THREE.Scene();
  41. //
  42. var light = new THREE.DirectionalLight( 0xefefff, 2 );
  43. light.position.set( 1, 1, 1 ).normalize();
  44. scene.add( light );
  45. var light = new THREE.DirectionalLight( 0xffefef, 2 );
  46. light.position.set( -1, -1, -1 ).normalize();
  47. scene.add( light );
  48. var loader = new THREE.JSONLoader();
  49. loader.load( "models/animated/horse.js", function( geometry ) {
  50. mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
  51. mesh.scale.set( 1.5, 1.5, 1.5 );
  52. scene.add( mesh );
  53. mixer = new THREE.AnimationMixer( mesh );
  54. var clip = THREE.AnimationClip.CreateFromMorphTargetSequence( 'gallop', geometry.morphTargets, 30 );
  55. mixer.addAction( new THREE.AnimationAction( clip ).warpToDuration( 1.5 ) );
  56. } );
  57. //
  58. renderer = new THREE.WebGLRenderer();
  59. renderer.setClearColor( 0xf0f0f0 );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. container.appendChild(renderer.domElement);
  63. //
  64. stats = new Stats();
  65. stats.domElement.style.position = 'absolute';
  66. stats.domElement.style.top = '0px';
  67. container.appendChild( stats.domElement );
  68. //
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. //
  77. function animate() {
  78. requestAnimationFrame( animate );
  79. render();
  80. stats.update();
  81. }
  82. var radius = 600;
  83. var theta = 0;
  84. var prevTime = Date.now();
  85. function render() {
  86. theta += 0.1;
  87. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  88. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  89. camera.lookAt( camera.target );
  90. if ( mixer ) {
  91. var time = Date.now();
  92. mixer.update( ( time - prevTime ) * 0.001 );
  93. prevTime = time;
  94. }
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>