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 | 1x 1x 1x 1x | /**
* WaterRipple3D - Adaptive flow ripple effects for Gam (Water) trigram footwork
*
* Creates concentric water ripple rings that emanate from footfall positions
* during Gam stance techniques. Ripples expand outward with wave motion and
* fade over time, following the philosophy: "물처럼 흘러 적의 힘을 이용하라"
* (Flow like water and use the enemy's force).
*
* PERFORMANCE OPTIMIZATION (Minimal Allocations):
* - Uses primitive numeric types instead of Vector3 objects to minimize allocations
* - Ring-based geometry with instancing for efficient rendering
* - Target: 60fps with up to 10 simultaneous ripple effects
*
* Features:
* - Concentric ring expansion from footfall
* - Korean cyberpunk cyan water coloring
* - Wave amplitude oscillation
* - Smooth alpha fade-out
* - Mobile performance optimization
*
* @module components/combat/WaterRipple3D
* @category Combat Effects - Water (감괘)
* @korean 물결파문3D
*/
import { useFrame } from "@react-three/fiber";
import React, { useRef, useMemo } from "react";
import * as THREE from "three";
import { KOREAN_COLORS } from "../../../../../types/constants";
/**
* Water ripple effect data for Gam (Water) trigram
* 물결 파문 효과 데이터 (감괘)
*/
export interface WaterRippleEffect {
/** Unique identifier */
readonly id: string;
/** Origin position in 3D world space (foot impact point) */
readonly position: [number, number, number];
/** Flow type: adaptive, flowing, or reactive */
readonly flowType: "adaptive" | "flowing" | "reactive";
/** Timestamp when effect was created */
readonly startTime: number;
/** Intensity of ripple (0.0 to 1.0) - affects amplitude and radius */
readonly intensity?: number;
}
/**
* Props for WaterRipple3D component
*/
export interface WaterRipple3DProps {
/** Active ripple effects to render */
readonly effects: readonly WaterRippleEffect[];
/** Whether to enable ripple effects */
readonly enabled?: boolean;
/** Mobile device mode (reduced ring count) */
readonly isMobile?: boolean;
/** Callback when effect completes */
readonly onEffectComplete?: (effectId: string) => void;
}
/**
* Individual ripple ring data
*/
interface RippleRing {
/** Current radius of ring (meters) */
radius: number;
/** Expansion speed (m/s) */
speed: number;
/** Current age (seconds) */
age: number;
/** Ring lifetime (seconds) */
lifetime: number;
/** Ring opacity (0.0 to 1.0) */
opacity: number;
/** Wave phase offset for oscillation */
phaseOffset: number;
}
/**
* Performance and physics constants
* 성능 및 물리 상수
*/
const RIPPLE_CONSTANTS = {
/** Number of concentric rings per effect */
RING_COUNT_DESKTOP: 5,
RING_COUNT_MOBILE: 3,
/** Ring spawn interval (seconds) */
RING_SPAWN_INTERVAL: 0.15,
/** Ripple expansion speed (m/s) */
EXPANSION_SPEED: {
adaptive: 2.5, // Moderate, responsive speed
flowing: 2.0, // Smooth, continuous speed
reactive: 3.0, // Fast, instant response
},
/** Ring lifetime (seconds) */
LIFETIME: 2.0,
/** Maximum radius before fade */
MAX_RADIUS: 4.0,
/** Wave amplitude (vertical oscillation) */
WAVE_AMPLITUDE: 0.08,
/** Wave frequency (oscillations per second) */
WAVE_FREQUENCY: 4.0,
/** Ring thickness */
RING_THICKNESS: 0.08,
/** Ring segments (higher = smoother circle) */
RING_SEGMENTS: 32,
/** Floor Y position */
FLOOR_Y: 0.01, // Slightly above floor to prevent z-fighting
/** Maximum delta time for physics stability */
MAX_DELTA: 1 / 30,
/** Korean cyberpunk water color (cyan) */
WATER_COLOR: KOREAN_COLORS.TRIGRAM_GAM_PRIMARY, // 0x1e90ff (blue)
WATER_COLOR_ADAPTIVE: KOREAN_COLORS.PRIMARY_CYAN, // 0x00e6e6 (cyan)
WATER_COLOR_FLOWING: 0x00ccff, // Light cyan
WATER_COLOR_REACTIVE: 0x00ffff, // Bright cyan
} as const;
/**
* Generate ripple rings for water effect
* PERFORMANCE: Minimal allocations, rings created incrementally
*/
const createRippleRings = (
effect: WaterRippleEffect,
ringCount: number
): RippleRing[] => {
const rings: RippleRing[] = [];
const speed = RIPPLE_CONSTANTS.EXPANSION_SPEED[effect.flowType];
const intensity = effect.intensity ?? 1.0;
for (let i = 0; i < ringCount; i++) {
rings.push({
radius: 0,
speed: speed * intensity,
age: -i * RIPPLE_CONSTANTS.RING_SPAWN_INTERVAL, // Negative age = spawn delayed
lifetime: RIPPLE_CONSTANTS.LIFETIME,
opacity: 0,
phaseOffset: Math.random() * Math.PI * 2, // Random wave phase
});
}
return rings;
};
/**
* Get color based on flow type
*/
const getFlowTypeColor = (flowType: "adaptive" | "flowing" | "reactive"): number => {
switch (flowType) {
case "adaptive":
return RIPPLE_CONSTANTS.WATER_COLOR_ADAPTIVE;
case "flowing":
return RIPPLE_CONSTANTS.WATER_COLOR_FLOWING;
case "reactive":
return RIPPLE_CONSTANTS.WATER_COLOR_REACTIVE;
}
};
/**
* WaterRipple3D Component
* Renders water ripple effects for Gam (Water) trigram techniques
*/
export const WaterRipple3D: React.FC<WaterRipple3DProps> = ({
effects,
enabled = true,
isMobile = false,
onEffectComplete,
}) => {
const groupRef = useRef<THREE.Group>(null);
// PERFORMANCE: Reuse materials instead of creating on every render
const materialsRef = useRef<Map<number, THREE.MeshBasicMaterial>>(new Map());
// PERFORMANCE: Reuse geometries instead of creating on every render
const geometriesRef = useRef<Map<string, THREE.RingGeometry>>(new Map());
// Create ring meshes for each effect
const ringMeshes = useMemo(() => {
if (!enabled || effects.length === 0) return [];
const ringCount = isMobile
? RIPPLE_CONSTANTS.RING_COUNT_MOBILE
: RIPPLE_CONSTANTS.RING_COUNT_DESKTOP;
return effects.map((effect) => ({
effectId: effect.id,
position: effect.position,
color: getFlowTypeColor(effect.flowType),
rings: createRippleRings(
effect,
ringCount
),
}));
}, [effects, enabled, isMobile]);
// Cleanup materials and geometries on unmount
React.useEffect(() => {
// Capture ref values in closure to avoid accessing refs in cleanup
const materials = materialsRef.current;
const geometries = geometriesRef.current;
return () => {
// Dispose all cached materials
materials.forEach((material) => material.dispose());
materials.clear();
// Dispose all cached geometries
geometries.forEach((geometry) => geometry.dispose());
geometries.clear();
};
}, []);
// Get or create reusable material for a color
const getMaterial = React.useCallback((color: number): THREE.MeshBasicMaterial => {
if (!materialsRef.current.has(color)) {
materialsRef.current.set(
color,
new THREE.MeshBasicMaterial({
color,
transparent: true,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthWrite: false,
})
);
}
const material = materialsRef.current.get(color);
if (!material) {
throw new Error(`Material for color ${color} should have been created`);
}
return material;
}, []);
// Get or create reusable geometry for a radius (quantized to fixed steps)
const getGeometry = React.useCallback((radius: number): THREE.RingGeometry => {
// PERFORMANCE: Quantize radius into 64 discrete steps to prevent unbounded geometry growth
// Without quantization, continuous radius expansion creates ~4000 unique geometries per effect
const GEOMETRY_STEP_COUNT = 64;
const maxRadius = RIPPLE_CONSTANTS.MAX_RADIUS;
// Quantize radius to nearest step
const step = Math.floor((radius / maxRadius) * GEOMETRY_STEP_COUNT);
const quantizedRadius = (step / GEOMETRY_STEP_COUNT) * maxRadius;
const key = `${step}`;
if (!geometriesRef.current.has(key)) {
geometriesRef.current.set(
key,
new THREE.RingGeometry(
quantizedRadius,
quantizedRadius + RIPPLE_CONSTANTS.RING_THICKNESS,
RIPPLE_CONSTANTS.RING_SEGMENTS
)
);
}
const geometry = geometriesRef.current.get(key);
if (!geometry) {
throw new Error(`Geometry for step ${step} should have been created`);
}
return geometry;
}, []);
// Physics update loop
useFrame((_state, delta) => {
if (!enabled || !groupRef.current) return;
const safeDelta = Math.min(delta, RIPPLE_CONSTANTS.MAX_DELTA);
ringMeshes.forEach((meshData) => {
meshData.rings.forEach((ring) => {
// Update ring age
ring.age += safeDelta;
// Skip if not yet spawned
if (ring.age < 0) {
ring.opacity = 0;
return;
}
// Expand radius
ring.radius += ring.speed * safeDelta;
// Calculate opacity based on lifetime
const lifeProgress = ring.age / ring.lifetime;
if (lifeProgress < 0.2) {
// Fade in
ring.opacity = lifeProgress * 5;
} else if (lifeProgress > 0.8) {
// Fade out
ring.opacity = (1 - lifeProgress) * 5;
} else {
// Full visibility
ring.opacity = 1;
}
// Check if ring expired
if (ring.age >= ring.lifetime || ring.radius >= RIPPLE_CONSTANTS.MAX_RADIUS) {
ring.opacity = 0;
}
});
// Check if all rings expired
const allExpired = meshData.rings.every(
(ring) => ring.age >= ring.lifetime || ring.radius >= RIPPLE_CONSTANTS.MAX_RADIUS
);
if (allExpired && onEffectComplete) {
onEffectComplete(meshData.effectId);
}
});
});
if (!enabled || ringMeshes.length === 0) {
return null;
}
return (
<group ref={groupRef} data-testid="water-ripple-3d">
{/* Three.js performance: reading cached geometries/materials during render */}
{/* eslint-disable react-hooks/refs */}
{ringMeshes.flatMap((meshData) =>
meshData.rings.map((ring, ringIndex) => {
// Skip rings not yet spawned or expired
if (ring.age < 0 || ring.opacity <= 0) {
return null;
}
// Get geometry and material (cached in refs for performance)
const geometry = getGeometry(ring.radius);
const baseMaterial = getMaterial(meshData.color);
// Calculate wave amplitude with oscillation
// Use ring age for animation timing (deterministic, not impure like performance.now)
const waveOffset =
Math.sin(ring.age * RIPPLE_CONSTANTS.WAVE_FREQUENCY + ring.phaseOffset) *
RIPPLE_CONSTANTS.WAVE_AMPLITUDE *
ring.opacity;
// Clone material per ring to prevent shared opacity issues
// (Multiple rings would otherwise share the same material and opacity)
// Note: React Three Fiber automatically disposes cloned materials on unmount
const material = baseMaterial.clone();
material.opacity = ring.opacity * 0.6;
material.transparent = true;
return (
<mesh
key={`${meshData.effectId}-ring-${ringIndex}`}
position={[
meshData.position[0],
RIPPLE_CONSTANTS.FLOOR_Y + waveOffset,
meshData.position[2],
]}
rotation={[-Math.PI / 2, 0, 0]} // Horizontal ring
geometry={geometry}
material={material}
/>
);
})
)}
{/* eslint-enable react-hooks/refs */}
</group>
);
};
/**
* Default export for lazy loading
*/
export default WaterRipple3D;
|