| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - morph targets - webcam</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <meta property="og:title" content="three.js webgl - morph targets - webcam">
- <meta property="og:type" content="website">
- <meta property="og:url" content="https://threejs.org/examples/webgl_morphtargets_webcam.html">
- <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_morphtargets_webcam.jpg">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #666666;
- }
- canvas {
- position: absolute;
- inset: 0;
- margin: auto;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - morph targets - webcam<br/>
- model by <a href="https://www.bannaflak.com/face-cap" target="_blank" rel="noopener">Face Cap</a>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
- import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- // Mediapipe
- import { FaceLandmarker, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.35';
- const blendshapesMap = {
- // '_neutral': '',
- 'browDownLeft': 'browDown_L',
- 'browDownRight': 'browDown_R',
- 'browInnerUp': 'browInnerUp',
- 'browOuterUpLeft': 'browOuterUp_L',
- 'browOuterUpRight': 'browOuterUp_R',
- 'cheekPuff': 'cheekPuff',
- 'cheekSquintLeft': 'cheekSquint_L',
- 'cheekSquintRight': 'cheekSquint_R',
- 'eyeBlinkLeft': 'eyeBlink_L',
- 'eyeBlinkRight': 'eyeBlink_R',
- 'eyeLookDownLeft': 'eyeLookDown_L',
- 'eyeLookDownRight': 'eyeLookDown_R',
- 'eyeLookInLeft': 'eyeLookIn_L',
- 'eyeLookInRight': 'eyeLookIn_R',
- 'eyeLookOutLeft': 'eyeLookOut_L',
- 'eyeLookOutRight': 'eyeLookOut_R',
- 'eyeLookUpLeft': 'eyeLookUp_L',
- 'eyeLookUpRight': 'eyeLookUp_R',
- 'eyeSquintLeft': 'eyeSquint_L',
- 'eyeSquintRight': 'eyeSquint_R',
- 'eyeWideLeft': 'eyeWide_L',
- 'eyeWideRight': 'eyeWide_R',
- 'jawForward': 'jawForward',
- 'jawLeft': 'jawLeft',
- 'jawOpen': 'jawOpen',
- 'jawRight': 'jawRight',
- 'mouthClose': 'mouthClose',
- 'mouthDimpleLeft': 'mouthDimple_L',
- 'mouthDimpleRight': 'mouthDimple_R',
- 'mouthFrownLeft': 'mouthFrown_L',
- 'mouthFrownRight': 'mouthFrown_R',
- 'mouthFunnel': 'mouthFunnel',
- 'mouthLeft': 'mouthLeft',
- 'mouthLowerDownLeft': 'mouthLowerDown_L',
- 'mouthLowerDownRight': 'mouthLowerDown_R',
- 'mouthPressLeft': 'mouthPress_L',
- 'mouthPressRight': 'mouthPress_R',
- 'mouthPucker': 'mouthPucker',
- 'mouthRight': 'mouthRight',
- 'mouthRollLower': 'mouthRollLower',
- 'mouthRollUpper': 'mouthRollUpper',
- 'mouthShrugLower': 'mouthShrugLower',
- 'mouthShrugUpper': 'mouthShrugUpper',
- 'mouthSmileLeft': 'mouthSmile_L',
- 'mouthSmileRight': 'mouthSmile_R',
- 'mouthStretchLeft': 'mouthStretch_L',
- 'mouthStretchRight': 'mouthStretch_R',
- 'mouthUpperUpLeft': 'mouthUpperUp_L',
- 'mouthUpperUpRight': 'mouthUpperUp_R',
- 'noseSneerLeft': 'noseSneer_L',
- 'noseSneerRight': 'noseSneer_R',
- // '': 'tongueOut'
- };
- // MediaPipe returns the head pose in a metric 3D space that assumes a
- // fixed virtual camera: right-handed, at the origin, looking down -Z, with
- // units in centimeters and a vertical field of view of 63 degrees. The
- // camera, the video plane and the model all have to share that frame for
- // the rendered face to register with the webcam image.
- const MP_FOV = 63; // vertical field of view, in degrees
- const MP_NEAR = 1; // 1 cm
- const MP_FAR = 10000; // 100 m
- const VIDEO_DISTANCE = 100; // depth of the video plane, in cm
- //
- const renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
- document.body.appendChild( renderer.domElement );
- // The render camera matches MediaPipe's virtual camera: at the origin,
- // looking down -Z. It must not be moved, otherwise the overlay drifts. Its
- // aspect switches to the video's once the webcam is running.
- const camera = new THREE.PerspectiveCamera( MP_FOV, window.innerWidth / window.innerHeight, MP_NEAR, MP_FAR );
- const scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x666666 );
- scene.scale.x = - 1; // mirror the whole scene for a selfie view ( flips video and pose together )
- scene.add( new THREE.AmbientLight( 0xffffff, 5 ) );
- // Face
- let face, eyeL, eyeR;
- const eyeRotationLimit = THREE.MathUtils.degToRad( 30 );
- // MediaPipe's facial transformation matrix is copied here verbatim. Until
- // the webcam delivers one, the face rests at a default frontal pose ( in
- // front of the camera, in centimeters ) so it is framed before tracking.
- const faceContainer = new THREE.Object3D();
- faceContainer.matrixAutoUpdate = false;
- faceContainer.matrix.makeTranslation( 0, 0, - 50 );
- faceContainer.matrixWorldNeedsUpdate = true;
- scene.add( faceContainer );
- // The Face Cap model is not MediaPipe's canonical face mesh, so this fixed
- // transform registers it into the canonical frame ( centimeters, +Y up,
- // +Z out of the face ) before the pose matrix is applied. The values are
- // derived from the model's eye positions.
- const registration = new THREE.Object3D();
- registration.scale.setScalar( 0.958 );
- registration.rotation.x = Math.PI / 2;
- registration.position.set( 0, 0.12, 1.18 );
- faceContainer.add( registration );
- const ktx2Loader = new KTX2Loader()
- .detectSupport( renderer );
- new GLTFLoader()
- .setKTX2Loader( ktx2Loader )
- .setMeshoptDecoder( MeshoptDecoder )
- .load( 'models/gltf/facecap.glb', ( gltf ) => {
- // Reparent the head/eyes/teeth and drop the model's own scale rig.
- const group = gltf.scene.getObjectByName( 'grp_transform' );
- registration.add( group );
- const head = group.getObjectByName( 'mesh_2' );
- head.material = new THREE.MeshNormalMaterial();
- const teeth = group.getObjectByName( 'mesh_3' );
- teeth.material = new THREE.MeshNormalMaterial();
- face = head;
- eyeL = group.getObjectByName( 'eyeLeft' );
- eyeR = group.getObjectByName( 'eyeRight' );
- // GUI
- const gui = new GUI();
- gui.close();
- const influences = head.morphTargetInfluences;
- for ( const [ key, value ] of Object.entries( head.morphTargetDictionary ) ) {
- gui.add( influences, value, 0, 1, 0.01 )
- .name( key.replace( 'blendShape1.', '' ) )
- .listen( influences );
- }
- renderer.setAnimationLoop( animate );
- } );
- // Video Texture
- const video = document.createElement( 'video' );
- const texture = new THREE.VideoTexture( video );
- texture.colorSpace = THREE.SRGBColorSpace;
- const geometry = new THREE.PlaneGeometry( 1, 1 );
- const material = new THREE.MeshBasicMaterial( { map: texture, depthTest: false, depthWrite: false } );
- const videomesh = new THREE.Mesh( geometry, material );
- videomesh.position.z = - VIDEO_DISTANCE;
- videomesh.renderOrder = - 1;
- scene.add( videomesh );
- // MediaPipe
- const filesetResolver = await FilesetResolver.forVisionTasks(
- 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.35/wasm'
- );
- const faceLandmarker = await FaceLandmarker.createFromOptions( filesetResolver, {
- baseOptions: {
- modelAssetPath: 'https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task',
- delegate: 'GPU'
- },
- outputFaceBlendshapes: true,
- outputFacialTransformationMatrixes: true,
- runningMode: 'VIDEO',
- numFaces: 1
- } );
- if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
- navigator.mediaDevices.getUserMedia( { video: { facingMode: 'user' } } )
- .then( function ( stream ) {
- video.srcObject = stream;
- video.play();
- } )
- .catch( function ( error ) {
- console.warn( 'Unable to access the camera/webcam.', error );
- } );
- }
- // The camera matches the video aspect; the canvas is sized to that aspect
- // and centered, so the grey body shows through as letterbox/pillarbox bars.
- video.addEventListener( 'loadedmetadata', function () {
- const aspect = video.videoWidth / video.videoHeight;
- camera.aspect = aspect;
- camera.updateProjectionMatrix();
- // Size the plane so it exactly fills the frustum at its depth.
- const height = 2 * VIDEO_DISTANCE * Math.tan( THREE.MathUtils.degToRad( MP_FOV / 2 ) );
- videomesh.scale.set( height * aspect, height, 1 );
- resize();
- } );
- function animate() {
- if ( video.readyState >= HTMLMediaElement.HAVE_METADATA ) {
- const results = faceLandmarker.detectForVideo( video, Date.now() );
- if ( results.facialTransformationMatrixes.length > 0 ) {
- // Apply MediaPipe's metric pose matrix directly.
- faceContainer.matrix.fromArray( results.facialTransformationMatrixes[ 0 ].data );
- faceContainer.matrixWorldNeedsUpdate = true;
- }
- if ( results.faceBlendshapes.length > 0 ) {
- const faceBlendshapes = results.faceBlendshapes[ 0 ].categories;
- // Morph values does not exist on the eye meshes, so we map the eyes blendshape score into rotation values
- const eyeScore = {
- leftHorizontal: 0,
- rightHorizontal: 0,
- leftVertical: 0,
- rightVertical: 0,
- };
- for ( const blendshape of faceBlendshapes ) {
- const categoryName = blendshape.categoryName;
- const score = blendshape.score;
- const index = face.morphTargetDictionary[ blendshapesMap[ categoryName ] ];
- if ( index !== undefined ) {
- face.morphTargetInfluences[ index ] = score;
- }
- // There are two blendshape for movement on each axis (up/down , in/out)
- // Add one and subtract the other to get the final score in -1 to 1 range
- switch ( categoryName ) {
- case 'eyeLookInLeft':
- eyeScore.leftHorizontal += score;
- break;
- case 'eyeLookOutLeft':
- eyeScore.leftHorizontal -= score;
- break;
- case 'eyeLookInRight':
- eyeScore.rightHorizontal -= score;
- break;
- case 'eyeLookOutRight':
- eyeScore.rightHorizontal += score;
- break;
- case 'eyeLookUpLeft':
- eyeScore.leftVertical -= score;
- break;
- case 'eyeLookDownLeft':
- eyeScore.leftVertical += score;
- break;
- case 'eyeLookUpRight':
- eyeScore.rightVertical -= score;
- break;
- case 'eyeLookDownRight':
- eyeScore.rightVertical += score;
- break;
- }
- }
- eyeL.rotation.z = eyeScore.leftHorizontal * eyeRotationLimit;
- eyeR.rotation.z = eyeScore.rightHorizontal * eyeRotationLimit;
- eyeL.rotation.x = eyeScore.leftVertical * eyeRotationLimit;
- eyeR.rotation.x = eyeScore.rightVertical * eyeRotationLimit;
- }
- }
- renderer.render( scene, camera );
- }
- function resize() {
- // Largest video-aspect rectangle that fits inside the window.
- let width = window.innerWidth;
- let height = window.innerHeight;
- if ( width / height > camera.aspect ) {
- width = height * camera.aspect;
- } else {
- height = width / camera.aspect;
- }
- renderer.setSize( width, height );
- }
- window.addEventListener( 'resize', resize );
- </script>
- </body>
- </html>
|