All files / components/shared/three/effects VitalPointMarkers3D.tsx

12.64% Statements 11/87
0% Branches 0/73
0% Functions 0/22
13.58% Lines 11/81

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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387                                                                                                            6x                                               6x 6x 6x 6x 6x     6x                             6x                                 6x                                                                     6x                                                                                                                                                                                                                                                                             6x                                                                                                                                                                                                      
/**
 * VitalPointMarkers3D - 3D vital point visualization
 *
 * Renders anatomical vital points (급소) in 3D space around character models
 * Provides Korean martial arts targeting system visualization
 * Note: Currently displays points from KOREAN_VITAL_POINTS data (expandable to 70 points)
 */
 
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useMemo, useRef, useState } from "react";
import * as THREE from "three";
import { KOREAN_VITAL_POINTS } from "../../../../systems/vitalpoint/KoreanVitalPoints";
import { VitalPoint } from "../../../../systems/vitalpoint/types";
import { Position, VitalPointSeverity } from "../../../../types/common";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../types/constants";
 
/**
 * Body region filter options
 */
export type BodyRegionFilter = "all" | "head" | "torso" | "arms" | "legs";
 
/**
 * Props for the VitalPointMarkers3D component.
 * Controls visibility and interaction with anatomical targeting points.
 */
export interface VitalPointMarkers3DProps {
  /** 3D world position of the character [x, y, z]. Defaults to [0, 0, 0] */
  readonly position?: [number, number, number];
  /** Whether markers are visible. Defaults to true */
  readonly visible?: boolean;
  /** Selected vital point ID for highlighting */
  readonly selectedPoint?: string | null;
  /** Callback when a vital point is clicked */
  readonly onPointClick?: (vitalPointId: string) => void;
  /** Callback when a vital point is hovered */
  readonly onPointHover?: (vitalPointId: string | null) => void;
  /** Filter points by severity level */
  readonly severityFilter?: VitalPointSeverity[];
  /** Filter points by body region */
  readonly regionFilter?: BodyRegionFilter;
  /** Search query to filter points by name */
  readonly searchQuery?: string;
  /** Whether to show point labels with Korean names */
  readonly showLabels?: boolean;
  /** Scale multiplier for marker size. Defaults to 1.0 */
  readonly scale?: number;
  /** Whether to enable pulsing animation. Defaults to true */
  readonly animated?: boolean;
}
 
/**
 * Get color based on vital point severity
 */
const getSeverityColor = (severity: VitalPointSeverity): number => {
  switch (severity) {
    case VitalPointSeverity.LETHAL:
      return KOREAN_COLORS.ACCENT_RED;
    case VitalPointSeverity.CRITICAL:
      return KOREAN_COLORS.SECONDARY_MAGENTA;
    case VitalPointSeverity.MAJOR:
      return KOREAN_COLORS.ACCENT_GOLD;
    case VitalPointSeverity.MODERATE:
      return KOREAN_COLORS.SECONDARY_YELLOW;
    case VitalPointSeverity.MINOR:
      return KOREAN_COLORS.ACCENT_CYAN;
    default:
      return KOREAN_COLORS.PRIMARY_CYAN;
  }
};
 
// Coordinate conversion constants
// The vital point data uses pixel coordinates where:
// - X: ~50-150 range, centered around 100 (character center)
// - Y: 0 at top of head, ~200 at feet
// The 3D character is:
// - X: centered at 0, width ~0.8 units
// - Y: 0 at feet, ~2.8 at top of head
const CHARACTER_CENTER_X = 100; // Pixel center of character sprite
const CHARACTER_SPRITE_HEIGHT = 200; // Pixel height of character sprite
const CHARACTER_3D_HEIGHT = 2.8; // 3D model height in world units
const CHARACTER_3D_WIDTH = 0.8; // Approximate 3D model width
const SPRITE_WIDTH = 100; // Half-width of sprite (total ~200 pixels)
 
// Label styling constants
const LABEL_STYLES = {
  padding: "4px 8px",
  borderRadius: "4px",
  fontSize: "10px",
  subtextSize: "8px",
  subtextOpacity: 0.8,
  borderWidth: "1px",
} as const;
 
/**
 * Convert color number to RGBA hex string
 * @param color - Color as number (e.g., 0xFF0000)
 * @param alpha - Alpha channel as hex string (e.g., "dd" for semi-transparent)
 * @returns RGBA hex string (e.g., "#ff0000dd")
 */
const colorToRgbaHex = (color: number, alpha: string = "ff"): string => {
  return `#${color.toString(16).padStart(6, "0")}${alpha}`;
};
 
