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

1.98% Statements 3/151
0% Branches 0/44
0% Functions 0/15
2.08% Lines 3/144

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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435                                                                                                                                        1x                                                                                                 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       1x  
/**
 * NerveDisruptionEffect3D.tsx
 *
 * Enhanced nerve disruption visual effects for Li (Fire) trigram precision strikes.
 * Displays electric arcs, neural pattern visualization, and nerve pathway disruption
 * with Korean martial arts theming.
 *
 * Features:
 * - Electric arc particle system with branching patterns
 * - Neural pathway visualization (nerve fiber glow)
 * - Pulse wave effects radiating from impact point
 * - Color-coded by disruption type (electric/paralysis/sensory)
 * - Optimized for 60fps with instancing and object pooling
 *
 * Performance: ~100 particles per effect, <1ms per frame on mid-range hardware
 *
 * @module components/shared/three/effects/NerveDisruptionEffect3D
 * @korean 신경교란효과3D
 */
 
import React, { useRef, useEffect } from "react";
import { useFrame } from "@react-three/fiber";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants";
import { ThreeObjectPools } from "../../../../utils/threeObjectPool";
 
/**
 * Nerve disruption effect types matching Li technique metadata
 */
export type NerveDisruptionType = "electric" | "paralysis" | "sensory";
 
/**
 * Nerve disruption effect data structure
 */
export interface NerveDisruptionEffect {
  /** Unique identifier for the effect */
  readonly id: string;
  /** 3D position of the nerve strike [x, y, z] */
  readonly position: readonly [number, number, number];
  /** Type of nerve disruption */
  readonly type: NerveDisruptionType;
  /** Effect intensity (0.0 to 1.0) */
  readonly intensity: number;
  /** Visual color for effect */
  readonly color: number;
  /** Effect duration in milliseconds */
  readonly duration: number;
  /** Timestamp when effect started (ms) */
  readonly startTime: number;
}
 
/**
 * Component props for NerveDisruptionEffect3D
 */
export interface NerveDisruptionEffect3DProps {
  /** Array of active nerve disruption effects */
  readonly effects: readonly NerveDisruptionEffect[];
  /** Whether effects are enabled */
  readonly enabled?: boolean;
  /** Whether running on mobile device (reduces particle count) */
  readonly isMobile?: boolean;
  /** Callback when an effect completes */
  readonly onEffectComplete?: (id: string) => void;
}
 
/**
 * Performance and physics constants
 */
const CONSTANTS = {
  // Particle counts
  PARTICLES_DESKTOP: 100,
  PARTICLES_MOBILE: 50,
  
  // Arc branching
  ARC_BRANCHES: 5,
  BRANCH_LENGTH: 0.6,
  
  // Timing
  PULSE_DURATION: 0.2, // Rapid pulse
  FADE_OUT_DURATION: 0.3, // Quick fade
  
  // Physics
  EXPANSION_SPEED: 4.0,
  MAX_RADIUS: 1.0,
  
  // Visual
  PARTICLE_SIZE: 0.15,
  ARC_THICKNESS: 0.08,
  
  // Performance
  MAX_DELTA: 1 / 30,
} as const;
 
/**
 * Get color for nerve disruption type with fallback
 */
function getEffectColor(effect: NerveDisruptionEffect): number {
  if (typeof effect.color === 'number') return effect.color;
  
  // Fallback colors based on type
  switch (effect.type) {
    case "electric":
      return KOREAN_COLORS.ACCENT_PRIMARY;
    case "paralysis":
      return KOREAN_COLORS.SECONDARY_MAGENTA;
    case "sensory":
      return KOREAN_COLORS.WARNING_YELLOW;
    default:
      return KOREAN_COLORS.ACCENT_PRIMARY;
  }
}
 
/**
 * NerveDisruptionEffect3D Component
 *
 * Renders electric arc and neural pattern effects for nerve disruption
 */
