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 | 1x 1x 1x 1x 1x | /**
* ThunderEffect3D - Lightning and thunder visual effects for Jin trigram
*
* Provides explosive lightning arcs, electric sparks, and thunder flashes
* for Jin (Thunder) stance techniques with Korean cyberpunk aesthetic.
*
* **Korean Martial Arts Context:**
* - 진괘 (Jin): Thunder trigram explosive power visualization
* - 번개 (Byeonggae): Lightning effect for charge phase
* - 천둥 (Cheondung): Thunder burst for release phase
*
* @module components/shared/three/effects/ThunderEffect3D
* @korean 천둥효과3D
*/
import { useFrame } from "@react-three/fiber";
import React, { useEffect, useMemo, useRef, useState } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../types/constants";
/**
* Props for ThunderEffect3D component
*/
export interface ThunderEffect3DProps {
/** Position in 3D space [x, y, z] */
readonly position: [number, number, number];
/** Intensity of the effect (0-1) */
readonly intensity?: number;
/** Effect type: charge (buildup) or release (burst) */
readonly effectType: "charge" | "release";
/** Duration in milliseconds */
readonly duration?: number;
/** Callback when effect completes */
readonly onComplete?: () => void;
/** Whether effect is active */
readonly active?: boolean;
}
/**
* Lightning arc between two points with animated energy
*/
const LightningArc: React.FC<{
start: THREE.Vector3;
end: THREE.Vector3;
intensity: number;
color: number;
}> = ({ start, end, intensity, color }) => {
const lineRef = useRef<THREE.Line>(null);
// Create line object with useState to avoid React purity violations
const [line] = useState(() => {
const points: THREE.Vector3[] = [];
const segments = 8;
const displacement = 0.2 * intensity;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const point = new THREE.Vector3().lerpVectors(start, end, t);
// Add random zigzag displacement (except at endpoints)
if (i > 0 && i < segments) {
point.x += (Math.random() - 0.5) * displacement;
point.y += (Math.random() - 0.5) * displacement;
point.z += (Math.random() - 0.5) * displacement;
}
points.push(point);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: intensity,
linewidth: 2,
});
return new THREE.Line(geometry, material);
});
// Update line geometry and material in-place when props change
useEffect(() => {
const positions = line.geometry.attributes.position.array as Float32Array;
// Update geometry points
const segments = 8;
const displacement = 0.2 * intensity;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const point = new THREE.Vector3().lerpVectors(start, end, t);
// Add random zigzag displacement (except at endpoints)
if (i > 0 && i < segments) {
point.x += (Math.random() - 0.5) * displacement;
point.y += (Math.random() - 0.5) * displacement;
point.z += (Math.random() - 0.5) * displacement;
}
positions[i * 3] = point.x;
positions[i * 3 + 1] = point.y;
positions[i * 3 + 2] = point.z;
}
line.geometry.attributes.position.needsUpdate = true;
// Update material properties
const material = line.material as THREE.LineBasicMaterial;
material.opacity = intensity;
material.color.set(color);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [start, end, intensity, color]); // line deliberately excluded to avoid re-creating
useFrame(() => {
if (lineRef.current) {
// Flicker effect
const flicker = 0.8 + Math.random() * 0.2;
const material = line.material as THREE.LineBasicMaterial;
material.opacity = flicker * intensity;
}
});
useEffect(() => {
return () => {
line.geometry.dispose();
(line.material as THREE.Material).dispose();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []); // line deliberately excluded - cleanup only on unmount
return <primitive object={line} ref={lineRef} />;
};
/**
* Electric spark particle for explosive effects
*/
const ElectricSpark: React.FC<{
position: THREE.Vector3;
velocity: THREE.Vector3;
intensity: number;
}> = ({ position: initialPosition, velocity, intensity }) => {
const meshRef = useRef<THREE.Mesh>(null);
const positionRef = useRef(initialPosition.clone());
const velocityRef = useRef(velocity.clone());
useFrame((_, delta) => {
if (meshRef.current) {
// Update position with velocity
positionRef.current.addScaledVector(velocityRef.current, delta);
meshRef.current.position.copy(positionRef.current);
// Apply gravity
velocityRef.current.y -= 9.8 * delta;
// Fade out
const material = meshRef.current.material as THREE.MeshBasicMaterial;
material.opacity *= 0.95;
}
});
return (
<mesh
ref={meshRef}
position={initialPosition}
userData={{ isElectricSpark: true }}
>
<sphereGeometry args={[0.03 * intensity, 8, 8]} />
<meshBasicMaterial
color={KOREAN_COLORS.PRIMARY_CYAN}
transparent
opacity={1}
/>
</mesh>
);
};
/**
* Thunder charge effect - electric arcs gathering energy
*/
const ThunderChargeEffect: React.FC<{
position: [number, number, number];
intensity: number;
}> = ({ position, intensity }) => {
const groupRef = useRef<THREE.Group>(null);
// Create multiple lightning arcs in a sphere pattern
const arcs = useMemo(() => {
const arcData: Array<{ start: THREE.Vector3; end: THREE.Vector3 }> = [];
// Center at local origin; group.position handles world offset
const center = new THREE.Vector3(0, 0, 0);
const radius = 0.5 * intensity;
// Create 6 arcs from surrounding points to center
for (let i = 0; i < 6; i++) {
const angle = (i * Math.PI * 2) / 6;
const start = new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle * 2) * radius * 0.5,
Math.sin(angle) * radius
);
arcData.push({ start, end: center });
}
return arcData;
}, [intensity]);
useFrame(() => {
if (groupRef.current) {
// Pulsing rotation
groupRef.current.rotation.y += 0.05;
const pulse = Math.sin(Date.now() * 0.005) * 0.1 + 1;
groupRef.current.scale.setScalar(pulse);
}
});
return (
<group ref={groupRef} position={position}>
{/* Central energy sphere */}
<mesh>
<sphereGeometry args={[0.15 * intensity, 16, 16]} />
<meshBasicMaterial
color={KOREAN_COLORS.PRIMARY_CYAN}
transparent
opacity={0.6}
/>
</mesh>
{/* Lightning arcs converging to center */}
{arcs.map((arc, i) => (
<LightningArc
key={i}
start={arc.start}
end={arc.end}
intensity={intensity}
color={KOREAN_COLORS.ACCENT_BLUE}
/>
))}
{/* Glow effect */}
<pointLight
position={[0, 0, 0]}
color={KOREAN_COLORS.PRIMARY_CYAN}
intensity={intensity * 2}
distance={2}
decay={2}
/>
</group>
);
};
/**
* Thunder release effect - explosive burst with lightning
*/
const ThunderReleaseEffect: React.FC<{
position: [number, number, number];
intensity: number;
progressRef: React.MutableRefObject<number>;
active: boolean;
}> = ({ position, intensity, progressRef, active }) => {
const groupRef = useRef<THREE.Group>(null);
// Generate electric sparks - regenerate when position changes or effect activates
const [sparks, setSparks] = useState<Array<{
position: THREE.Vector3;
velocity: THREE.Vector3;
}>>([]);
useEffect(() => {
if (!active) return;
const sparkData: Array<{
position: THREE.Vector3;
velocity: THREE.Vector3;
}> = [];
// Center at local origin; group.position handles world offset
const center = new THREE.Vector3(0, 0, 0);
// Create 20 sparks in random directions
for (let i = 0; i < 20; i++) {
const angle = Math.random() * Math.PI * 2;
const elevation = Math.random() * Math.PI - Math.PI / 2;
const speed = 3 + Math.random() * 2;
const velocity = new THREE.Vector3(
Math.cos(angle) * Math.cos(elevation) * speed,
Math.sin(elevation) * speed,
Math.sin(angle) * Math.cos(elevation) * speed
);
sparkData.push({ position: center.clone(), velocity });
}
setSparks(sparkData);
}, [position, active]);
useFrame(() => {
if (groupRef.current) {
const progress = progressRef.current;
// Expanding explosion
const scale = 1 + progress * 2;
groupRef.current.scale.setScalar(scale);
// Fade out over time (skip ElectricSpark meshes which have their own fade logic)
groupRef.current.traverse((object) => {
if (
object instanceof THREE.Mesh &&
object.material &&
!object.userData.isElectricSpark
) {
const material = object.material as THREE.Material & {
opacity?: number;
};
if (material.opacity !== undefined) {
material.opacity = (1 - progress) * intensity;
}
}
});
}
});
return (
<group ref={groupRef} position={position}>
{/* Central explosion sphere */}
<mesh>
<sphereGeometry args={[0.5 * intensity, 16, 16]} />
<meshBasicMaterial
color={KOREAN_COLORS.ACCENT_GOLD}
transparent
opacity={0.8}
/>
</mesh>
{/* Expanding shockwave ring */}
<mesh rotation={[-Math.PI / 2, 0, 0]}>
<ringGeometry args={[0.4 * intensity, 0.6 * intensity, 32]} />
<meshBasicMaterial
color={KOREAN_COLORS.PRIMARY_CYAN}
transparent
opacity={0.6}
side={THREE.DoubleSide}
/>
</mesh>
{/* Lightning cross pattern */}
{[0, 1].map((i) => (
<mesh key={i} rotation={[0, (i * Math.PI) / 2, 0]}>
<boxGeometry args={[1.5 * intensity, 0.05, 0.05]} />
<meshBasicMaterial
color={KOREAN_COLORS.ACCENT_RED}
transparent
opacity={0.9}
/>
</mesh>
))}
{/* Electric sparks */}
{sparks.map((spark, i) => (
<ElectricSpark
key={i}
position={spark.position}
velocity={spark.velocity}
intensity={intensity}
/>
))}
</group>
);
};
/**
* Main Thunder Effect Component
* Displays charge or release effects for Jin trigram techniques
*/
export const ThunderEffect3D: React.FC<ThunderEffect3DProps> = ({
position,
intensity = 1.0,
effectType,
duration = 1000,
onComplete,
active = true,
}) => {
const progressRef = useRef(0);
const startTimeRef = useRef(0);
const completedRef = useRef(false);
useEffect(() => {
startTimeRef.current = Date.now();
progressRef.current = 0;
completedRef.current = false;
}, [active, effectType]);
useFrame(() => {
if (!active) return;
const elapsed = Date.now() - startTimeRef.current;
const newProgress = Math.min(elapsed / duration, 1);
progressRef.current = newProgress;
if (newProgress >= 1 && !completedRef.current && onComplete) {
completedRef.current = true;
onComplete();
}
});
if (!active) return null;
return (
<group data-testid="thunder-effect-3d">
{effectType === "charge" && (
<ThunderChargeEffect position={position} intensity={intensity} />
)}
{effectType === "release" && (
<ThunderReleaseEffect
position={position}
intensity={intensity}
progressRef={progressRef}
active={active}
/>
)}
</group>
);
};
export default ThunderEffect3D;
|