1
0

webgl_morphtargets_face.html 4.0 KB

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