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 | 1x 1x | /**
* WindParticles3D - Korean-themed wind particle system for Son (Wind) stance
*
* Creates swirling wind trails following Son technique strike paths with
* authentic Korean cyberpunk aesthetic. Optimized for 60fps performance
* using instanced rendering and object pooling.
*
* Features:
* - Swirling particle motion along strike paths
* - Korean cyberpunk color scheme (KOREAN_COLORS.TRIGRAM_SON_PRIMARY)
* - Instance-based rendering for performance
* - Physics-based particle lifetime and velocity
* - Mobile-optimized particle counts
* - Object pooling to reduce GC pressure
*
* @module components/effects/WindParticles3D
* @category Combat Effects
* @korean 바람입자3D - 손 자세 바람 입자 시스템
*/
import { Points, PointMaterial } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import React, { useRef, useEffect, useState, useMemo } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../types/constants"; // eslint-disable-line no-restricted-imports -- This 3D effect component directly uses color constants
import { ThreeObjectPools } from "../../utils/threeObjectPool";
/**
* Wind trail data for particle emission
* 바람 궤적 데이터 (입자 방출용)
*/
interface WindTrail {
/** Starting position of wind trail */
position: THREE.Vector3;
/** Direction vector of wind motion */
direction: THREE.Vector3;
/** Trail length in meters */
length: number;
/** Timestamp when trail was created */
age: number;
/** Whether vectors are pooled (for cleanup) */
isPooled: boolean;
}
/**
* Wind particle instance data
* 바람 입자 인스턴스 데이터
*
* PERFORMANCE: position and velocity use pooled Vector3 objects
*/
interface WindParticle {
/** Current position [x, y, z] - POOLED Vector3 */
position: THREE.Vector3;
/** Current velocity [x, y, z] - POOLED Vector3 */
velocity: THREE.Vector3;
/** Swirl phase for circular motion (radians) */
swirlPhase: number;
/** Swirl radius in meters */
swirlRadius: number;
/** Particle lifetime in seconds */
lifetime: number;
/** Time elapsed since creation */
age: number;
/** Flag to track if vectors are pooled and need release */
isPooled: boolean;
}
/**
* Wind effect configuration
* 바람 효과 설정
*/
export interface WindEffect {
/** Unique identifier */
readonly id: string;
/** Origin position in 3D world space */
readonly position: [number, number, number];
/** Strike direction for particle trail */
readonly direction: [number, number, number];
/** Effect intensity (0.0 to 1.0) */
readonly intensity: number;
/** Timestamp when effect was created */
readonly startTime: number;
}
/**
* Props for WindParticles3D component
*/
export interface WindParticles3DProps {
/** Active wind effects to render */
readonly effects: readonly WindEffect[];
/** Whether to enable wind effects */
readonly enabled?: boolean;
/** Mobile device mode (reduced particle count) */
readonly isMobile?: boolean;
/** Callback when effect completes */
readonly onEffectComplete?: (effectId: string) => void;
}
/**
* Performance and physics constants
* 성능 및 물리 상수
*/
const WIND_CONSTANTS = {
/** Maximum particles per effect */
MAX_PARTICLES_DESKTOP: 150,
MAX_PARTICLES_MOBILE: 50,
/** Particle lifetime in seconds */
PARTICLE_LIFETIME: 1.5,
/** Effect trail lifetime in seconds */
TRAIL_LIFETIME: 0.8,
/** Base particle velocity (m/s) */
BASE_VELOCITY: 3.0,
/** Velocity spread factor */
VELOCITY_SPREAD: 0.5,
/** Swirl radius range (meters) */
SWIRL_RADIUS_MIN: 0.05,
SWIRL_RADIUS_MAX: 0.15,
/** Swirl speed (radians/second) */
SWIRL_SPEED: 4.0,
/** Particle size */
PARTICLE_SIZE: 0.04,
/** Emission rate (particles per trail per second) */
EMISSION_RATE: 100,
/** Maximum per-frame delta time (seconds) */
MAX_DELTA: 1 / 30,
} as const;
/**
* WindParticles3D Component
*
* Renders physics-based wind particles that swirl along Son (Wind) technique
* strike paths. Uses instanced rendering for 60fps performance on mobile
* and desktop.
*
* @example
* ```tsx
* const [windEffects, setWindEffects] = useState<WindEffect[]>([]);
*
* // On Son technique execution
* const handleWindTechnique = (position: [number, number, number], direction: [number, number, number]) => {
* setWindEffects([...windEffects, {
* id: generateId(),
* position,
* direction,
* intensity: 1.0,
* startTime: Date.now(),
* }]);
* };
*
* <WindParticles3D
* effects={windEffects}
* enabled={visualEffects.wind}
* isMobile={isMobileDevice}
* onEffectComplete={(id) => {
* setWindEffects(prev => prev.filter(e => e.id !== id));
* }}
* />
* ```
*/
export const WindParticles3D: React.FC<WindParticles3DProps> = ({
effects,
enabled = true,
isMobile = false,
onEffectComplete,
}) => {
const pointsRef = useRef<THREE.Points>(null);
const [particles, setParticles] = useState<WindParticle[]>([]);
const [trails, setTrails] = useState<Map<string, WindTrail>>(new Map());
const completedEffectsRef = useRef<Set<string>>(new Set());
// Track particles and trails for cleanup
const particlesRef = useRef<WindParticle[]>([]);
const trailsRef = useRef<Map<string, WindTrail>>(new Map());
// Reusable color object to avoid creating new THREE.Color on every frame
// This significantly reduces GC pressure when rendering many particles
const windColor = useMemo(
() => new THREE.Color(KOREAN_COLORS.TRIGRAM_SON_PRIMARY),
[]
);
const maxParticles = isMobile
? WIND_CONSTANTS.MAX_PARTICLES_MOBILE
: WIND_CONSTANTS.MAX_PARTICLES_DESKTOP;
// Update refs when state changes
useEffect(() => {
particlesRef.current = particles;
}, [particles]);
useEffect(() => {
trailsRef.current = trails;
}, [trails]);
// Initialize wind trails from effects
useEffect(() => {
if (!enabled) return;
const newTrails = new Map(trails);
effects.forEach((effect) => {
if (!newTrails.has(effect.id) && !completedEffectsRef.current.has(effect.id)) {
// Acquire Vector3 objects from pool
const position = ThreeObjectPools.vector3.acquire();
const direction = ThreeObjectPools.vector3.acquire();
position.set(effect.position[0], effect.position[1], effect.position[2]);
direction.set(effect.direction[0], effect.direction[1], effect.direction[2]).normalize();
newTrails.set(effect.id, {
position,
direction,
length: 1.5 * effect.intensity, // Trail length based on intensity
age: 0,
isPooled: true,
});
}
});
setTrails(newTrails);
}, [effects, enabled, trails]);
// Emit particles along wind trails
useFrame((_, delta) => {
if (!enabled || trails.size === 0) return;
const safeDelta = Math.min(delta, WIND_CONSTANTS.MAX_DELTA);
const particlesToEmit = Math.floor(WIND_CONSTANTS.EMISSION_RATE * safeDelta);
// Update existing particles
const updatedParticles = particles
.map((particle) => {
particle.age += safeDelta;
if (particle.age >= particle.lifetime) {
// Release pooled vectors
if (particle.isPooled) {
ThreeObjectPools.vector3.release(particle.position);
ThreeObjectPools.vector3.release(particle.velocity);
}
return null;
}
// Update swirl phase
particle.swirlPhase += WIND_CONSTANTS.SWIRL_SPEED * safeDelta;
// Calculate swirl offset
const swirlX = Math.cos(particle.swirlPhase) * particle.swirlRadius;
const swirlY = Math.sin(particle.swirlPhase) * particle.swirlRadius;
// Update position with velocity and swirl
particle.position.x += (particle.velocity.x + swirlX) * safeDelta;
particle.position.y += (particle.velocity.y + swirlY) * safeDelta;
particle.position.z += particle.velocity.z * safeDelta;
return particle;
})
.filter((p): p is WindParticle => p !== null);
// Emit new particles from active trails
const newParticles: WindParticle[] = [];
const updatedTrails = new Map(trails);
const trailsToRemove: string[] = [];
updatedTrails.forEach((trail, effectId) => {
trail.age += safeDelta;
if (trail.age >= WIND_CONSTANTS.TRAIL_LIFETIME) {
trailsToRemove.push(effectId);
completedEffectsRef.current.add(effectId);
onEffectComplete?.(effectId);
// Release pooled vectors
if (trail.isPooled) {
ThreeObjectPools.vector3.release(trail.position);
ThreeObjectPools.vector3.release(trail.direction);
}
return;
}
// Emit particles along trail
if (updatedParticles.length + newParticles.length < maxParticles) {
for (let i = 0; i < particlesToEmit; i++) {
const t = Math.random(); // Random position along trail
const spreadAngle = (Math.random() - 0.5) * Math.PI / 4; // ±45° spread
// Calculate emission position along trail
const position = ThreeObjectPools.vector3.acquire();
position.copy(trail.position).addScaledVector(trail.direction, t * trail.length);
// Calculate velocity with spread
const velocity = ThreeObjectPools.vector3.acquire();
velocity.copy(trail.direction).multiplyScalar(WIND_CONSTANTS.BASE_VELOCITY);
// Add perpendicular spread
const perpendicular = ThreeObjectPools.vector3.acquire();
perpendicular.set(-trail.direction.z, 0, trail.direction.x).normalize();
velocity.addScaledVector(perpendicular, Math.sin(spreadAngle) * WIND_CONSTANTS.VELOCITY_SPREAD);
ThreeObjectPools.vector3.release(perpendicular);
newParticles.push({
position,
velocity,
swirlPhase: Math.random() * Math.PI * 2,
swirlRadius:
WIND_CONSTANTS.SWIRL_RADIUS_MIN +
Math.random() * (WIND_CONSTANTS.SWIRL_RADIUS_MAX - WIND_CONSTANTS.SWIRL_RADIUS_MIN),
lifetime: WIND_CONSTANTS.PARTICLE_LIFETIME,
age: 0,
isPooled: true,
});
}
}
});
// Remove completed trails
trailsToRemove.forEach((id) => updatedTrails.delete(id));
setTrails(updatedTrails);
// Update particles state
setParticles([...updatedParticles, ...newParticles]);
// Update Points geometry
if (pointsRef.current && updatedParticles.length > 0) {
const positions = new Float32Array(updatedParticles.length * 3);
const colors = new Float32Array(updatedParticles.length * 3);
updatedParticles.forEach((particle, i) => {
const i3 = i * 3;
positions[i3] = particle.position.x;
positions[i3 + 1] = particle.position.y;
positions[i3 + 2] = particle.position.z;
// Fade color based on age using pre-allocated color object
const alpha = 1.0 - particle.age / particle.lifetime;
colors[i3] = windColor.r * alpha;
colors[i3 + 1] = windColor.g * alpha;
colors[i3 + 2] = windColor.b * alpha;
});
const geometry = pointsRef.current.geometry;
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
geometry.attributes.position.needsUpdate = true;
geometry.attributes.color.needsUpdate = true;
}
});
// Cleanup on unmount - uses refs to access latest state
useEffect(() => {
return () => {
// Release all pooled vectors from latest particles
particlesRef.current.forEach((particle) => {
if (particle.isPooled) {
ThreeObjectPools.vector3.release(particle.position);
ThreeObjectPools.vector3.release(particle.velocity);
}
});
// Release all pooled vectors from latest trails
trailsRef.current.forEach((trail) => {
if (trail.isPooled) {
ThreeObjectPools.vector3.release(trail.position);
ThreeObjectPools.vector3.release(trail.direction);
}
});
};
}, []); // Empty deps OK - refs always have latest values
if (!enabled || particles.length === 0) return null;
return (
<Points ref={pointsRef}>
<bufferGeometry />
<PointMaterial
vertexColors
size={WIND_CONSTANTS.PARTICLE_SIZE}
sizeAttenuation
transparent
opacity={0.9}
depthWrite={false}
blending={THREE.AdditiveBlending}
/>
</Points>
);
};
|