webgl_morphtargets_face.html 4.1 KB

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