All files / utils fabricTextures.ts

25.34% Statements 37/146
35.89% Branches 14/39
72.72% Functions 8/11
27% Lines 37/137

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                                                                                                                  12x                                                                           12x                           12x               12x 15512x 15512x 15512x 15512x                   12x     15512x 15512x 15512x 15512x 15512x       15512x                 15512x                             12x       6160x 6160x                                                                                                                                                                       12x       6160x 6160x                                                                                                                                 12x       3192x 3192x                                                                                                                                                                         12x           6160x   6160x                   6160x 6160x     6160x         6160x 6160x 6160x 6160x     6160x                     12x                                
/**
 * Procedural fabric texture generation for realistic clothing
 *
 * **Korean**: 절차적 직물 텍스처 (Procedural Fabric Textures)
 *
 * Generates canvas-based textures for realistic dobok (도복) martial arts
 * uniform rendering without external image assets. Uses Three.js CanvasTexture
 * for efficient GPU-based texture mapping.
 *
 * **Features**:
 * - Procedural weave patterns for fabric realism
 * - Normal maps for surface detail and depth
 * - Roughness maps for material variation
 * - Memory-safe with proper cleanup
 *
 * @module utils/fabricTextures
 * @category Visual Effects
 * @korean 직물텍스처유틸
 */
 
import * as THREE from "three";
 
/**
 * Fabric texture configuration
 */
export interface FabricTextureConfig {
  /** Base color for the fabric */
  readonly baseColor: string;
  /** Weave pattern density (higher = finer weave) */
  readonly weaveDensity: number;
  /** Texture resolution (power of 2 recommended) */
  readonly resolution: number;
  /** Thread variation intensity (0-1) */
  readonly threadVariation: number;
  /** Whether to generate normal map */
  readonly generateNormalMap: boolean;
  /** Whether to generate roughness map */
  readonly generateRoughnessMap: boolean;
}
 
/**
 * Generated fabric texture set
 */
export interface FabricTextureSet {
  /** Color/diffuse map */
  readonly colorMap: THREE.CanvasTexture;
  /** Normal map for surface detail (optional) */
  readonly normalMap?: THREE.CanvasTexture;
  /** Roughness map for material variation (optional) */
  readonly roughnessMap?: THREE.CanvasTexture;
  /** Cleanup function to dispose all textures */
  readonly dispose: () => void;
}
 
/**
 * Default fabric texture configurations for different materials
 */
export const FABRIC_PRESETS: Record<string, Partial<FabricTextureConfig>> = {
  /** Traditional cotton dobok (도복) */
  dobok: {
    weaveDensity: 32,
    threadVariation: 0.15,
    resolution: 256,
    generateNormalMap: true,
    generateRoughnessMap: true,
  },
  /** Tactical synthetic fabric */
  tactical: {
    weaveDensity: 48,
    threadVariation: 0.08,
    resolution: 256,
    generateNormalMap: true,
    generateRoughnessMap: true,
  },
  /** Leather material */
  leather: {
    weaveDensity: 16,
    threadVariation: 0.25,
    resolution: 256,
    generateNormalMap: true,
    generateRoughnessMap: false,
  },
  /** Silk/satin material */
  silk: {
    weaveDensity: 64,
    threadVariation: 0.05,
    resolution: 256,
    generateNormalMap: true,
    generateRoughnessMap: true,
  },
};
 
/**
 * Convert hex color to RGB components
 */
const hexToRgb = (hex: string): { r: number; g: number; b: number } => {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result
    ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16),
      }
    : { r: 128, g: 128, b: 128 };
};
 
/**
 * Simple seeded random for reproducible textures
 */
const seededRandom = (seed: number): number => {
  const x = Math.sin(seed) * 10000;
  return x - Math.floor(x);
};
 
/**
 * Check if we're in a browser environment with canvas support
 */
