webgl_morphtargets_sphere.html 3.4 KB

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