webgl_morphtargets_face.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  32. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  33. import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
  34. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. init();
  37. function init() {
  38. let mixer;
  39. const clock = new THREE.Clock();
  40. const container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20 );
  43. camera.position.set( - 1.8, 0.8, 3 );
  44. const scene = new THREE.Scene();
  45. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  49. container.appendChild( renderer.domElement );
  50. const ktx2Loader = new KTX2Loader()
  51. .setTranscoderPath( 'jsm/libs/basis/' )
  52. .detectSupport( renderer );
  53. new GLTFLoader()
  54. .setKTX2Loader( ktx2Loader )
  55. .setMeshoptDecoder( MeshoptDecoder )
  56. .load( 'models/gltf/facecap.glb', ( gltf ) => {
  57. const mesh = gltf.scene.children[ 0 ];
  58. scene.add( mesh );
  59. mixer = new THREE.AnimationMixer( mesh );
  60. mixer.clipAction( gltf.animations[ 0 ] ).play();
  61. // GUI
  62. const head = mesh.getObjectByName( 'mesh_2' );
  63. const influences = head.morphTargetInfluences;
  64. const gui = new GUI();
  65. gui.close();
  66. for ( const [ key, value ] of Object.entries( head.morphTargetDictionary ) ) {
  67. gui.add( influences, value, 0, 1, 0.01 )
  68. .name( key.replace( 'blendShape1.', '' ) )
  69. .listen();
  70. }
  71. } );
  72. const environment = new RoomEnvironment( renderer );
  73. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  74. scene.background = new THREE.Color( 0x666666 );
  75. scene.environment = pmremGenerator.fromScene( environment ).texture;
  76. const controls = new OrbitControls( camera, renderer.domElement );
  77. controls.enableDamping = true;
  78. controls.minDistance = 2.5;
  79. controls.maxDistance = 5;
  80. controls.minAzimuthAngle = - Math.PI / 2;
  81. controls.maxAzimuthAngle = Math.PI / 2;
  82. controls.maxPolarAngle = Math.PI / 1.8;
  83. controls.target.set( 0, 0.15, - 0.2 );
  84. const stats = new Stats();
  85. container.appendChild( stats.dom );
  86. renderer.setAnimationLoop( () => {
  87. const delta = clock.getDelta();
  88. if ( mixer ) {
  89. mixer.update( delta );
  90. }
  91. renderer.render( scene, camera );
  92. controls.update();
  93. stats.update();
  94. } );
  95. window.addEventListener( 'resize', () => {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. } );
  100. }
  101. </script>
  102. </body>
  103. </html>