const canCreateCanvas = (): boolean => {
  Iif (typeof document === "undefined") return false;
  try {
    const canvas = document.createElement("canvas");
    return canvas.getContext("2d") !== null;
  } catch {
    return false;
  }
};
 
/**
 * Create a fallback texture for test environments
 * Uses a minimal texture that works in all environments
 */
const createFallbackTexture = (): THREE.CanvasTexture => {
  // In test environments, THREE mocks may not have all features
  // Create a minimal mock that satisfies the interface
  try {
    const data = new Uint8Array([128, 128, 128, 255]);
    const dataTexture = new THREE.DataTexture(data, 1, 1, THREE.RGBAFormat);
    dataTexture.needsUpdate = true;
    return dataTexture as unknown as THREE.CanvasTexture;
  } catch {
    // Ultimate fallback - create a minimal mock texture object
    // Use numeric constants instead of THREE constants for test compatibility
    const mockTexture = {
      dispose: () => {},
      needsUpdate: true,
      wrapS: 1000, // THREE.RepeatWrapping value
      wrapT: 1000,
      repeat: { set: () => {} },
      uuid: "mock-fabric-texture",
      isTexture: true,
    };
    return mockTexture as unknown as THREE.CanvasTexture;
  }
};
 
/**
 * Generate a procedural fabric weave color map
 *
 * Creates a canvas-based texture with realistic thread patterns
 * simulating woven fabric like cotton dobok material.
 *
 * @param config - Fabric texture configuration
 * @returns CanvasTexture with weave pattern
 *
 * @korean 직물색상맵생성
 */
export const generateFabricColorMap = (
  config: FabricTextureConfig,
): THREE.CanvasTexture => {
  // Return fallback for test environments without canvas support
  Eif (!canCreateCanvas()) {
    return createFallbackTexture();
  }
 
  const { baseColor, weaveDensity, resolution, threadVariation } = config;
 
  const canvas = document.createElement("canvas");
  canvas.width = resolution;
  canvas.height = resolution;
  const ctx = canvas.getContext("2d");
 
  if (!ctx) {
    return createFallbackTexture();
  }
 
  const rgb = hexToRgb(baseColor);
 
  // Fill base color
  ctx.fillStyle = baseColor;
  ctx.fillRect(0, 0, resolution, resolution);
 
  // Create weave pattern
  const threadWidth = resolution / weaveDensity;
 
  // Horizontal threads (weft)
  for (let y = 0; y < weaveDensity; y++) {
    const yPos = y * threadWidth;
    const variation = (seededRandom(y * 17) - 0.5) * threadVariation * 255;
 
    const r = Math.max(0, Math.min(255, rgb.r + variation));
    const g = Math.max(0, Math.min(255, rgb.g + variation));
    const b = Math.max(0, Math.min(255, rgb.b + variation));
 
    ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
 
    // Draw thread with slight offset pattern
    for (let x = 0; x < weaveDensity; x++) {
      const xPos = x * threadWidth;
      // Alternating over/under pattern
      if ((x + y) % 2 === 0) {
        ctx.fillRect(xPos, yPos, threadWidth * 0.95, threadWidth * 0.45);
      }
    }
  }
 
  // Vertical threads (warp)
  for (let x = 0; x < weaveDensity; x++) {
    const xPos = x * threadWidth;
    const variation = (seededRandom(x * 31) - 0.5) * threadVariation * 255;
 
    const r = Math.max(0, Math.min(255, rgb.r + variation * 0.8));
    const g = Math.max(0, Math.min(255, rgb.g + variation * 0.8));
    const b = Math.max(0, Math.min(255, rgb.b + variation * 0.8));
 
    ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
 
    for (let y = 0; y < weaveDensity; y++) {
      const yPos = y * threadWidth;
      // Opposite pattern for weave effect
      if ((x + y) % 2 === 1) {
        ctx.fillRect(xPos, yPos, threadWidth * 0.45, threadWidth * 0.95);
      }
    }
  }
 
  const texture = new THREE.CanvasTexture(canvas);
  texture.wrapS = THREE.RepeatWrapping;
  texture.wrapT = THREE.RepeatWrapping;
  texture.repeat.set(4, 4); // Tile the texture
  texture.needsUpdate = true;
 
  return texture;
};
 
