On Day 5, we’ll add virtual accessories like hats, glasses, and masks using Three.js. These AR filters will attach to facial landmarks and move dynamically with the avatar.
1. Overview of AR Filters in AI Avatars
AR filters enhance avatars by adding accessories that move with the face in real time. We’ll:
- Attach hats to the forehead.
- Attach glasses to the eyes.
- Attach masks to the nose & mouth.
2. Installing 3D Models for AR Filters
Step 1: Install drei
for Easy Model Importing
npm install @react-three/drei
Step 2: Download 3D Models
Find free 3D accessories on:
Use GLTF or OBJ format for compatibility.
3. Adding a Virtual Hat to the Avatar
Step 1: Import a Hat Model
Modify AvatarRenderer.js
:
import { useGLTF } from '@react-three/drei';
function Hat({ position }) {
const { scene } = useGLTF('/models/hat.gltf'); // Load 3D hat model
return <primitive object={scene} position={position} scale={[0.5, 0.5, 0.5]} />;
}
Step 2: Attach Hat to the Forehead
Modify AvatarModel.js
to position the hat:
function AvatarModel({ facialExpressions, landmarks }) {
return (
<group>
<mesh>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="orange" />
</mesh>
{/* Hat positioned above the forehead */}
{landmarks && <Hat position={[landmarks.nose[0], landmarks.nose[1] + 0.2, 1]} />}
</group>
);
}
4. Adding Glasses to the Avatar
Step 1: Import Glasses Model
Modify AvatarRenderer.js
:
function Glasses({ position }) {
const { scene } = useGLTF('/models/glasses.gltf');
return <primitive object={scene} position={position} scale={[0.8, 0.8, 0.8]} />;
}
Step 2: Attach Glasses to the Eyes
Modify AvatarModel.js
:
{landmarks && <Glasses position={[landmarks.leftEye[0], landmarks.leftEye[1], 1]} />}
5. Adding a Face Mask to the Avatar
Step 1: Import Mask Model
function Mask({ position }) {
const { scene } = useGLTF('/models/mask.gltf');
return <primitive object={scene} position={position} scale={[0.9, 0.9, 0.9]} />;
}
Step 2: Attach Mask to the Nose & Mouth
{landmarks && <Mask position={[landmarks.nose[0], landmarks.nose[1] - 0.1, 1]} />}
6. Synchronizing AR Filters with Face Tracking
Modify detectFace
in CameraScreen.js
to extract landmark positions:
const extractLandmarks = (landmarks) => {
return {
leftEye: landmarks[33],
rightEye: landmarks[263],
nose: landmarks[1],
mouth: landmarks[13],
};
};
Send these to the Avatar Renderer:
setFacialExpressions({ headTilt, headTurn });
setLandmarks(extractLandmarks(predictions[0].scaledMesh));
7. Testing the AR Filters
Step 1: Start the App
expo start
Step 2: Test Different Accessories
- Move your head left/right → Glasses & Hat should follow.
- Open your mouth → Mask should stay in position.
8. Optimizing Performance
- Use low-poly models for better rendering speed.
- Reduce update frequency:
if (frameCount % 2 === 0) detectFace(frame);
9. Key Concepts Covered
✅ Added hats, glasses, and masks to the avatar.
✅ Synced 3D accessories with facial landmarks.
✅ Optimized real-time rendering for better performance.
10. Next Steps: Adding Avatar Customization
Tomorrow, we’ll: 🔹 Add custom avatar skins.
🔹 Allow users to change outfits and colors.
11. References & Learning Resources
12. SEO Keywords:
React Native AR filters, Three.js avatar accessories, AI face tracking with 3D models, real-time avatar customization, building VTuber face filters.