webgl_morphtargets_sphere.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. padding: 5px;
  19. font-family:Monospace;
  20. font-size:13px;
  21. text-align:center;
  22. color: #ffffff;
  23. }
  24. a {
  25. color: #ffffff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGL morph target example
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script src="js/controls/OrbitControls.js"></script>
  37. <script src="js/loaders/GLTFLoader.js"></script>
  38. <script>
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var geometry, objects;
  42. var mesh;
  43. var sign = 1;
  44. init();
  45. animate();
  46. function init() {
  47. container = document.getElementById( 'container' );
  48. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.2, 100 );
  49. camera.position.set( 0, 5, 5 );
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x222222 );
  52. scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
  53. var light = new THREE.PointLight( 0xff2200, 0.7 );
  54. light.position.set( 100, 100, 100 );
  55. scene.add( light );
  56. light = new THREE.PointLight( 0x22ff00, 0.7 );
  57. light.position.set( -100, -100, -100 );
  58. scene.add( light );
  59. light = new THREE.AmbientLight( 0x111111 );
  60. scene.add( light );
  61. var loader = new THREE.GLTFLoader();
  62. loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
  63. gltf.scene.traverse( function ( node ) {
  64. if ( node.isMesh ) mesh = node;
  65. } );
  66. mesh.material.morphTargets = true;
  67. mesh.rotation.z = Math.PI / 2;
  68. scene.add( mesh );
  69. });
  70. //
  71. renderer = new THREE.WebGLRenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. //
  76. controls = new THREE.OrbitControls( camera, renderer.domElement );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. }
  89. function render() {
  90. if ( mesh !== undefined ) {
  91. mesh.rotation.y += 0.01;
  92. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + 0.01 * sign;
  93. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  94. sign *= -1;
  95. }
  96. }
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>