webgl_morphtargets_face.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - face</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 - face">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_morphtargets_face.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_morphtargets_face.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #666666;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - face<br/>
  21. model by <a href="https://www.bannaflak.com/face-cap" target="_blank" rel="noopener">Face Cap</a>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import Stats from 'three/addons/libs/stats.module.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  36. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  37. import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
  38. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  39. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  40. let camera, scene, renderer, stats, mixer, timer, controls;
  41. init();
  42. function init() {
  43. timer = new THREE.Timer();
  44. timer.connect( document );
  45. const container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20 );
  48. camera.position.set( - 1.8, 0.8, 3 );
  49. scene = new THREE.Scene();
  50. renderer = new THREE.WebGLRenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. renderer.setAnimationLoop( animate );
  54. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  55. container.appendChild( renderer.domElement );
  56. const ktx2Loader = new KTX2Loader()
  57. .detectSupport( renderer );
  58. new GLTFLoader()
  59. .setKTX2Loader( ktx2Loader )
  60. .setMeshoptDecoder( MeshoptDecoder )
  61. .load( 'models/gltf/facecap.glb', ( gltf ) => {
  62. const mesh = gltf.scene.children[ 0 ];
  63. scene.add( mesh );
  64. mixer = new THREE.AnimationMixer( mesh );
  65. mixer.clipAction( gltf.animations[ 0 ] ).play();
  66. // GUI
  67. const head = mesh.getObjectByName( 'mesh_2' );
  68. const influences = head.morphTargetInfluences;
  69. const gui = new GUI();
  70. gui.close();
  71. for ( const [ key, value ] of Object.entries( head.morphTargetDictionary ) ) {
  72. gui.add( influences, value, 0, 1, 0.01 )
  73. .name( key.replace( 'blendShape1.', '' ) )
  74. .listen();
  75. }
  76. } );
  77. const environment = new RoomEnvironment();
  78. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  79. scene.background = new THREE.Color( 0x666666 );
  80. scene.environment = pmremGenerator.fromScene( environment, 0.04 ).texture;
  81. controls = new OrbitControls( camera, renderer.domElement );
  82. controls.enableDamping = true;
  83. controls.minDistance = 2.5;
  84. controls.maxDistance = 5;
  85. controls.minAzimuthAngle = - Math.PI / 2;
  86. controls.maxAzimuthAngle = Math.PI / 2;
  87. controls.maxPolarAngle = Math.PI / 1.8;
  88. controls.target.set( 0, 0.15, - 0.2 );
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. window.addEventListener( 'resize', onWindowResize );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. timer.update();
  100. const delta = timer.getDelta();
  101. if ( mixer ) {
  102. mixer.update( delta );
  103. }
  104. renderer.render( scene, camera );
  105. controls.update();
  106. stats.update();
  107. }
  108. </script>
  109. </body>
  110. </html>
粤ICP备19079148号