webgl_morphtargets_sphere.html 3.6 KB

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