webgl_morphtargets_face.html 3.8 KB

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