webgl_morphtargets_sphere.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. 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. timer = new Timer();
  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. renderer.setAnimationLoop( animate );
  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. timer.update();
  82. render();
  83. }
  84. function render() {
  85. const delta = timer.getDelta();
  86. if ( mesh !== undefined ) {
  87. const step = delta * speed;
  88. mesh.rotation.y += step;
  89. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + step * sign;
  90. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  91. sign *= - 1;
  92. }
  93. }
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>