All files / src/view mat-texture.ts

0% Statements 0/160
0% Branches 0/1
0% Functions 0/1
0% Lines 0/160

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import * as THREE from 'three';
import { MatColorSampler } from '../sensors/sensor-manager';
import { resolveAssetUrl } from '../utils/asset-path';
 
export type MatMapType = 'numbered' | 'grid' | 'procedural';
 
export class CompetitionMatTexture implements MatColorSampler {
  public canvas: HTMLCanvasElement;
  public ctx: CanvasRenderingContext2D;
  public texture: THREE.CanvasTexture;
 
  // Real world dimensions (meters): 2.40m x 1.40m (matches official FLL mat aspect ratio)
  public worldLength = 2.40;
  public worldWidth = 1.40;
 
  private canvasWidth = 2048;
  private canvasHeight = 1024;
  private pixelData: Uint8ClampedArray | null = null;
  public currentMap: MatMapType = 'numbered';
 
  constructor() {
    this.canvas = document.createElement('canvas');
    this.canvas.width = this.canvasWidth;
    this.canvas.height = this.canvasHeight;
    this.ctx = this.canvas.getContext('2d', { willReadFrequently: true })!;
 
    // Initial procedural render so canvas is valid immediately
    this.drawCompetitionMat();
    this.texture = new THREE.CanvasTexture(this.canvas);
    this.texture.anisotropy = 8;
    this.texture.wrapS = THREE.ClampToEdgeWrapping;
    this.texture.wrapT = THREE.ClampToEdgeWrapping;
 
    // Cache image data for fast CPU sensor sampling
    this.cacheImageData();
 
    // Default to the official BioGlow numbered competition map
    this.loadMap('numbered').catch(() => {});
  }
 
  /**
   * Switches active competition mat between numbered field mat, grid image, and procedural canvas
   */
  public async loadMap(mapType: MatMapType): Promise<void> {
    this.currentMap = mapType;
    if (mapType === 'procedural') {
      this.drawCompetitionMat();
      this.cacheImageData();
      this.texture.needsUpdate = true;
      return;
    }
 
    // In environments without Image constructor (e.g. Node tests), remain procedural
    if (typeof Image === 'undefined') {
      this.drawCompetitionMat();
      this.cacheImageData();
      return;
    }
 
    const src = mapType === 'numbered'
      ? resolveAssetUrl('maps/bioglow_numbered_mat.png')
      : resolveAssetUrl('maps/playing_field_grid.png');
 
    return new Promise<void>((resolve) => {
      const img = new Image();
      img.crossOrigin = 'anonymous';
      img.onload = () => {
        this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
        this.ctx.drawImage(img, 0, 0, this.canvasWidth, this.canvasHeight);
        this.cacheImageData();
        this.texture.needsUpdate = true;
        resolve();
      };
      img.onerror = () => {
        // Fallback to procedural if image fails to load
        this.drawCompetitionMat();
        this.cacheImageData();
        this.texture.needsUpdate = true;
        resolve();
      };
      img.src = src;
    });
  }
 
  private cacheImageData(): void {
    const imgData = this.ctx.getImageData(0, 0, this.canvasWidth, this.canvasHeight);
    this.pixelData = imgData.data;
  }
 
