webgl_morphtargets_sphere.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. <meta property="og:title" content="three.js webgl - morph targets - sphere">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_morphtargets_sphere.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_morphtargets_sphere.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGL morph target example
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  30. let camera, scene, renderer, timer;
  31. let mesh;
  32. let sign = 1;
  33. const speed = 0.5;
  34. init();
  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. timer = new THREE.Timer();
  41. timer.connect( document );
  42. const light1 = new THREE.PointLight( 0xff2200, 50000 );
  43. light1.position.set( 100, 100, 100 );
  44. scene.add( light1 );
  45. const light2 = new THREE.PointLight( 0x22ff00, 10000 );
  46. light2.position.set( - 100, - 100, - 100 );
  47. scene.add( light2 );
  48. scene.add( new THREE.AmbientLight( 0x111111 ) );
  49. const loader = new GLTFLoader();
  50. loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
  51. mesh = gltf.scene.getObjectByName( 'AnimatedMorphSphere' );
  52. mesh.rotation.z = Math.PI / 2;
  53. scene.add( mesh );
  54. //
  55. const pointsMaterial = new THREE.PointsMaterial( {
  56. size: 10,
  57. sizeAttenuation: false,
  58. map: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ),
  59. alphaTest: 0.5
  60. } );
  61. const points = new THREE.Points( mesh.geometry, pointsMaterial );
  62. points.morphTargetInfluences = mesh.morphTargetInfluences;
  63. points.morphTargetDictionary = mesh.morphTargetDictionary;
  64. mesh.add( points );
  65. } );
  66. //
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. container.appendChild( renderer.domElement );
  72. //
  73. const controls = new OrbitControls( camera, renderer.domElement );
  74. controls.minDistance = 1;
  75. controls.maxDistance = 20;
  76. //
  77. window.addEventListener( 'resize', onWindowResize );
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. timer.update();
  86. render();
  87. }
  88. function render() {
  89. const delta = timer.getDelta();
  90. if ( mesh !== undefined ) {
  91. const step = delta * speed;
  92. mesh.rotation.y += step;
  93. mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + step * sign;
  94. if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
  95. sign *= - 1;
  96. }
  97. }
  98. renderer.render( scene, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>
粤ICP备19079148号