/**
 * Convert 2D sprite coordinates to 3D body position
 * Maps vital point pixel coordinates to 3D character model space
 *
 * Input: 2D pixel coordinates where:
 *   - X: ~50-150 (100 = center), positive = right side of character
 *   - Y: 0 = top of head, 200 = feet (y increases downward)
 *
 * Output: 3D world coordinates relative to character position where:
 *   - X: horizontal offset from character center
 *   - Y: height from ground (0 = feet, 2.8 = top)
 *   - Z: depth offset (positive = in front of character)
 */
const convert2DTo3D = (
  pos2D: Position,
  basePosition: [number, number, number]
): [number, number, number] => {
  // Convert X from pixel (100=center) to 3D offset
  // Map from [50, 150] to [-0.4, 0.4] approximately
  const normalizedX = (pos2D.x - CHARACTER_CENTER_X) / SPRITE_WIDTH;
  const x3D = normalizedX * CHARACTER_3D_WIDTH;
 
  // Convert Y from pixel (0=top, 200=bottom) to 3D height (0=feet, 2.8=head)
  // Invert Y axis and scale to 3D height
  const normalizedY = 1 - pos2D.y / CHARACTER_SPRITE_HEIGHT;
  const y3D = normalizedY * CHARACTER_3D_HEIGHT;
 
  // Z offset - slight forward position for visibility
  const z3D = 0.15;
 
  return [basePosition[0] + x3D, basePosition[1] + y3D, basePosition[2] + z3D];
};
 
/**
 * Individual Vital Point Marker Component
 */
interface VitalPointMarkerProps {
  readonly vitalPoint: VitalPoint;
  readonly position3D: [number, number, number];
  readonly selected: boolean;
  readonly hovered: boolean;
  readonly showLabel: boolean;
  readonly scale: number;
  readonly animated: boolean;
  readonly onHover: (hovered: boolean) => void;
  readonly onClick: () => void;
}
 
const VitalPointMarker: React.FC<VitalPointMarkerProps> = ({
  vitalPoint,
  position3D,
  selected,
  hovered,
  showLabel,
  scale,
  animated,
  onHover,
  onClick,
}) => {
  const sphereRef = useRef<THREE.Mesh>(null);
  const ringRef = useRef<THREE.Mesh>(null);
 
  // Animate marker
  useFrame((state) => {
    if (!sphereRef.current || !animated) return;
 
    // Pulsing animation
    const pulse = Math.sin(state.clock.elapsedTime * 2) * 0.1 + 1;
    sphereRef.current.scale.setScalar(pulse * scale);
 
    // Rotate ring for selected/hovered
    if (ringRef.current && (selected || hovered)) {
      ringRef.current.rotation.z += 0.05;
    }
  });
 
  const color = useMemo(() => {
    if (selected) return KOREAN_COLORS.ACCENT_GOLD;
    if (hovered) return KOREAN_COLORS.PRIMARY_CYAN;
    return getSeverityColor(vitalPoint.severity);
  }, [selected, hovered, vitalPoint.severity]);
 
  const markerSize = useMemo(() => {
    // Base marker size and severity multipliers
    // Increased base size for better visibility in combat
    const DEFAULT_MARKER_SIZE = 0.08;
 
    switch (vitalPoint.severity) {
      case VitalPointSeverity.LETHAL:
      case VitalPointSeverity.CRITICAL:
        return DEFAULT_MARKER_SIZE * 1.6 * scale; // 0.128
      case VitalPointSeverity.MAJOR:
        return DEFAULT_MARKER_SIZE * 1.3 * scale; // 0.104
      case VitalPointSeverity.MODERATE:
        return DEFAULT_MARKER_SIZE * 1.0 * scale; // 0.08
      case VitalPointSeverity.MINOR:
        return DEFAULT_MARKER_SIZE * 0.8 * scale; // 0.064
      default:
        return DEFAULT_MARKER_SIZE * scale;
    }
  }, [vitalPoint.severity, scale]);
 
  return (
    <group position={position3D}>
      {/* Main sphere marker */}
      <mesh
        ref={sphereRef}
        onPointerOver={() => onHover(true)}
        onPointerOut={() => onHover(false)}
        onClick={(e) => {
          e.stopPropagation();
          onClick();
        }}
      >
        <sphereGeometry args={[markerSize, 16, 16]} />
        <meshStandardMaterial
          color={color}
          emissive={color}
          emissiveIntensity={hovered || selected ? 0.8 : 0.4}
          transparent
          opacity={hovered || selected ? 1.0 : 0.85}
        />
      </mesh>
 
      {/* Outer ring for selected/hovered */}
      {(selected || hovered) && (
        <mesh ref={ringRef} rotation={[Math.PI / 2, 0, 0]} position={[0, 0, 0]}>
          <ringGeometry args={[markerSize * 1.5, markerSize * 1.8, 32]} />
          <meshBasicMaterial
            color={color}
            transparent
            opacity={0.5}
            side={THREE.DoubleSide}
          />
        </mesh>
      )}
 
      {/* Label overlay */}
      {showLabel && (hovered || selected) && (
        <Html
          position={[0, markerSize * 2, 0]}
          center
          distanceFactor={5}
          style={{
            pointerEvents: "none",
            userSelect: "none",
          }}
        >
          <div
            style={{
              background: colorToRgbaHex(color, "dd"),
              color: "#ffffff",
              padding: LABEL_STYLES.padding,
              borderRadius: LABEL_STYLES.borderRadius,
              fontSize: LABEL_STYLES.fontSize,
              fontFamily: FONT_FAMILY.KOREAN,
              whiteSpace: "nowrap",
              textAlign: "center",
              border: `${LABEL_STYLES.borderWidth} solid ${colorToRgbaHex(
                color
              )}`,
            }}
          >
            <div>{vitalPoint.names.korean}</div>
            <div
              style={{
                fontSize: LABEL_STYLES.subtextSize,
                opacity: LABEL_STYLES.subtextOpacity,
              }}
            >
              {vitalPoint.names.english}
            </div>
          </div>
        </Html>
      )}
    </group>
  );
};
 
