webgl_morphtargets_sphere.html 3.8 KB

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