All files / utils threeObjectPool.ts

100% Statements 119/119
80% Branches 20/25
100% Functions 41/41
100% Lines 106/106

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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552                                                91x       91x               4655x 4655x 4640x 4640x   15x               4652x 4652x                 14x 14x 2710x               20x             115x                         91x       91x               190x 190x 177x 177x   13x               188x 188x                 9x 9x 1700x               12x             115x                         91x       91x               7x 7x 1x 1x   6x               5x 5x                 8x 8x 675x               8x             115x                         91x       91x               5718x 5718x 5692x 5692x   26x               5716x 5716x                 8x 8x 680x               6x             115x                           91x       91x               13x 13x 5x 5x   8x               10x 10x                 10x 10x 425x               12x             115x                                                           91x                                                       6x 6x 6x 6x 6x                           1x                         115x 115x 115x 115x 115x                                                     63x 63x 63x 1688x   63x   1688x                                   2x 2x 2x 4x   2x   4x                                   2x 2x 2x 4x   2x   4x                                                     3x 3x 3x 8x   3x   8x      
/**
 * Three.js Object Pool for Animation Performance
 * 
 * Specialized object pooling for frequently created Three.js objects
 * to reduce garbage collection pressure during animation updates.
 * 
 * Target: Reduce ~1,344 object allocations per frame to near-zero.
 * 
 * @module utils/threeObjectPool
 * @category Performance
 * @korean Three.js객체풀
 */
 
import * as THREE from "three";
 
/**
 * Pool for THREE.Euler objects used in bone rotations
 * 
 * Reduces GC pressure from creating new Euler objects every frame.
 * Prewarmed with 200 objects (2 characters × 28 bones × 3.5 keyframes avg)
 * 
 * @korean 오일러풀
 */
class EulerPool {
  private pool: THREE.Euler[] = [];
  private readonly maxSize: number;
 
  constructor(maxSize = 500) {
    this.maxSize = maxSize;
  }
 
  /**
   * Acquire an Euler object from the pool
   * @returns Pooled or new Euler object (reset to 0,0,0)
   */
  acquire(): THREE.Euler {
    const euler = this.pool.pop();
    if (euler) {
      euler.set(0, 0, 0);
      return euler;
    }
    return new THREE.Euler(0, 0, 0);
  }
 
  /**
   * Return an Euler object to the pool
   * @param euler - Euler object to return
   */
  release(euler: THREE.Euler): void {
    Eif (this.pool.length < this.maxSize) {
      this.pool.push(euler);
    }
  }
 
  /**
   * Pre-populate pool with objects
   * @param count - Number of objects to create
   */
  prewarm(count: number): void {
    const toCreate = Math.min(count, this.maxSize - this.pool.length);
    for (let i = 0; i < toCreate; i++) {
      this.pool.push(new THREE.Euler(0, 0, 0));
    }
  }
 
  /**
   * Get current pool size
   */
  get size(): number {
    return this.pool.length;
  }
 
  /**
   * Clear all pooled objects
   */
  clear(): void {
    this.pool = [];
  }
}
 
/**
 * Pool for THREE.Vector3 objects used in bone positions
 * 
 * Reduces GC pressure from creating new Vector3 objects every frame.
 * Prewarmed with 200 objects (2 characters × 28 bones × 3.5 keyframes avg)
 * 
 * @korean 벡터3풀
 */
class Vector3Pool {
  private pool: THREE.Vector3[] = [];
  private readonly maxSize: number;
 
  constructor(maxSize = 500) {
    this.maxSize = maxSize;
  }
 
  /**
   * Acquire a Vector3 object from the pool
   * @returns Pooled or new Vector3 object (reset to 0,0,0)
   */
  acquire(): THREE.Vector3 {
    const vector = this.pool.pop();
    if (vector) {
      vector.set(0, 0, 0);
      return vector;
    }
    return new THREE.Vector3(0, 0, 0);
  }
 
