All files / components/screens/philosophy/components TrigramSymbol3D.tsx

50% Statements 16/32
72.41% Branches 21/29
83.33% Functions 5/6
51.61% Lines 16/31

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                                                                                                          1x                     11x 11x     11x 11x     11x 11x     11x 11x         11x 11x 11x     11x   11x                             11x                                                     11x                                                                                                                                                                      
import { Html } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useRef, useMemo } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants/colors";
import { hexToRgbaString } from "../../../../utils/colorUtils";
import type { TrigramStanceData } from "../../../../systems/trigram/types";
import { TrigramStance } from "../../../../types";
 
export interface TrigramSymbol3DProps {
  readonly trigram: TrigramStanceData;
  readonly stance: TrigramStance; // The trigram stance key for test IDs
  readonly position: [number, number, number];
  readonly isSelected: boolean;
  readonly isHovered: boolean;
  readonly onClick?: () => void;
  readonly onPointerOver?: () => void;
  readonly onPointerOut?: () => void;
  readonly scale?: number;
}
 
/**
 * 3D Trigram Symbol Component
 * 
 * **Korean**: 3D 트라이그램 심볼
 * 
 * Renders an individual trigram symbol in 3D space with:
 * - Smooth rotation animation at 60fps
 * - Color changes based on selection and hover state
 * - Particle glow effects for mystical feel
 * - Korean cyberpunk aesthetic with neon glow
 * - Html overlay for the trigram symbol character
 * 
 * Performance optimizations:
 * - Memoized geometries and materials
 * - useFrame for efficient 60fps animation
 * - Smooth lerp transitions for scale changes
 * 
 * @example
 * ```typescript
 * <TrigramSymbol3D
 *   trigram={TRIGRAM_DATA[TrigramStance.GEON]}
 *   stance={TrigramStance.GEON}
 *   position={[0, 0, 0]}
 *   isSelected={false}
 *   isHovered={true}
 *   onClick={() => console.log("Clicked")}
 * />
 * ```
 * 
 * @public
 * @category Philosophy Components
 */
export const TrigramSymbol3D: React.FC<TrigramSymbol3DProps> = ({
  trigram,
  stance,
  position,
  isSelected,
  isHovered,
  onClick,
  onPointerOver,
  onPointerOut,
  scale = 1,
}) => {
  const meshRef = useRef<THREE.Mesh>(null);
  const glowRef = useRef<THREE.Mesh>(null);
 
  // Memoize target scales to avoid recreation in useFrame
  const targetScaleSelected = useMemo(
    () => new THREE.Vector3(1.5 * scale, 1.5 * scale, 1.5 * scale),
    [scale]
  );
  const targetScaleHovered = useMemo(
    () => new THREE.Vector3(1.2 * scale, 1.2 * scale, 1.2 * scale),
    [scale]
  );
  const targetScaleNormal = useMemo(
    () => new THREE.Vector3(1 * scale, 1 * scale, 1 * scale),
    [scale]
  );
 
  // Memoize material properties and color strings
  const materialConfig = useMemo(() => {
    const baseColor = trigram.theme.primary;
    const emissiveColor = isSelected
      ? KOREAN_COLORS.ACCENT_GOLD
      : KOREAN_COLORS.PRIMARY_CYAN;
    const emissiveIntensity = isSelected ? 0.8 : isHovered ? 0.5 : 0.2;
 
    return {
      color: baseColor,
      emissive: emissiveColor,
      emissiveIntensity,
      metalness: 0.7,
      roughness: 0.3,
      // Pre-calculate color strings for Html overlay
      symbolColor: `#${baseColor.toString(16).padStart(6, "0")}`,
      glowColor: isSelected
        ? `#${KOREAN_COLORS.ACCENT_GOLD.toString(16).padStart(6, "0")}`
        : `#${baseColor.toString(16).padStart(6, "0")}`,
    };
  }, [trigram.theme.primary, isSelected, isHovered]);
 
  // 60fps animation loop
  useFrame((state, delta) => {
    if (!meshRef.current || !glowRef.current) return;
 
    // Smooth rotation
    meshRef.current.rotation.y += delta * 0.5;
    meshRef.current.rotation.x += delta * 0.2;
 
    // Glow rotation in opposite direction
    glowRef.current.rotation.y -= delta * 0.3;
 
    // Scale animation with lerp for smooth transitions
    let targetScale = targetScaleNormal;
    if (isSelected) {
      targetScale = targetScaleSelected;
    } else if (isHovered) {
      targetScale = targetScaleHovered;
    }
    meshRef.current.scale.lerp(targetScale, 0.1);
 
    // Pulsing glow effect, scaled relative to the main mesh
    const pulseFactor = Math.sin(state.clock.elapsedTime * 2) * 0.1 + 1;
    const baseScale = meshRef.current.scale.x;
    const glowBaseScale = 1.3 * baseScale;
    const glowScale = glowBaseScale * pulseFactor;
    glowRef.current.scale.set(glowScale, glowScale, glowScale);
  });
 
  return (
    <group position={position}>
      {/* Main trigram mesh */}
      <mesh
        ref={meshRef}
        onClick={onClick}
        onPointerOver={onPointerOver}
        onPointerOut={onPointerOut}
        castShadow
        receiveShadow
        data-testid={`trigram-symbol-${stance}`}
      >
        <boxGeometry args={[1, 1, 0.15]} />
        <meshStandardMaterial
          color={materialConfig.color}
          emissive={materialConfig.emissive}
          emissiveIntensity={materialConfig.emissiveIntensity}
          metalness={materialConfig.metalness}
          roughness={materialConfig.roughness}
        />
      </mesh>
 
      {/* Glow effect */}
      <mesh ref={glowRef}>
        <boxGeometry args={[1.3, 1.3, 0.1]} />
        <meshBasicMaterial
          color={isSelected ? KOREAN_COLORS.ACCENT_GOLD : trigram.theme.primary}
          transparent
          opacity={isSelected ? 0.3 : isHovered ? 0.2 : 0.1}
        />
      </mesh>
 
      {/* Html overlay for trigram symbol and name */}
      <Html
        center
        position={[0, 0.8, 0]}
        style={{
          pointerEvents: "none",
          userSelect: "none",
        }}
        data-testid={`trigram-symbol-overlay-${stance}`}
      >
        <div
          style={{
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            gap: "4px",
          }}
        >
          {/* Trigram symbol */}
          <div
            style={{
              fontSize: isSelected ? "48px" : isHovered ? "40px" : "36px",
              color: materialConfig.symbolColor,
              textShadow: `0 0 20px ${materialConfig.glowColor}`,
              transition: "all 0.3s ease",
              fontWeight: "bold",
            }}
          >
            {trigram.symbol}
          </div>
 
          {/* Korean name */}
          <div
            style={{
              fontSize: isSelected ? "16px" : "14px",
              color: "#ffffff",
              textShadow: `0 0 10px ${hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.8)}`,
              fontWeight: "bold",
              whiteSpace: "nowrap",
              transition: "all 0.3s ease",
            }}
          >
            {trigram.name.korean}
          </div>
        </div>
      </Html>
    </group>
  );
};
 
export default TrigramSymbol3D;