  /**
   * Draws a competition-ready FLL mat texture with launch area, lines, and target zones
   */
  public drawCompetitionMat(): void {
    const ctx = this.ctx;
    const w = this.canvasWidth;
    const h = this.canvasHeight;
 
    // 1. Base Vinyl Canvas (light off-white matte)
    ctx.fillStyle = '#f8f9fa';
    ctx.fillRect(0, 0, w, h);
 
    // 2. Subtle Grid Marks
    ctx.strokeStyle = '#e9ecef';
    ctx.lineWidth = 1;
    for (let x = 0; x < w; x += 64) {
      ctx.beginPath();
      ctx.moveTo(x, 0);
      ctx.lineTo(x, h);
      ctx.stroke();
    }
    for (let y = 0; y < h; y += 64) {
      ctx.beginPath();
      ctx.moveTo(0, y);
      ctx.lineTo(w, y);
      ctx.stroke();
    }
 
    // 3. Launch Area (Home Zone) at Bottom-Left (West/South)
    // In FLL, Launch Area is a designated quarter or rectangle marked by thick lines
    const launchW = w * 0.28;
    const launchH = h * 0.45;
    ctx.fillStyle = 'rgba(230, 240, 255, 0.5)';
    ctx.fillRect(20, h - launchH - 20, launchW, launchH);
 
    ctx.strokeStyle = '#1e3a8a';
    ctx.lineWidth = 6;
    ctx.strokeRect(20, h - launchH - 20, launchW, launchH);
 
    ctx.fillStyle = '#1e3a8a';
    ctx.font = 'bold 24px monospace';
    ctx.fillText('HOME / LAUNCH AREA', 40, h - launchH + 30);
 
    // 4. Mission Target Zones (Colored rings & boxes)
    // Red Mission Zone
    ctx.fillStyle = 'rgba(239, 68, 68, 0.85)';
    ctx.beginPath();
    ctx.arc(w * 0.55, h * 0.35, 75, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = '#991b1b';
    ctx.lineWidth = 5;
    ctx.stroke();
 
    // Green Mission Zone
    ctx.fillStyle = 'rgba(34, 197, 94, 0.85)';
    ctx.beginPath();
    ctx.arc(w * 0.8, h * 0.7, 85, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = '#166534';
    ctx.lineWidth = 5;
    ctx.stroke();
 
    // Blue Mission Box
    ctx.fillStyle = 'rgba(59, 130, 246, 0.85)';
    ctx.fillRect(w * 0.75 - 75, h * 0.25 - 60, 150, 120);
    ctx.strokeStyle = '#1e40af';
    ctx.lineWidth = 5;
    ctx.strokeRect(w * 0.75 - 75, h * 0.25 - 60, 150, 120);
 
    // Yellow Caution Zone
    ctx.fillStyle = 'rgba(234, 179, 8, 0.85)';
    ctx.fillRect(w * 0.4 - 60, h * 0.65 - 60, 120, 120);
    ctx.strokeStyle = '#854d0e';
    ctx.lineWidth = 5;
    ctx.strokeRect(w * 0.4 - 60, h * 0.65 - 60, 120, 120);
 
    // 5. Main FLL Navigation Lines (Crisp 25mm black lines for line-following)
    ctx.strokeStyle = '#111827';
    ctx.lineWidth = 22; // ~25mm line width on 2048px canvas
    ctx.lineCap = 'round';
    ctx.lineJoin = 'round';
 
    // Main Track Loop
    ctx.beginPath();
    // Line 1: Exit from Launch Area heading East
    ctx.moveTo(launchW + 20, h - launchH * 0.5 - 20);
    ctx.lineTo(w * 0.5, h - launchH * 0.5 - 20);
    // Smooth turn towards North-East
    ctx.lineTo(w * 0.65, h * 0.5);
    ctx.lineTo(w * 0.8, h * 0.5);
    // Up to Blue zone
    ctx.lineTo(w * 0.8, h * 0.25);
    // Across top towards West
    ctx.lineTo(w * 0.35, h * 0.25);
    // Down to yellow zone
    ctx.lineTo(w * 0.35, h * 0.55);
    // T-junction line
    ctx.stroke();
 
    // Secondary Cross Line (for squaring off)
    ctx.beginPath();
    ctx.moveTo(w * 0.48, h * 0.15);
    ctx.lineTo(w * 0.48, h * 0.85);
    ctx.stroke();
 
    // Perimeter boundary border
    ctx.strokeStyle = '#374151';
    ctx.lineWidth = 8;
    ctx.strokeRect(4, 4, w - 8, h - 8);
  }
 
  /**
   * Sample RGB at world coordinates (worldX: -halfL to +halfL, worldZ: -halfW to +halfW)
   */
  public sampleAt(worldX: number, worldZ: number): { r: number; g: number; b: number } {
    if (!this.pixelData) {
      return { r: 255, g: 255, b: 255 };
    }
 
    const halfL = this.worldLength / 2;
    const halfW = this.worldWidth / 2;
 
    // Normalize coordinates to 0..1 UV
    const u = (worldX + halfL) / this.worldLength;
    const v = (worldZ + halfW) / this.worldWidth;
 
    if (u < 0 || u > 1 || v < 0 || v > 1) {
      return { r: 245, g: 245, b: 245 };
    }
 
    const px = Math.min(this.canvasWidth - 1, Math.max(0, Math.floor(u * this.canvasWidth)));
    const py = Math.min(this.canvasHeight - 1, Math.max(0, Math.floor(v * this.canvasHeight)));
 
    const idx = (py * this.canvasWidth + px) * 4;
    return {
      r: this.pixelData[idx],
      g: this.pixelData[idx + 1],
      b: this.pixelData[idx + 2],
    };
  }
}