Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | 4x 4x 284x 142x 284x 142x 142x 142x 142x 142x 284x 284x 4x 142x 71x 142x 142x 4x 142x 142x 4x 142x 71x 71x 71x 71x 71x 71x 142x 71x 71x 71x 142x 71x 142x 142x | /**
* Face3D component with realistic facial features
*
* Renders facial features with expressions, eye tracking, and damage visualization:
* - Eyes with pupils that track opponent
* - Mouth that opens/closes based on expression
* - Nose geometry
* - Damage effects (bruises, swelling, bleeding)
*
* @module components/three/Face3D
* @category 3D Components
* @korean 얼굴3D컴포넌트
*/
import React, { useMemo } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../types/constants";
import {
EYE_OPENNESS,
MOUTH_OPENNESS,
type EyeProps,
type Face3DProps,
type MouthProps,
} from "../../types/facial";
import { mixColors } from "../../utils/colorHelpers";
/**
* Head position offset from bone center
* Since Face3D is rendered inside the head bone group,
* this is a small offset to position the face correctly
* @korean 머리위치오프셋
*/
const HEAD_POSITION_OFFSET = 0.1;
/**
* Eye component with pupil tracking and swelling
*
* Renders eye with adjustable openness, pupil tracking, and swelling effects.
*
* @param props - Eye component props
* @returns Eye 3D mesh group
*
* @korean 눈컴포넌트
*/
const Eye: React.FC<EyeProps> = ({
position,
expression,
lookDirection,
swelling,
side,
}) => {
// Calculate eye openness based on expression
const eyeOpenness = useMemo(() => {
return EYE_OPENNESS[expression];
}, [expression]);
// Calculate pupil offset based on look direction
const pupilOffset = useMemo(() => {
// Normalize look direction
const normalized = lookDirection.clone().normalize();
// Convert to 2D offset (x, y in eye space)
// Limit pupil movement to realistic range
const maxOffset = 0.015;
const x = normalized.x * maxOffset;
const y = -normalized.y * maxOffset * 0.5; // Less vertical movement
return new THREE.Vector3(x, y, 0.03);
}, [lookDirection]);
// Swelling color (purple bruise)
const swellingColor = 0x663366;
return (
<group position={position} data-testid={`eye-${side}`}>
{/* Eye white (sclera) */}
<mesh scale={[1, eyeOpenness, 1]}>
<sphereGeometry args={[0.04, 8, 8]} />
<meshBasicMaterial color={0xffffff} />
</mesh>
{/* Pupil (tracks opponent) */}
{eyeOpenness > 0.1 && (
<mesh position={pupilOffset}>
<sphereGeometry args={[0.015, 8, 8]} />
<meshBasicMaterial color={0x000000} />
</mesh>
)}
{/* Swelling indicator */}
{swelling > 0 && (
<mesh scale={[1 + swelling * 0.5, 1 + swelling * 0.5, 1]}>
<sphereGeometry args={[0.05, 8, 8]} />
<meshStandardMaterial
color={swellingColor}
transparent
opacity={swelling * 0.6}
emissive={swellingColor}
emissiveIntensity={0.1}
/>
</mesh>
)}
</group>
);
};
/**
* Mouth component with expression-based openness and bleeding
*
* Renders mouth that opens/closes based on expression.
*
* @param props - Mouth component props
* @returns Mouth 3D mesh group
*
* @korean 입컴포넌트
*/
const Mouth: React.FC<MouthProps> = ({ position, expression, bleeding }) => {
// Calculate mouth openness based on expression
const mouthOpenness = useMemo(() => {
return MOUTH_OPENNESS[expression];
}, [expression]);
// Blood color
const bloodColor = KOREAN_COLORS.ACCENT_RED;
return (
<group position={position} data-testid="mouth">
{/* Mouth line */}
<mesh scale={[0.08, mouthOpenness * 0.04 + 0.005, 0.01]}>
<boxGeometry args={[1, 1, 1]} />
<meshBasicMaterial color={0x330000} />
</mesh>
{/* Blood effect */}
{bleeding > 0 && (
<>
{/* Blood on lip */}
<mesh position={[0, -0.01, 0]}>
<sphereGeometry args={[0.015, 8, 8]} />
<meshBasicMaterial
color={bloodColor}
transparent
opacity={bleeding * 0.8}
/>
</mesh>
{/* Blood drip */}
{bleeding > 0.5 && (
<mesh position={[0, -0.03, 0]} scale={[0.5, 1, 0.5]}>
<cylinderGeometry args={[0.005, 0.008, 0.04, 8]} />
<meshBasicMaterial
color={bloodColor}
transparent
opacity={bleeding * 0.7}
/>
</mesh>
)}
</>
)}
</group>
);
};
/**
* Nose component
*
* Simple geometric nose.
*
* @param position - Nose position
* @param skinColor - Skin tone color
* @param bleeding - Bleeding intensity (0-1)
* @returns Nose 3D mesh
*
* @korean 코컴포넌트
*/
const Nose: React.FC<{
position: [number, number, number];
skinColor: number;
bleeding: number;
}> = ({ position, skinColor, bleeding }) => {
const bloodColor = KOREAN_COLORS.ACCENT_RED;
return (
<group position={position} data-testid="nose">
{/* Nose */}
<mesh rotation={[Math.PI, 0, 0]}>
<coneGeometry args={[0.03, 0.06, 8]} />
<meshStandardMaterial color={skinColor} />
</mesh>
{/* Blood from nose */}
{bleeding > 0 && (
<mesh position={[0, -0.03, 0.01]}>
<sphereGeometry args={[0.01, 6, 6]} />
<meshBasicMaterial
color={bloodColor}
transparent
opacity={bleeding * 0.8}
/>
</mesh>
)}
</group>
);
};
/**
* Face3D component
*
* Main facial features component with head sphere, eyes, mouth, nose,
* and damage visualization.
*
* @param props - Face3D props
* @returns Face 3D mesh group
*
* @example
* ```tsx
* <Face3D
* expression={FacialExpression.PAINED}
* damage={damageState}
* opponentPosition={new THREE.Vector3(5, 2, 0)}
* headRotation={new THREE.Euler(0, 0, 0)}
* enableEyeTracking={true}
* />
* ```
*
* @korean 얼굴3D
*/
export const Face3D: React.FC<Face3DProps> = ({
expression,
damage,
opponentPosition,
headRotation,
enableEyeTracking = true,
enableDamageVisuals = true,
isMobile = false,
skinColor = 0xffdbac, // Default skin tone
}) => {
// Calculate eye direction toward opponent
const eyeDirection = useMemo(() => {
Iif (!enableEyeTracking) {
return new THREE.Vector3(0, 0, 1); // Look forward
}
// Validate opponent position - check for required THREE.Vector3 methods
// This is more robust than instanceof check in test environments
Iif (
!opponentPosition ||
typeof opponentPosition.x !== "number" ||
typeof opponentPosition.y !== "number" ||
typeof opponentPosition.z !== "number"
) {
if (process.env.NODE_ENV !== "production") {
console.warn(
"Face3D: opponentPosition is not a valid THREE.Vector3; defaulting eye direction forward."
);
}
return new THREE.Vector3(0, 0, 1);
}
// Calculate direction from head to opponent using component subtraction
// to avoid prototype chain issues in test environments
// Use the same offset as the face position
const headPos = new THREE.Vector3(0, HEAD_POSITION_OFFSET, 0);
const dir = new THREE.Vector3(
opponentPosition.x - headPos.x,
opponentPosition.y - headPos.y,
opponentPosition.z - headPos.z
);
dir.normalize();
return dir;
}, [opponentPosition, enableEyeTracking]);
// Calculate overall bruise color intensity
const bruiseIntensity = useMemo(() => {
Iif (!enableDamageVisuals) return 0;
const avgBruise =
(damage.leftCheekBruise +
damage.rightCheekBruise +
damage.foreheadBruise +
damage.jawBruise) /
4;
return avgBruise;
}, [damage, enableDamageVisuals]);
// Adjust head color for bruising using helper function
const headColor = useMemo(() => {
Eif (bruiseIntensity === 0) return skinColor;
// Mix skin color with purple bruise color
const bruiseColor = 0x663366;
return mixColors(skinColor, bruiseColor, bruiseIntensity * 0.3);
}, [skinColor, bruiseIntensity]);
// NOTE: Damage visuals are currently handled via headColor bruise interpolation.
// In a future implementation, this could be replaced with a canvas-based texture.
const damageTexture = null;
return (
<group
position={[0, HEAD_POSITION_OFFSET, 0]}
rotation={headRotation}
data-testid="face3d"
>
{/* Head sphere */}
<mesh>
<sphereGeometry args={[0.2, isMobile ? 12 : 16, isMobile ? 12 : 16]} />
<meshStandardMaterial
color={headColor}
map={damageTexture}
roughness={0.8}
metalness={0.1}
/>
</mesh>
{/* Left eye */}
<Eye
position={[-0.08, 0.05, 0.15]}
expression={expression}
lookDirection={eyeDirection}
swelling={enableDamageVisuals ? damage.leftEyeSwelling : 0}
side="left"
/>
{/* Right eye */}
<Eye
position={[0.08, 0.05, 0.15]}
expression={expression}
lookDirection={eyeDirection}
swelling={enableDamageVisuals ? damage.rightEyeSwelling : 0}
side="right"
/>
{/* Mouth */}
<Mouth
position={[0, -0.05, 0.15]}
expression={expression}
bleeding={enableDamageVisuals ? damage.mouthBleeding : 0}
/>
{/* Nose */}
<Nose
position={[0, 0.02, 0.18]}
skinColor={skinColor}
bleeding={enableDamageVisuals ? damage.noseBleeding : 0}
/>
{/* Ears (simple geometric shapes) */}
{!isMobile && (
<>
{/* Left ear */}
<mesh position={[-0.2, 0, 0]} rotation={[0, 0, Math.PI / 6]}>
<sphereGeometry args={[0.04, 8, 8]} />
<meshStandardMaterial color={headColor} />
</mesh>
{/* Right ear */}
<mesh position={[0.2, 0, 0]} rotation={[0, 0, -Math.PI / 6]}>
<sphereGeometry args={[0.04, 8, 8]} />
<meshStandardMaterial color={headColor} />
</mesh>
</>
)}
</group>
);
};
export default Face3D;
|