  /**
   * Return a Vector3 object to the pool
   * @param vector - Vector3 object to return
   */
  release(vector: THREE.Vector3): void {
    Eif (this.pool.length < this.maxSize) {
      this.pool.push(vector);
    }
  }
 
  /**
   * Pre-populate pool with objects
   * @param count - Number of objects to create
   */
  prewarm(count: number): void {
    const toCreate = Math.min(count, this.maxSize - this.pool.length);
    for (let i = 0; i < toCreate; i++) {
      this.pool.push(new THREE.Vector3(0, 0, 0));
    }
  }
 
  /**
   * Get current pool size
   */
  get size(): number {
    return this.pool.length;
  }
 
  /**
   * Clear all pooled objects
   */
  clear(): void {
    this.pool = [];
  }
}
 
/**
 * Pool for THREE.Matrix4 objects used in bone transformations
 * 
 * Reduces GC pressure from creating new Matrix4 objects during calculations.
 * Prewarmed with 100 objects (2 characters × 28 bones × 2 temp matrices)
 * 
 * @korean 행렬4풀
 */
class Matrix4Pool {
  private pool: THREE.Matrix4[] = [];
  private readonly maxSize: number;
 
  constructor(maxSize = 200) {
    this.maxSize = maxSize;
  }
 
  /**
   * Acquire a Matrix4 object from the pool
   * @returns Pooled or new Matrix4 object (reset to identity)
   */
  acquire(): THREE.Matrix4 {
    const matrix = this.pool.pop();
    if (matrix) {
      matrix.identity();
      return matrix;
    }
    return new THREE.Matrix4();
  }
 
  /**
   * Return a Matrix4 object to the pool
   * @param matrix - Matrix4 object to return
   */
  release(matrix: THREE.Matrix4): void {
    Eif (this.pool.length < this.maxSize) {
      this.pool.push(matrix);
    }
  }
 
  /**
   * Pre-populate pool with objects
   * @param count - Number of objects to create
   */
  prewarm(count: number): void {
    const toCreate = Math.min(count, this.maxSize - this.pool.length);
    for (let i = 0; i < toCreate; i++) {
      this.pool.push(new THREE.Matrix4());
    }
  }
 
  /**
   * Get current pool size
   */
  get size(): number {
    return this.pool.length;
  }
 
  /**
   * Clear all pooled objects
   */
  clear(): void {
    this.pool = [];
  }
}
 
/**
 * Pool for THREE.Quaternion objects used in rotations
 * 
 * Reduces GC pressure from quaternion operations.
 * Prewarmed with 100 objects (2 characters × 28 bones × 2 temp quaternions)
 * 
 * @korean 쿼터니언풀
 */
class QuaternionPool {
  private pool: THREE.Quaternion[] = [];
  private readonly maxSize: number;
 
  constructor(maxSize = 200) {
    this.maxSize = maxSize;
  }
 
  /**
   * Acquire a Quaternion object from the pool
   * @returns Pooled or new Quaternion object (reset to identity)
   */
  acquire(): THREE.Quaternion {
    const quat = this.pool.pop();
    if (quat) {
      quat.set(0, 0, 0, 1);
      return quat;
    }
    return new THREE.Quaternion(0, 0, 0, 1);
  }
 
  /**
   * Return a Quaternion object to the pool
   * @param quat - Quaternion object to return
   */
  release(quat: THREE.Quaternion): void {
    Eif (this.pool.length < this.maxSize) {
      this.pool.push(quat);
    }
  }
 
  /**
   * Pre-populate pool with objects
   * @param count - Number of objects to create
   */
  prewarm(count: number): void {
    const toCreate = Math.min(count, this.maxSize - this.pool.length);
    for (let i = 0; i < toCreate; i++) {
      this.pool.push(new THREE.Quaternion(0, 0, 0, 1));
    }
  }
 
