webgpu_morphtargets_face.html 4.0 KB

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