webgl_morphtargets_sphere.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGL morph target example
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  18. var container;
  19. var camera, scene, renderer, clock;
  20. var mesh;
  21. var sign = 1;
  22. var speed = 0.5;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.getElementById( 'container' );
  27. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.2, 100 );
  28. camera.position.set( 0, 5, 5 );
  29. scene = new THREE.Scene();
  30. clock = new THREE.Clock();
  31. var light = new THREE.PointLight( 0xff2200, 0.7 );
  32. light.position.set( 100, 100, 100 );
  33. scene.add( light );
  34. light = new THREE.PointLight( 0x22ff00, 0.7 );
  35. light.position.set( - 100, - 100, - 100 );
  36. scene.add( light );
  37. light = new THREE.AmbientLight( 0x111111 );
  38. scene.add( light );
  39. var loader = new GLTFLoader();
  40. loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
  41. gltf.scene.traverse( function ( node ) {
  42. if ( node.isMesh ) mesh = node;
  43. } );
  44. mesh.material.morphTargets = true;
  45. mesh.rotation.z = Math.PI / 2;
  46. //mesh.material.visible = false;
  47. scene.add( mesh );
  48. //
  49. var pointsMaterial = new THREE.PointsMaterial( {
  50. size: 10,
  51. sizeAttenuation: false,
  52. map: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ),
  53. alphaTest: 0.5,
  54. morphTargets: true
  55. } );
  56. var points = new THREE.Points( mesh.geometry, pointsMaterial );
  57. points.morphTargetInfluences = mesh.morphTargetInfluences;
  58. points.morphTargetDictionary = mesh.morphTargetDictionary;
  59. mesh.add( points );
  60. } );
  61. //
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. //
  67. var controls = new OrbitControls( camera, renderer.domElement );
  68. controls.minDistance = 1;
  69. controls.maxDistance = 20;
  70. //
  71. window.addEventListener( 'resize', onWindowResize, false );
  72. document.addEventListener( 'visibilitychange', onVisibilityChange );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function onVisibilityChange() {
  80. if ( document.hidden === true ) {
  81. clock.stop();
  82. } else {
  83. clock.start();
  84. }
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. render();
  89. }
  90. function render() {
  91. var delta = clock.getDelta();
  92. if ( mesh !== undefined ) {
  93. var step = delta * speed;
  94. mesh.rotation.y += step;
  95. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + step * sign;
  96. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  97. sign *= - 1;
  98. }
  99. }
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>