  /**
   * Get current pool size
   */
  get size(): number {
    return this.pool.length;
  }
 
  /**
   * Clear all pooled objects
   */
  clear(): void {
    this.pool = [];
  }
}
 
/**
 * Pool for THREE.Color objects used in material and particle effects
 * 
 * Reduces GC pressure from creating new Color objects for visual effects,
 * particle systems, and dynamic material updates.
 * Prewarmed with 50 objects for typical combat scenarios.
 * 
 * @korean 색상풀
 */
class ColorPool {
  private pool: THREE.Color[] = [];
  private readonly maxSize: number;
 
  constructor(maxSize = 100) {
    this.maxSize = maxSize;
  }
 
  /**
   * Acquire a Color object from the pool
   * @returns Pooled or new Color object (reset to white)
   */
  acquire(): THREE.Color {
    const color = this.pool.pop();
    if (color) {
      color.set(0xffffff);
      return color;
    }
    return new THREE.Color(0xffffff);
  }
 
  /**
   * Return a Color object to the pool
   * @param color - Color object to return
   */
  release(color: THREE.Color): void {
    Eif (this.pool.length < this.maxSize) {
      this.pool.push(color);
    }
  }
 
  /**
   * Pre-populate pool with objects
   * @param count - Number of objects to create
   */
  prewarm(count: number): void {
    const toCreate = Math.min(count, this.maxSize - this.pool.length);
    for (let i = 0; i < toCreate; i++) {
      this.pool.push(new THREE.Color(0xffffff));
    }
  }
 
  /**
   * Get current pool size
   */
  get size(): number {
    return this.pool.length;
  }
 
  /**
   * Clear all pooled objects
   */
  clear(): void {
    this.pool = [];
  }
}
 
/**
 * Global Three.js object pools
 * 
 * Singleton pools for animation system to reduce GC pressure.
 * Prewarmed during app initialization for optimal performance.
 * 
 * @example
 * ```typescript
 * // Acquire temporary Euler for calculation
 * const tempEuler = ThreeObjectPools.euler.acquire();
 * tempEuler.set(x, y, z);
 * // ... use tempEuler ...
 * ThreeObjectPools.euler.release(tempEuler);
 * 
 * // Acquire temporary Color for particle effect
 * const tempColor = ThreeObjectPools.color.acquire();
 * tempColor.set(0xff0000);
 * // ... use tempColor ...
 * ThreeObjectPools.color.release(tempColor);
 * 
 * // Prewarm pools on app start
 * ThreeObjectPools.prewarmAll();
 * ```
 * 
 * @korean Three.js객체풀들
 */
export const ThreeObjectPools = {
  /** Euler rotation pool */
  euler: new EulerPool(500),
  
  /** Vector3 position pool */
  vector3: new Vector3Pool(500),
  
  /** Matrix4 transformation pool */
  matrix4: new Matrix4Pool(200),
  
  /** Quaternion rotation pool */
  quaternion: new QuaternionPool(200),
 
  /** Color pool for materials and effects */
  color: new ColorPool(100),
 
  /**
   * Pre-populate all pools with recommended sizes
   * 
   * Call this during app initialization for optimal performance.
   * Recommended sizes based on 2 characters with 28 bones each plus effects:
   * - Euler: 200 objects (rotations)
   * - Vector3: 200 objects (positions, velocities)
   * - Matrix4: 100 objects (transformations)
   * - Quaternion: 100 objects (rotations)
   * - Color: 50 objects (particle effects, materials)
   */
  prewarmAll(): void {
    this.euler.prewarm(200);
    this.vector3.prewarm(200);
    this.matrix4.prewarm(100);
    this.quaternion.prewarm(100);
    this.color.prewarm(50);
  },
 
  /**
   * Get current status of all pools
   * @returns Pool status for monitoring
   */
  getStatus(): {
    euler: number;
    vector3: number;
    matrix4: number;
    quaternion: number;
    color: number;
  } {
    return {
      euler: this.euler.size,
      vector3: this.vector3.size,
      matrix4: this.matrix4.size,
      quaternion: this.quaternion.size,
      color: this.color.size,
    };
  },
 
  /**
   * Clear all pools (useful for testing)
   */
  clearAll(): void {
    this.euler.clear();
    this.vector3.clear();
    this.matrix4.clear();
    this.quaternion.clear();
    this.color.clear();
  },
} as const;
 
