1
0

webgl_morphtargets_face.html 4.0 KB

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