webgl_morphtargets_webcam.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morph targets - webcam</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. <meta property="og:title" content="three.js webgl - morph targets - webcam">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_morphtargets_webcam.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_morphtargets_webcam.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #666666;
  15. }
  16. canvas {
  17. position: absolute;
  18. inset: 0;
  19. margin: auto;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - webcam<br/>
  26. model by <a href="https://www.bannaflak.com/face-cap" target="_blank" rel="noopener">Face Cap</a>
  27. </div>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.module.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  39. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  40. import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
  41. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  42. // Mediapipe
  43. import { FaceLandmarker, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.35';
  44. const blendshapesMap = {
  45. // '_neutral': '',
  46. 'browDownLeft': 'browDown_L',
  47. 'browDownRight': 'browDown_R',
  48. 'browInnerUp': 'browInnerUp',
  49. 'browOuterUpLeft': 'browOuterUp_L',
  50. 'browOuterUpRight': 'browOuterUp_R',
  51. 'cheekPuff': 'cheekPuff',
  52. 'cheekSquintLeft': 'cheekSquint_L',
  53. 'cheekSquintRight': 'cheekSquint_R',
  54. 'eyeBlinkLeft': 'eyeBlink_L',
  55. 'eyeBlinkRight': 'eyeBlink_R',
  56. 'eyeLookDownLeft': 'eyeLookDown_L',
  57. 'eyeLookDownRight': 'eyeLookDown_R',
  58. 'eyeLookInLeft': 'eyeLookIn_L',
  59. 'eyeLookInRight': 'eyeLookIn_R',
  60. 'eyeLookOutLeft': 'eyeLookOut_L',
  61. 'eyeLookOutRight': 'eyeLookOut_R',
  62. 'eyeLookUpLeft': 'eyeLookUp_L',
  63. 'eyeLookUpRight': 'eyeLookUp_R',
  64. 'eyeSquintLeft': 'eyeSquint_L',
  65. 'eyeSquintRight': 'eyeSquint_R',
  66. 'eyeWideLeft': 'eyeWide_L',
  67. 'eyeWideRight': 'eyeWide_R',
  68. 'jawForward': 'jawForward',
  69. 'jawLeft': 'jawLeft',
  70. 'jawOpen': 'jawOpen',
  71. 'jawRight': 'jawRight',
  72. 'mouthClose': 'mouthClose',
  73. 'mouthDimpleLeft': 'mouthDimple_L',
  74. 'mouthDimpleRight': 'mouthDimple_R',
  75. 'mouthFrownLeft': 'mouthFrown_L',
  76. 'mouthFrownRight': 'mouthFrown_R',
  77. 'mouthFunnel': 'mouthFunnel',
  78. 'mouthLeft': 'mouthLeft',
  79. 'mouthLowerDownLeft': 'mouthLowerDown_L',
  80. 'mouthLowerDownRight': 'mouthLowerDown_R',
  81. 'mouthPressLeft': 'mouthPress_L',
  82. 'mouthPressRight': 'mouthPress_R',
  83. 'mouthPucker': 'mouthPucker',
  84. 'mouthRight': 'mouthRight',
  85. 'mouthRollLower': 'mouthRollLower',
  86. 'mouthRollUpper': 'mouthRollUpper',
  87. 'mouthShrugLower': 'mouthShrugLower',
  88. 'mouthShrugUpper': 'mouthShrugUpper',
  89. 'mouthSmileLeft': 'mouthSmile_L',
  90. 'mouthSmileRight': 'mouthSmile_R',
  91. 'mouthStretchLeft': 'mouthStretch_L',
  92. 'mouthStretchRight': 'mouthStretch_R',
  93. 'mouthUpperUpLeft': 'mouthUpperUp_L',
  94. 'mouthUpperUpRight': 'mouthUpperUp_R',
  95. 'noseSneerLeft': 'noseSneer_L',
  96. 'noseSneerRight': 'noseSneer_R',
  97. // '': 'tongueOut'
  98. };
  99. // MediaPipe returns the head pose in a metric 3D space that assumes a
  100. // fixed virtual camera: right-handed, at the origin, looking down -Z, with
  101. // units in centimeters and a vertical field of view of 63 degrees. The
  102. // camera, the video plane and the model all have to share that frame for
  103. // the rendered face to register with the webcam image.
  104. const MP_FOV = 63; // vertical field of view, in degrees
  105. const MP_NEAR = 1; // 1 cm
  106. const MP_FAR = 10000; // 100 m
  107. const VIDEO_DISTANCE = 100; // depth of the video plane, in cm
  108. //
  109. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  110. renderer.setPixelRatio( window.devicePixelRatio );
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  113. document.body.appendChild( renderer.domElement );
  114. // The render camera matches MediaPipe's virtual camera: at the origin,
  115. // looking down -Z. It must not be moved, otherwise the overlay drifts. Its
  116. // aspect switches to the video's once the webcam is running.
  117. const camera = new THREE.PerspectiveCamera( MP_FOV, window.innerWidth / window.innerHeight, MP_NEAR, MP_FAR );
  118. const scene = new THREE.Scene();
  119. scene.background = new THREE.Color( 0x666666 );
  120. scene.scale.x = - 1; // mirror the whole scene for a selfie view ( flips video and pose together )
  121. scene.add( new THREE.AmbientLight( 0xffffff, 5 ) );
  122. // Face
  123. let face, eyeL, eyeR;
  124. const eyeRotationLimit = THREE.MathUtils.degToRad( 30 );
  125. // MediaPipe's facial transformation matrix is copied here verbatim. Until
  126. // the webcam delivers one, the face rests at a default frontal pose ( in
  127. // front of the camera, in centimeters ) so it is framed before tracking.
  128. const faceContainer = new THREE.Object3D();
  129. faceContainer.matrixAutoUpdate = false;
  130. faceContainer.matrix.makeTranslation( 0, 0, - 50 );
  131. faceContainer.matrixWorldNeedsUpdate = true;
  132. scene.add( faceContainer );
  133. // The Face Cap model is not MediaPipe's canonical face mesh, so this fixed
  134. // transform registers it into the canonical frame ( centimeters, +Y up,
  135. // +Z out of the face ) before the pose matrix is applied. The values are
  136. // derived from the model's eye positions.
  137. const registration = new THREE.Object3D();
  138. registration.scale.setScalar( 0.958 );
  139. registration.rotation.x = Math.PI / 2;
  140. registration.position.set( 0, 0.12, 1.18 );
  141. faceContainer.add( registration );
  142. const ktx2Loader = new KTX2Loader()
  143. .detectSupport( renderer );
  144. new GLTFLoader()
  145. .setKTX2Loader( ktx2Loader )
  146. .setMeshoptDecoder( MeshoptDecoder )
  147. .load( 'models/gltf/facecap.glb', ( gltf ) => {
  148. // Reparent the head/eyes/teeth and drop the model's own scale rig.
  149. const group = gltf.scene.getObjectByName( 'grp_transform' );
  150. registration.add( group );
  151. const head = group.getObjectByName( 'mesh_2' );
  152. head.material = new THREE.MeshNormalMaterial();
  153. const teeth = group.getObjectByName( 'mesh_3' );
  154. teeth.material = new THREE.MeshNormalMaterial();
  155. face = head;
  156. eyeL = group.getObjectByName( 'eyeLeft' );
  157. eyeR = group.getObjectByName( 'eyeRight' );
  158. // GUI
  159. const gui = new GUI();
  160. gui.close();
  161. const influences = head.morphTargetInfluences;
  162. for ( const [ key, value ] of Object.entries( head.morphTargetDictionary ) ) {
  163. gui.add( influences, value, 0, 1, 0.01 )
  164. .name( key.replace( 'blendShape1.', '' ) )
  165. .listen( influences );
  166. }
  167. renderer.setAnimationLoop( animate );
  168. } );
  169. // Video Texture
  170. const video = document.createElement( 'video' );
  171. const texture = new THREE.VideoTexture( video );
  172. texture.colorSpace = THREE.SRGBColorSpace;
  173. const geometry = new THREE.PlaneGeometry( 1, 1 );
  174. const material = new THREE.MeshBasicMaterial( { map: texture, depthTest: false, depthWrite: false } );
  175. const videomesh = new THREE.Mesh( geometry, material );
  176. videomesh.position.z = - VIDEO_DISTANCE;
  177. videomesh.renderOrder = - 1;
  178. scene.add( videomesh );
  179. // MediaPipe
  180. const filesetResolver = await FilesetResolver.forVisionTasks(
  181. 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.35/wasm'
  182. );
  183. const faceLandmarker = await FaceLandmarker.createFromOptions( filesetResolver, {
  184. baseOptions: {
  185. modelAssetPath: 'https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task',
  186. delegate: 'GPU'
  187. },
  188. outputFaceBlendshapes: true,
  189. outputFacialTransformationMatrixes: true,
  190. runningMode: 'VIDEO',
  191. numFaces: 1
  192. } );
  193. if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
  194. navigator.mediaDevices.getUserMedia( { video: { facingMode: 'user' } } )
  195. .then( function ( stream ) {
  196. video.srcObject = stream;
  197. video.play();
  198. } )
  199. .catch( function ( error ) {
  200. console.warn( 'Unable to access the camera/webcam.', error );
  201. } );
  202. }
  203. // The camera matches the video aspect; the canvas is sized to that aspect
  204. // and centered, so the grey body shows through as letterbox/pillarbox bars.
  205. video.addEventListener( 'loadedmetadata', function () {
  206. const aspect = video.videoWidth / video.videoHeight;
  207. camera.aspect = aspect;
  208. camera.updateProjectionMatrix();
  209. // Size the plane so it exactly fills the frustum at its depth.
  210. const height = 2 * VIDEO_DISTANCE * Math.tan( THREE.MathUtils.degToRad( MP_FOV / 2 ) );
  211. videomesh.scale.set( height * aspect, height, 1 );
  212. resize();
  213. } );
  214. function animate() {
  215. if ( video.readyState >= HTMLMediaElement.HAVE_METADATA ) {
  216. const results = faceLandmarker.detectForVideo( video, Date.now() );
  217. if ( results.facialTransformationMatrixes.length > 0 ) {
  218. // Apply MediaPipe's metric pose matrix directly.
  219. faceContainer.matrix.fromArray( results.facialTransformationMatrixes[ 0 ].data );
  220. faceContainer.matrixWorldNeedsUpdate = true;
  221. }
  222. if ( results.faceBlendshapes.length > 0 ) {
  223. const faceBlendshapes = results.faceBlendshapes[ 0 ].categories;
  224. // Morph values does not exist on the eye meshes, so we map the eyes blendshape score into rotation values
  225. const eyeScore = {
  226. leftHorizontal: 0,
  227. rightHorizontal: 0,
  228. leftVertical: 0,
  229. rightVertical: 0,
  230. };
  231. for ( const blendshape of faceBlendshapes ) {
  232. const categoryName = blendshape.categoryName;
  233. const score = blendshape.score;
  234. const index = face.morphTargetDictionary[ blendshapesMap[ categoryName ] ];
  235. if ( index !== undefined ) {
  236. face.morphTargetInfluences[ index ] = score;
  237. }
  238. // There are two blendshape for movement on each axis (up/down , in/out)
  239. // Add one and subtract the other to get the final score in -1 to 1 range
  240. switch ( categoryName ) {
  241. case 'eyeLookInLeft':
  242. eyeScore.leftHorizontal += score;
  243. break;
  244. case 'eyeLookOutLeft':
  245. eyeScore.leftHorizontal -= score;
  246. break;
  247. case 'eyeLookInRight':
  248. eyeScore.rightHorizontal -= score;
  249. break;
  250. case 'eyeLookOutRight':
  251. eyeScore.rightHorizontal += score;
  252. break;
  253. case 'eyeLookUpLeft':
  254. eyeScore.leftVertical -= score;
  255. break;
  256. case 'eyeLookDownLeft':
  257. eyeScore.leftVertical += score;
  258. break;
  259. case 'eyeLookUpRight':
  260. eyeScore.rightVertical -= score;
  261. break;
  262. case 'eyeLookDownRight':
  263. eyeScore.rightVertical += score;
  264. break;
  265. }
  266. }
  267. eyeL.rotation.z = eyeScore.leftHorizontal * eyeRotationLimit;
  268. eyeR.rotation.z = eyeScore.rightHorizontal * eyeRotationLimit;
  269. eyeL.rotation.x = eyeScore.leftVertical * eyeRotationLimit;
  270. eyeR.rotation.x = eyeScore.rightVertical * eyeRotationLimit;
  271. }
  272. }
  273. renderer.render( scene, camera );
  274. }
  275. function resize() {
  276. // Largest video-aspect rectangle that fits inside the window.
  277. let width = window.innerWidth;
  278. let height = window.innerHeight;
  279. if ( width / height > camera.aspect ) {
  280. width = height * camera.aspect;
  281. } else {
  282. height = width / camera.aspect;
  283. }
  284. renderer.setSize( width, height );
  285. }
  286. window.addEventListener( 'resize', resize );
  287. </script>
  288. </body>
  289. </html>
粤ICP备19079148号