/**
 * Generate a normal map for fabric surface detail
 *
 * Creates subtle bumps and ridges that simulate thread
 * texture on the fabric surface for enhanced realism.
 *
 * @param config - Fabric texture configuration
 * @returns CanvasTexture normal map
 *
 * @korean 법선맵생성
 */
export const generateFabricNormalMap = (
  config: FabricTextureConfig,
): THREE.CanvasTexture => {
  // Return fallback for test environments without canvas support
  Eif (!canCreateCanvas()) {
    return createFallbackTexture();
  }
 
  const { weaveDensity, resolution } = config;
 
  const canvas = document.createElement("canvas");
  canvas.width = resolution;
  canvas.height = resolution;
  const ctx = canvas.getContext("2d");
 
  if (!ctx) {
    return createFallbackTexture();
  }
 
  // Neutral normal (pointing straight out)
  ctx.fillStyle = "rgb(128, 128, 255)";
  ctx.fillRect(0, 0, resolution, resolution);
 
  const threadWidth = resolution / weaveDensity;
 
  // Create normal variations for weave pattern
  for (let y = 0; y < weaveDensity; y++) {
    for (let x = 0; x < weaveDensity; x++) {
      const xPos = x * threadWidth;
      const yPos = y * threadWidth;
 
      // Create subtle normal variation based on weave position
      if ((x + y) % 2 === 0) {
        // Horizontal thread - slight upward normal
        ctx.fillStyle = "rgb(128, 140, 255)";
        ctx.fillRect(xPos, yPos, threadWidth * 0.9, threadWidth * 0.4);
      } else {
        // Vertical thread - slight rightward normal
        ctx.fillStyle = "rgb(140, 128, 255)";
        ctx.fillRect(xPos, yPos, threadWidth * 0.4, threadWidth * 0.9);
      }
 
      // Thread edge highlights
      const edgeIntensity = 20;
      ctx.fillStyle = `rgb(${128 + edgeIntensity}, ${128 + edgeIntensity}, 255)`;
      ctx.fillRect(xPos, yPos, threadWidth * 0.1, threadWidth);
      ctx.fillRect(xPos, yPos, threadWidth, threadWidth * 0.1);
    }
  }
 
  const texture = new THREE.CanvasTexture(canvas);
  texture.wrapS = THREE.RepeatWrapping;
  texture.wrapT = THREE.RepeatWrapping;
  texture.repeat.set(4, 4);
  texture.needsUpdate = true;
 
  return texture;
};
 
/**
 * Generate a roughness map for fabric material variation
 *
 * Creates subtle roughness variations that simulate the
 * different reflective properties of woven threads.
 *
 * @param config - Fabric texture configuration
 * @returns CanvasTexture roughness map
 *
 * @korean 거칠기맵생성
 */