/**
 * VitalPointMarkers3D Component
 * Renders all vital points as 3D markers around a character
 */
export const VitalPointMarkers3D: React.FC<VitalPointMarkers3DProps> = ({
  position = [0, 0, 0],
  visible = true,
  selectedPoint = null,
  onPointClick,
  onPointHover,
  severityFilter,
  regionFilter = "all",
  searchQuery = "",
  showLabels = true,
  scale = 1.0,
  animated = true,
}) => {
  const [hoveredPoint, setHoveredPoint] = useState<string | null>(null);
 
  // Filter vital points based on severity, region, and search
  const visiblePoints = useMemo(() => {
    let points = [...KOREAN_VITAL_POINTS];
 
    // Filter by severity
    if (severityFilter && severityFilter.length > 0) {
      points = points.filter((vp) => severityFilter.includes(vp.severity));
    }
 
    // Filter by region
    if (regionFilter !== "all") {
      if (regionFilter === "arms") {
        // Match both left and right arm vital points
        points = points.filter(
          (vp) =>
            vp.id.startsWith("arm_left_") || vp.id.startsWith("arm_right_")
        );
      } else if (regionFilter === "legs") {
        // Match both left and right leg vital points
        points = points.filter(
          (vp) =>
            vp.id.startsWith("leg_left_") || vp.id.startsWith("leg_right_")
        );
      } else {
        // Simple prefix match for head_ or torso_
        const prefix = `${regionFilter}_`;
        points = points.filter((vp) => vp.id.startsWith(prefix));
      }
    }
 
    // Filter by search query
    if (searchQuery) {
      const query = searchQuery.toLowerCase();
      points = points.filter(
        (vp) =>
          vp.names.korean.toLowerCase().includes(query) ||
          vp.names.english.toLowerCase().includes(query) ||
          vp.names.romanized.toLowerCase().includes(query) ||
          vp.id.toLowerCase().includes(query)
      );
    }
 
    return points;
  }, [severityFilter, regionFilter, searchQuery]);
 
  // Handle point hover
  const handlePointHover = (vitalPointId: string, hovered: boolean) => {
    const newHovered = hovered ? vitalPointId : null;
    setHoveredPoint(newHovered);
    onPointHover?.(newHovered);
  };
 
  // Handle point click
  const handlePointClick = (vitalPointId: string) => {
    onPointClick?.(vitalPointId);
  };
 
  if (!visible) return null;
 
  return (
    <group position={position}>
      {visiblePoints.map((vitalPoint) => {
        const position3D = convert2DTo3D(vitalPoint.position, [0, 0, 0]);
 
        return (
          <VitalPointMarker
            key={vitalPoint.id}
            vitalPoint={vitalPoint}
            position3D={position3D}
            selected={selectedPoint === vitalPoint.id}
            hovered={hoveredPoint === vitalPoint.id}
            showLabel={showLabels}
            scale={scale}
            animated={animated}
            onHover={(hovered) => handlePointHover(vitalPoint.id, hovered)}
            onClick={() => handlePointClick(vitalPoint.id)}
          />
        );
      })}
    </group>
  );
};
 
export default VitalPointMarkers3D;