webgpu_morphtargets_face.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  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 { Inspector } from 'three/addons/inspector/Inspector.js';
  37. init();
  38. async function init() {
  39. let mixer;
  40. const timer = new THREE.Timer();
  41. timer.connect( document );
  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.WebGPURenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.setAnimationLoop( animate );
  49. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  50. renderer.inspector = new Inspector();
  51. document.body.appendChild( renderer.domElement );
  52. await renderer.init();
  53. const environment = new RoomEnvironment();
  54. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  55. scene.background = new THREE.Color( 0x666666 );
  56. scene.environment = pmremGenerator.fromScene( environment, 0.04 ).texture;
  57. const ktx2Loader = await new KTX2Loader()
  58. .setTranscoderPath( 'jsm/libs/basis/' )
  59. .detectSupport( renderer );
  60. new GLTFLoader()
  61. .setKTX2Loader( ktx2Loader )
  62. .setMeshoptDecoder( MeshoptDecoder )
  63. .load( 'models/gltf/facecap.glb', ( gltf ) => {
  64. const mesh = gltf.scene.children[ 0 ];
  65. scene.add( mesh );
  66. mixer = new THREE.AnimationMixer( mesh );
  67. mixer.clipAction( gltf.animations[ 0 ] ).play();
  68. // GUI
  69. const head = mesh.getObjectByName( 'mesh_2' );
  70. const influences = head.morphTargetInfluences;
  71. const gui = renderer.inspector.createParameters( 'Morph Targets' );
  72. for ( const [ key, value ] of Object.entries( head.morphTargetDictionary ) ) {
  73. gui.add( influences, value, 0, 1, 0.01 )
  74. .name( key.replace( 'blendShape1.', '' ) )
  75. .listen();
  76. }
  77. } );
  78. scene.background = new THREE.Color( 0x666666 );
  79. const controls = new OrbitControls( camera, renderer.domElement );
  80. controls.enableDamping = true;
  81. controls.minDistance = 2.5;
  82. controls.maxDistance = 5;
  83. controls.minAzimuthAngle = - Math.PI / 2;
  84. controls.maxAzimuthAngle = Math.PI / 2;
  85. controls.maxPolarAngle = Math.PI / 1.8;
  86. controls.target.set( 0, 0.15, - 0.2 );
  87. function animate() {
  88. timer.update();
  89. const delta = timer.getDelta();
  90. if ( mixer ) {
  91. mixer.update( delta );
  92. }
  93. renderer.render( scene, camera );
  94. controls.update();
  95. }
  96. window.addEventListener( 'resize', () => {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. } );
  101. }
  102. </script>
  103. </body>
  104. </html>
粤ICP备19079148号