webgl_morphtargets_sphere.html 3.2 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGL morph target example
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  18. var container;
  19. var camera, scene, renderer;
  20. var mesh;
  21. var sign = 1;
  22. init();
  23. animate();
  24. function init() {
  25. container = document.getElementById( 'container' );
  26. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.2, 100 );
  27. camera.position.set( 0, 5, 5 );
  28. scene = new THREE.Scene();
  29. var light = new THREE.PointLight( 0xff2200, 0.7 );
  30. light.position.set( 100, 100, 100 );
  31. scene.add( light );
  32. light = new THREE.PointLight( 0x22ff00, 0.7 );
  33. light.position.set( - 100, - 100, - 100 );
  34. scene.add( light );
  35. light = new THREE.AmbientLight( 0x111111 );
  36. scene.add( light );
  37. var loader = new GLTFLoader();
  38. loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
  39. gltf.scene.traverse( function ( node ) {
  40. if ( node.isMesh ) mesh = node;
  41. } );
  42. mesh.material.morphTargets = true;
  43. mesh.rotation.z = Math.PI / 2;
  44. //mesh.material.visible = false;
  45. scene.add( mesh );
  46. //
  47. var pointsMaterial = new THREE.PointsMaterial( {
  48. size: 10,
  49. sizeAttenuation: false,
  50. map: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ),
  51. alphaTest: 0.5,
  52. morphTargets: true
  53. } );
  54. var points = new THREE.Points( mesh.geometry, pointsMaterial );
  55. points.morphTargetInfluences = mesh.morphTargetInfluences;
  56. points.morphTargetDictionary = mesh.morphTargetDictionary;
  57. mesh.add( points );
  58. } );
  59. //
  60. renderer = new THREE.WebGLRenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. container.appendChild( renderer.domElement );
  64. //
  65. var controls = new OrbitControls( camera, renderer.domElement );
  66. controls.minDistance = 1;
  67. controls.maxDistance = 20;
  68. //
  69. window.addEventListener( 'resize', onWindowResize, false );
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. render();
  79. }
  80. function render() {
  81. if ( mesh !== undefined ) {
  82. mesh.rotation.y += 0.01;
  83. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + 0.01 * sign;
  84. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  85. sign *= - 1;
  86. }
  87. }
  88. renderer.render( scene, camera );
  89. }
  90. </script>
  91. </body>
  92. </html>