webgl_morphtargets_sphere.html 3.6 KB

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