/**
 * Helper to execute a function with temporary Euler objects
 * Automatically acquires and releases Euler objects.
 * 
 * @param count - Number of Euler objects needed
 * @param fn - Function that uses the Euler objects
 * @returns Result of the function
 * 
 * @example
 * ```typescript
 * const result = withTempEulers(2, ([euler1, euler2]) => {
 *   euler1.set(x1, y1, z1);
 *   euler2.set(x2, y2, z2);
 *   return calculateSomething(euler1, euler2);
 * });
 * ```
 * 
 * @korean 임시오일러사용
 */
export function withTempEulers<T>(
  count: number,
  fn: (eulers: THREE.Euler[]) => T
): T {
  const eulers: THREE.Euler[] = [];
  try {
    for (let i = 0; i < count; i++) {
      eulers.push(ThreeObjectPools.euler.acquire());
    }
    return fn(eulers);
  } finally {
    eulers.forEach((euler) => ThreeObjectPools.euler.release(euler));
  }
}
 
/**
 * Helper to execute a function with temporary Vector3 objects
 * Automatically acquires and releases Vector3 objects.
 * 
 * @param count - Number of Vector3 objects needed
 * @param fn - Function that uses the Vector3 objects
 * @returns Result of the function
 * 
 * @korean 임시벡터3사용
 */
export function withTempVectors<T>(
  count: number,
  fn: (vectors: THREE.Vector3[]) => T
): T {
  const vectors: THREE.Vector3[] = [];
  try {
    for (let i = 0; i < count; i++) {
      vectors.push(ThreeObjectPools.vector3.acquire());
    }
    return fn(vectors);
  } finally {
    vectors.forEach((vector) => ThreeObjectPools.vector3.release(vector));
  }
}
 
/**
 * Helper to execute a function with temporary Matrix4 objects
 * Automatically acquires and releases Matrix4 objects.
 * 
 * @param count - Number of Matrix4 objects needed
 * @param fn - Function that uses the Matrix4 objects
 * @returns Result of the function
 * 
 * @korean 임시행렬4사용
 */
export function withTempMatrices<T>(
  count: number,
  fn: (matrices: THREE.Matrix4[]) => T
): T {
  const matrices: THREE.Matrix4[] = [];
  try {
    for (let i = 0; i < count; i++) {
      matrices.push(ThreeObjectPools.matrix4.acquire());
    }
    return fn(matrices);
  } finally {
    matrices.forEach((matrix) => ThreeObjectPools.matrix4.release(matrix));
  }
}
 
/**
 * Helper to execute a function with temporary Color objects
 * Automatically acquires and releases Color objects.
 * 
 * @param count - Number of Color objects needed
 * @param fn - Function that uses the Color objects
 * @returns Result of the function
 * 
 * @example
 * ```typescript
 * const avgColor = withTempColors(2, ([color1, color2]) => {
 *   color1.set(0xff0000);
 *   color2.set(0x0000ff);
 *   return color1.lerp(color2, 0.5);
 * });
 * ```
 * 
 * @korean 임시색상사용
 */
export function withTempColors<T>(
  count: number,
  fn: (colors: THREE.Color[]) => T
): T {
  const colors: THREE.Color[] = [];
  try {
    for (let i = 0; i < count; i++) {
      colors.push(ThreeObjectPools.color.acquire());
    }
    return fn(colors);
  } finally {
    colors.forEach((color) => ThreeObjectPools.color.release(color));
  }
}