export const NerveDisruptionEffect3D: React.FC<NerveDisruptionEffect3DProps> = ({
  effects,
  enabled = true,
  isMobile = false,
  onEffectComplete,
}) => {
  const groupRef = useRef<THREE.Group>(null);
  const effectInstancesRef = useRef<Map<string, EffectInstance>>(new Map());
  
  // Determine particle count based on device
  const particleCount = isMobile
    ? CONSTANTS.PARTICLES_MOBILE
    : CONSTANTS.PARTICLES_DESKTOP;
  
  // Track which effects need initialization
  useEffect(() => {
    if (!enabled) return;
    
    effects.forEach((effect) => {
      if (!effectInstancesRef.current.has(effect.id)) {
        effectInstancesRef.current.set(effect.id, {
          effect,
          particleSystem: createParticleSystem(effect, particleCount),
          arcLines: createArcLines(effect),
          startTime: effect.startTime,
          completed: false,
        });
      }
    });
    
    // Capture ref values in effect scope to avoid stale references in cleanup
    const group = groupRef.current;
    const effectInstances = effectInstancesRef.current;
    
    // Cleanup any remaining Three.js objects on unmount or dependency change
    return () => {
      effectInstances.forEach((instance) => {
        // Check if particleSystem exists before accessing its properties
        if (instance.particleSystem) {
          if (instance.particleSystem.parent) {
            group?.remove(instance.particleSystem);
          }
 
          if (instance.particleSystem.geometry) {
            instance.particleSystem.geometry.dispose();
          }
          if (instance.particleSystem.material) {
            (instance.particleSystem.material as THREE.Material).dispose();
          }
        }
 
        instance.arcLines.forEach((line) => {
          if (line.parent) {
            group?.remove(line);
          }
          if (line.geometry) {
            line.geometry.dispose();
          }
          if (line.material) {
            (line.material as THREE.Material).dispose();
          }
        });
      });
 
      effectInstances.clear();
    };
  }, [effects, enabled, particleCount]);
  
  // Animation loop
  useFrame((_state, delta) => {
    if (!enabled || !groupRef.current) return;
    
    const safeDelta = Math.min(delta, CONSTANTS.MAX_DELTA);
    const currentTime = Date.now();
    
    effectInstancesRef.current.forEach((instance, id) => {
      const elapsed = (currentTime - instance.startTime) / 1000; // Convert to seconds
      const totalDuration = instance.effect.duration / 1000; // Convert to seconds
      
      // Check if effect is complete
      if (elapsed >= totalDuration) {
        if (!instance.completed) {
          instance.completed = true;
          onEffectComplete?.(id);
          
          // Remove from scene and dispose
          if (instance.particleSystem.parent) {
            groupRef.current?.remove(instance.particleSystem);
          }
          instance.arcLines.forEach((line) => {
            if (line.parent) {
              groupRef.current?.remove(line);
            }
            line.geometry.dispose();
            (line.material as THREE.Material).dispose();
          });
          instance.particleSystem.geometry.dispose();
          (instance.particleSystem.material as THREE.Material).dispose();
          effectInstancesRef.current.delete(id);
        }
        return;
      }
      
      // Add to scene if not already added
      if (!instance.particleSystem.parent) {
        groupRef.current?.add(instance.particleSystem);
        instance.arcLines.forEach((line) => groupRef.current?.add(line));
      }
      
      // Update particle animation
      updateParticleAnimation(instance, elapsed, totalDuration, safeDelta);
    });
  });
  
  return <group ref={groupRef} />;
};
 
/**
 * Internal effect instance structure
 */
interface EffectInstance {
  effect: NerveDisruptionEffect;
  particleSystem: THREE.Points;
  arcLines: THREE.Line[];
  startTime: number;
  completed: boolean;
}
 
/**
 * Create particle system for nerve disruption effect
 */
function createParticleSystem(
  effect: NerveDisruptionEffect,
  particleCount: number
): THREE.Points {
  const geometry = new THREE.BufferGeometry();
  
  // Initialize particle positions and velocities
  const positions = new Float32Array(particleCount * 3);
  const velocities = new Float32Array(particleCount * 3);
  const initialRadii = new Float32Array(particleCount);
  
  const tempDir = ThreeObjectPools.vector3.acquire();
  
  try {
    for (let i = 0; i < particleCount; i++) {
      // Random point on unit sphere
      const theta = Math.random() * Math.PI * 2;
      const phi = Math.acos(2 * Math.random() - 1);
      
      const x = Math.sin(phi) * Math.cos(theta);
      const y = Math.sin(phi) * Math.sin(theta);
      const z = Math.cos(phi);
      
      // Start at impact point
      positions[i * 3] = effect.position[0];
      positions[i * 3 + 1] = effect.position[1];
      positions[i * 3 + 2] = effect.position[2];
      
      // Store direction for expansion
      tempDir.set(x, y, z).normalize();
      velocities[i * 3] = tempDir.x;
      velocities[i * 3 + 1] = tempDir.y;
      velocities[i * 3 + 2] = tempDir.z;
      
      // Store initial radius for arc pattern
      initialRadii[i] = Math.random() * 0.4;
    }
  } finally {
    ThreeObjectPools.vector3.release(tempDir);
  }
  
  geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
  geometry.setAttribute("velocity", new THREE.BufferAttribute(velocities, 3));
  geometry.setAttribute("initialRadius", new THREE.BufferAttribute(initialRadii, 1));
  
  // Material with additive blending for electric glow
  const color = getEffectColor(effect);
  
  const material = new THREE.PointsMaterial({
    color,
    size: CONSTANTS.PARTICLE_SIZE,
    transparent: true,
    opacity: 1.0,
    sizeAttenuation: true,
    blending: THREE.AdditiveBlending,
    depthWrite: false,
  });
  
  const points = new THREE.Points(geometry, material);
  points.renderOrder = 100; // Render after most objects
  
  return points;
}
 
