webgl_morphtargets_sphere.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - sphere</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;
  40. var camera, scene, renderer;
  41. var mesh;
  42. var sign = 1;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.getElementById( 'container' );
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.2, 100 );
  48. camera.position.set( 0, 5, 5 );
  49. scene = new THREE.Scene();
  50. var light = new THREE.PointLight( 0xff2200, 0.7 );
  51. light.position.set( 100, 100, 100 );
  52. scene.add( light );
  53. light = new THREE.PointLight( 0x22ff00, 0.7 );
  54. light.position.set( - 100, - 100, - 100 );
  55. scene.add( light );
  56. light = new THREE.AmbientLight( 0x111111 );
  57. scene.add( light );
  58. var loader = new THREE.GLTFLoader();
  59. loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
  60. gltf.scene.traverse( function ( node ) {
  61. if ( node.isMesh ) mesh = node;
  62. } );
  63. mesh.material.morphTargets = true;
  64. mesh.rotation.z = Math.PI / 2;
  65. //mesh.material.visible = false;
  66. scene.add( mesh );
  67. //
  68. var pointsMaterial = new THREE.PointsMaterial( {
  69. size: 10,
  70. sizeAttenuation: false,
  71. map: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ),
  72. alphaTest: 0.5,
  73. morphTargets: true
  74. } );
  75. var points = new THREE.Points( mesh.geometry, pointsMaterial );
  76. points.morphTargetInfluences = mesh.morphTargetInfluences;
  77. points.morphTargetDictionary = mesh.morphTargetDictionary;
  78. mesh.add( points );
  79. } );
  80. //
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. //
  86. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  87. controls.minDistance = 1;
  88. controls.maxDistance = 20;
  89. //
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. }
  101. function render() {
  102. if ( mesh !== undefined ) {
  103. mesh.rotation.y += 0.01;
  104. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + 0.01 * sign;
  105. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  106. sign *= - 1;
  107. }
  108. }
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>