webgl_morphtargets_horse.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.js"></script>
  18. <script src="js/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. var mesh;
  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://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>';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.y = 300;
  38. camera.useTarget = true;
  39. camera.target.position.y = 150;
  40. scene = new THREE.Scene();
  41. var light = new THREE.DirectionalLight( 0xefefff, 2 );
  42. light.position.x = 1;
  43. light.position.y = 1;
  44. light.position.z = 1;
  45. light.position.normalize();
  46. scene.add( light );
  47. var light = new THREE.DirectionalLight( 0xffefef, 2 );
  48. light.position.x = - 1;
  49. light.position.y = - 1;
  50. light.position.z = - 1;
  51. light.position.normalize();
  52. scene.add( light );
  53. var loader = new THREE.JSONLoader( true );
  54. loader.load( { model: "models/animated/horse.js", callback: function( geometry ) {
  55. mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0x606060, morphTargets: true } ) );
  56. mesh.scale.set( 1.5, 1.5, 1.5 );
  57. scene.add( mesh );
  58. } } );
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.sortObjects = false;
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. container.appendChild(renderer.domElement);
  63. stats = new Stats();
  64. stats.domElement.style.position = 'absolute';
  65. stats.domElement.style.top = '0px';
  66. container.appendChild( stats.domElement );
  67. }
  68. //
  69. function animate() {
  70. requestAnimationFrame( animate );
  71. render();
  72. stats.update();
  73. }
  74. var radius = 600;
  75. var theta = 0;
  76. var duration = 1000;
  77. var keyframes = 15 /*16*/, interpolation = duration / keyframes;
  78. var lastKeyframe = 0, currentKeyframe = 0;
  79. function render() {
  80. theta += 0.2;
  81. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  82. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  83. if ( mesh ) {
  84. // Alternate morph targets
  85. var time = new Date().getTime() % duration;
  86. var keyframe = Math.floor( time / interpolation ) + 1;
  87. if ( keyframe != currentKeyframe ) {
  88. mesh.morphTargetInfluences[ lastKeyframe ] = 0;
  89. mesh.morphTargetInfluences[ currentKeyframe ] = 1;
  90. mesh.morphTargetInfluences[ keyframe ] = 0;
  91. lastKeyframe = currentKeyframe;
  92. currentKeyframe = keyframe;
  93. // console.log( mesh.morphTargetInfluences );
  94. }
  95. mesh.morphTargetInfluences[ keyframe ] = ( time % interpolation ) / interpolation;
  96. mesh.morphTargetInfluences[ lastKeyframe ] = 1 - mesh.morphTargetInfluences[ keyframe ];
  97. }
  98. renderer.render( scene, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>