export const generateFabricRoughnessMap = (
  config: FabricTextureConfig,
): THREE.CanvasTexture => {
  // Return fallback for test environments without canvas support
  Eif (!canCreateCanvas()) {
    return createFallbackTexture();
  }
 
  const { weaveDensity, resolution, threadVariation } = config;
 
  const canvas = document.createElement("canvas");
  canvas.width = resolution;
  canvas.height = resolution;
  const ctx = canvas.getContext("2d");
 
  if (!ctx) {
    return createFallbackTexture();
  }
 
  // Base roughness (white = rough, black = smooth)
  ctx.fillStyle = "rgb(180, 180, 180)";
  ctx.fillRect(0, 0, resolution, resolution);
 
  const threadWidth = resolution / weaveDensity;
 
  // Create roughness variation for weave pattern
  for (let y = 0; y < weaveDensity; y++) {
    for (let x = 0; x < weaveDensity; x++) {
      const xPos = x * threadWidth;
      const yPos = y * threadWidth;
 
      // Random roughness variation per thread
      const variation = seededRandom(x * 13 + y * 29) * threadVariation * 50;
      const roughness = Math.max(128, Math.min(230, 180 + variation));
 
      ctx.fillStyle = `rgb(${roughness}, ${roughness}, ${roughness})`;
      ctx.fillRect(xPos, yPos, threadWidth * 0.9, threadWidth * 0.9);
 
      // Thread gaps are slightly smoother
      ctx.fillStyle = "rgb(160, 160, 160)";
      ctx.fillRect(
        xPos + threadWidth * 0.9,
        yPos,
        threadWidth * 0.1,
        threadWidth,
      );
      ctx.fillRect(
        xPos,
        yPos + threadWidth * 0.9,
        threadWidth,
        threadWidth * 0.1,
      );
    }
  }
 
  const texture = new THREE.CanvasTexture(canvas);
  texture.wrapS = THREE.RepeatWrapping;
  texture.wrapT = THREE.RepeatWrapping;
  texture.repeat.set(4, 4);
  texture.needsUpdate = true;
 
  return texture;
};
 
/**
 * Generate complete fabric texture set with all maps
 *
 * Creates a set of textures (color, normal, roughness) for
 * realistic fabric rendering with proper memory management.
 *
 * @param baseColor - Base color for the fabric (hex string)
 * @param preset - Preset name or custom config
 * @returns FabricTextureSet with all generated textures
 *
 * @example
 * ```typescript
 * const textures = generateFabricTextureSet("#2d2d2d", "dobok");
 *
 * const material = new THREE.MeshPhysicalMaterial({
 *   map: textures.colorMap,
 *   normalMap: textures.normalMap,
 *   roughnessMap: textures.roughnessMap,
 * });
 *
 * // Cleanup when done
 * textures.dispose();
 * ```
 *
 * @korean 완전직물텍스처세트생성
 */
export const generateFabricTextureSet = (
  baseColor: string,
  preset: keyof typeof FABRIC_PRESETS | Partial<FabricTextureConfig> = "dobok",
): FabricTextureSet => {
  // Merge preset with defaults
  const presetConfig =
    typeof preset === "string" ? FABRIC_PRESETS[preset] : preset;
 
  const config: FabricTextureConfig = {
    baseColor,
    weaveDensity: presetConfig?.weaveDensity ?? 32,
    resolution: presetConfig?.resolution ?? 256,
    threadVariation: presetConfig?.threadVariation ?? 0.15,
    generateNormalMap: presetConfig?.generateNormalMap ?? true,
    generateRoughnessMap: presetConfig?.generateRoughnessMap ?? true,
  };
 
  // Generate textures
  const colorMap = generateFabricColorMap(config);
  const normalMap = config.generateNormalMap
    ? generateFabricNormalMap(config)
    : undefined;
  const roughnessMap = config.generateRoughnessMap
    ? generateFabricRoughnessMap(config)
    : undefined;
 
  // Cleanup function
  const dispose = () => {
    colorMap.dispose();
    normalMap?.dispose();
    roughnessMap?.dispose();
  };
 
  return {
    colorMap,
    normalMap,
    roughnessMap,
    dispose,
  };
};
 
/**
 * Pre-defined dobok colors for Korean martial arts uniforms
 */
export const DOBOK_COLORS = {
  /** Traditional white dobok (흰 도복) */
  WHITE: "#f5f5f5",
  /** Black dobok for masters (검정 도복) */
  BLACK: "#1a1a1a",
  /** Navy blue tactical (남색) */
  NAVY: "#1a2744",
  /** Traditional Korean gray (회색) */
  GRAY: "#2d2d2d",
  /** Dark red for elite (암적색) */
  DARK_RED: "#4a1a1a",
  /** Cyber cyan accent */
  CYBER_CYAN: "#003333",
} as const;
 
export default generateFabricTextureSet;