2
0

webgl_morphtargets_sphere.html 3.7 KB

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