/**
 * Create arc lines for branching electric effect
 */
function createArcLines(effect: NerveDisruptionEffect): THREE.Line[] {
  const lines: THREE.Line[] = [];
  const color = getEffectColor(effect);
  
  for (let i = 0; i < CONSTANTS.ARC_BRANCHES; i++) {
    const geometry = new THREE.BufferGeometry();
    
    // Create line points from center to random direction
    const angle = (Math.PI * 2 * i) / CONSTANTS.ARC_BRANCHES;
    const radius = CONSTANTS.BRANCH_LENGTH;
    
    const points = [
      new THREE.Vector3(effect.position[0], effect.position[1], effect.position[2]),
      new THREE.Vector3(
        effect.position[0] + Math.cos(angle) * radius,
        effect.position[1] + (Math.random() - 0.5) * 0.3,
        effect.position[2] + Math.sin(angle) * radius
      ),
    ];
    
    geometry.setFromPoints(points);
    
    const material = new THREE.LineBasicMaterial({
      color,
      transparent: true,
      opacity: 1.0,
      linewidth: 2,
      blending: THREE.AdditiveBlending,
      depthWrite: false,
    });
    
    const line = new THREE.Line(geometry, material);
    line.renderOrder = 99;
    
    lines.push(line);
  }
  
  return lines;
}
 
/**
 * Update particle animation based on elapsed time
 */
function updateParticleAnimation(
  instance: EffectInstance,
  elapsed: number,
  totalDuration: number,
  delta: number
): void {
  const { particleSystem, arcLines, effect } = instance;
  const geometry = particleSystem.geometry;
  const material = particleSystem.material as THREE.PointsMaterial;
  
  const positions = geometry.attributes.position.array as Float32Array;
  const velocities = geometry.attributes.velocity.array as Float32Array;
  const initialRadii = geometry.attributes.initialRadius.array as Float32Array;
  
  const particleCount = positions.length / 3;
  
  // Calculate expansion progress
  const expansionProgress = Math.min(elapsed / totalDuration, 1.0);
  const currentRadius = expansionProgress * CONSTANTS.MAX_RADIUS;
  
  // Use pooled vectors for calculations
  const tempTarget = ThreeObjectPools.vector3.acquire();
  const tempDelta = ThreeObjectPools.vector3.acquire();
  const effectPos = ThreeObjectPools.vector3.acquire();
  
  try {
    effectPos.set(effect.position[0], effect.position[1], effect.position[2]);
    
    // Update particle positions (electric arc expansion)
    for (let i = 0; i < particleCount; i++) {
      const i3 = i * 3;
      
      // Expand outward along velocity direction
      const targetRadius = currentRadius + initialRadii[i];
      tempTarget.set(velocities[i3], velocities[i3 + 1], velocities[i3 + 2]);
      tempTarget.multiplyScalar(targetRadius);
      tempTarget.add(effectPos);
      
      // Smooth interpolation to target position
      tempDelta.set(positions[i3], positions[i3 + 1], positions[i3 + 2]);
      tempDelta.sub(tempTarget).multiplyScalar(-delta * 12);
      
      positions[i3] += tempDelta.x;
      positions[i3 + 1] += tempDelta.y;
      positions[i3 + 2] += tempDelta.z;
    }
  } finally {
    ThreeObjectPools.vector3.release(tempTarget);
    ThreeObjectPools.vector3.release(tempDelta);
    ThreeObjectPools.vector3.release(effectPos);
  }
  
  geometry.attributes.position.needsUpdate = true;
  
  // Calculate fade-out progress
  const fadeStart = totalDuration - CONSTANTS.FADE_OUT_DURATION;
  let opacity = 1.0;
  
  if (elapsed > fadeStart) {
    const fadeProgress = (elapsed - fadeStart) / CONSTANTS.FADE_OUT_DURATION;
    opacity = 1.0 - fadeProgress;
  }
  
  // Update particle opacity and size based on intensity
  material.opacity = opacity * effect.intensity;
  material.size = CONSTANTS.PARTICLE_SIZE * (0.5 + effect.intensity * 0.5);
  
  // Update arc line opacity
  arcLines.forEach((line) => {
    const lineMaterial = line.material as THREE.LineBasicMaterial;
    lineMaterial.opacity = opacity * effect.intensity * 0.7;
  });
}
 
// Set display name for debugging
NerveDisruptionEffect3D.displayName = "NerveDisruptionEffect3D";