webgl_morphtargets_horse.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - horse</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #f0f0f0;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="../build/Three.js"></script>
  17. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  18. <script type="text/javascript" src="js/Stats.js"></script>
  19. <script type="text/javascript">
  20. var container, stats;
  21. var camera, scene, projector, renderer;
  22. var mesh;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. var info = document.createElement( 'div' );
  29. info.style.position = 'absolute';
  30. info.style.top = '10px';
  31. info.style.width = '100%';
  32. info.style.textAlign = 'center';
  33. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" 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>';
  34. container.appendChild( info );
  35. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.y = 300;
  37. camera.target.position.y = 150;
  38. scene = new THREE.Scene();
  39. var light = new THREE.DirectionalLight( 0xefefff, 2 );
  40. light.position.x = 1;
  41. light.position.y = 1;
  42. light.position.z = 1;
  43. light.position.normalize();
  44. scene.addLight( light );
  45. var light = new THREE.DirectionalLight( 0xffefef, 2 );
  46. light.position.x = - 1;
  47. light.position.y = - 1;
  48. light.position.z = - 1;
  49. light.position.normalize();
  50. scene.addLight( light );
  51. var loader = new THREE.JSONLoader( true );
  52. loader.load( { model: "models/animated/horse.js", callback: function( geometry ) {
  53. mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
  54. mesh.scale.set( 1.5, 1.5, 1.5 );
  55. scene.addObject( mesh );
  56. } } );
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.sortObjects = false;
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. container.appendChild(renderer.domElement);
  61. stats = new Stats();
  62. stats.domElement.style.position = 'absolute';
  63. stats.domElement.style.top = '0px';
  64. container.appendChild( stats.domElement );
  65. }
  66. //
  67. function animate() {
  68. requestAnimationFrame( animate );
  69. render();
  70. stats.update();
  71. }
  72. var radius = 600;
  73. var theta = 0;
  74. var duration = 1000;
  75. var keyframes = 15 /*16*/, interpolation = duration / keyframes;
  76. var lastKeyframe = 0, currentKeyframe = 0;
  77. function render() {
  78. theta += 0.2;
  79. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  80. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  81. if ( mesh ) {
  82. // Alternate morph targets
  83. var time = new Date().getTime() % duration;
  84. var keyframe = Math.floor( time / interpolation ) + 1;
  85. if ( keyframe != currentKeyframe ) {
  86. mesh.morphTargetInfluences[ lastKeyframe ] = 0;
  87. mesh.morphTargetInfluences[ currentKeyframe ] = 1;
  88. mesh.morphTargetInfluences[ keyframe ] = 0;
  89. lastKeyframe = currentKeyframe;
  90. currentKeyframe = keyframe;
  91. // console.log( mesh.morphTargetInfluences );
  92. }
  93. mesh.morphTargetInfluences[ keyframe ] = ( time % interpolation ) / interpolation;
  94. mesh.morphTargetInfluences[ lastKeyframe ] = 1 - mesh.morphTargetInfluences[ keyframe ];
  95. }
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>