|
| 1 | +const ATLAS_WIDTH = 2048; |
| 2 | +const ATLAS_HEIGHT = 2048; |
| 3 | +const MAX_CELL_SIZE = 128; |
| 4 | +const PADDING = 2; |
| 5 | + |
| 6 | +export interface ImageAtlasEntry { |
| 7 | + u0: number; |
| 8 | + v0: number; |
| 9 | + u1: number; |
| 10 | + v1: number; |
| 11 | + aspect: number; |
| 12 | +} |
| 13 | + |
| 14 | +interface Shelf { |
| 15 | + y: number; |
| 16 | + height: number; |
| 17 | + x: number; |
| 18 | +} |
| 19 | + |
| 20 | +interface PendingImage { |
| 21 | + image: HTMLImageElement; |
| 22 | + loaded: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +export class ImageAtlas { |
| 26 | + private _gl: WebGL2RenderingContext; |
| 27 | + private _canvas: HTMLCanvasElement; |
| 28 | + private _ctx: CanvasRenderingContext2D; |
| 29 | + private _texture: WebGLTexture | null = null; |
| 30 | + private _cache = new Map<string, ImageAtlasEntry>(); |
| 31 | + private _pending = new Map<string, PendingImage>(); |
| 32 | + private _shelves: Shelf[] = []; |
| 33 | + private _dirty = false; |
| 34 | + private _textureAllocated = false; |
| 35 | + |
| 36 | + constructor(gl: WebGL2RenderingContext) { |
| 37 | + this._gl = gl; |
| 38 | + this._canvas = document.createElement('canvas'); |
| 39 | + this._canvas.width = ATLAS_WIDTH; |
| 40 | + this._canvas.height = ATLAS_HEIGHT; |
| 41 | + this._ctx = this._canvas.getContext('2d', { willReadFrequently: false })!; |
| 42 | + this._texture = gl.createTexture(); |
| 43 | + |
| 44 | + gl.bindTexture(gl.TEXTURE_2D, this._texture); |
| 45 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 46 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
| 47 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 48 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 49 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 50 | + } |
| 51 | + |
| 52 | + getOrCreate(url: string): ImageAtlasEntry | null { |
| 53 | + const cached = this._cache.get(url); |
| 54 | + if (cached) { |
| 55 | + return cached; |
| 56 | + } |
| 57 | + |
| 58 | + const pending = this._pending.get(url); |
| 59 | + if (pending) { |
| 60 | + if (!pending.loaded) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + return this._packImage(url, pending.image); |
| 64 | + } |
| 65 | + |
| 66 | + const image = new Image(); |
| 67 | + image.crossOrigin = 'anonymous'; |
| 68 | + |
| 69 | + const record: PendingImage = { image, loaded: false }; |
| 70 | + this._pending.set(url, record); |
| 71 | + |
| 72 | + image.onload = () => { |
| 73 | + record.loaded = true; |
| 74 | + }; |
| 75 | + image.onerror = () => { |
| 76 | + this._pending.delete(url); |
| 77 | + }; |
| 78 | + image.src = url; |
| 79 | + |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + bind(unit: number): void { |
| 84 | + const gl = this._gl; |
| 85 | + gl.activeTexture(gl.TEXTURE0 + unit); |
| 86 | + gl.bindTexture(gl.TEXTURE_2D, this._texture); |
| 87 | + } |
| 88 | + |
| 89 | + uploadIfDirty(): void { |
| 90 | + if (!this._dirty) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const gl = this._gl; |
| 95 | + gl.bindTexture(gl.TEXTURE_2D, this._texture); |
| 96 | + |
| 97 | + if (!this._textureAllocated) { |
| 98 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, ATLAS_WIDTH, ATLAS_HEIGHT, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
| 99 | + this._textureAllocated = true; |
| 100 | + } |
| 101 | + |
| 102 | + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, this._canvas); |
| 103 | + |
| 104 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 105 | + this._dirty = false; |
| 106 | + } |
| 107 | + |
| 108 | + clear(): void { |
| 109 | + this._cache.clear(); |
| 110 | + this._pending.clear(); |
| 111 | + this._shelves = []; |
| 112 | + this._dirty = false; |
| 113 | + this._ctx.clearRect(0, 0, ATLAS_WIDTH, ATLAS_HEIGHT); |
| 114 | + } |
| 115 | + |
| 116 | + private _packImage(url: string, image: HTMLImageElement): ImageAtlasEntry | null { |
| 117 | + if (!image.naturalWidth || !image.naturalHeight) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + const aspect = image.naturalWidth / image.naturalHeight; |
| 122 | + |
| 123 | + let drawW: number; |
| 124 | + let drawH: number; |
| 125 | + if (image.naturalWidth >= image.naturalHeight) { |
| 126 | + drawW = Math.min(image.naturalWidth, MAX_CELL_SIZE); |
| 127 | + drawH = Math.round(drawW / aspect); |
| 128 | + } else { |
| 129 | + drawH = Math.min(image.naturalHeight, MAX_CELL_SIZE); |
| 130 | + drawW = Math.round(drawH * aspect); |
| 131 | + } |
| 132 | + |
| 133 | + const cellW = drawW + PADDING * 2; |
| 134 | + const cellH = drawH + PADDING * 2; |
| 135 | + |
| 136 | + const slot = this._allocate(cellW, cellH); |
| 137 | + if (!slot) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + this._ctx.drawImage(image, slot.x + PADDING, slot.y + PADDING, drawW, drawH); |
| 142 | + |
| 143 | + const entry: ImageAtlasEntry = { |
| 144 | + u0: (slot.x + PADDING) / ATLAS_WIDTH, |
| 145 | + v0: (slot.y + PADDING) / ATLAS_HEIGHT, |
| 146 | + u1: (slot.x + PADDING + drawW) / ATLAS_WIDTH, |
| 147 | + v1: (slot.y + PADDING + drawH) / ATLAS_HEIGHT, |
| 148 | + aspect, |
| 149 | + }; |
| 150 | + |
| 151 | + this._cache.set(url, entry); |
| 152 | + this._pending.delete(url); |
| 153 | + this._dirty = true; |
| 154 | + return entry; |
| 155 | + } |
| 156 | + |
| 157 | + private _allocate(w: number, h: number): { x: number; y: number } | null { |
| 158 | + for (let i = 0; i < this._shelves.length; i++) { |
| 159 | + const shelf = this._shelves[i]; |
| 160 | + if (shelf.x + w <= ATLAS_WIDTH && h <= shelf.height) { |
| 161 | + const pos = { x: shelf.x, y: shelf.y }; |
| 162 | + shelf.x += w; |
| 163 | + return pos; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + const shelfY = |
| 168 | + this._shelves.length === 0 |
| 169 | + ? 0 |
| 170 | + : this._shelves[this._shelves.length - 1].y + this._shelves[this._shelves.length - 1].height; |
| 171 | + |
| 172 | + if (shelfY + h > ATLAS_HEIGHT) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + const newShelf: Shelf = { y: shelfY, height: h, x: w }; |
| 177 | + this._shelves.push(newShelf); |
| 178 | + return { x: 0, y: shelfY }; |
| 179 | + } |
| 180 | +} |
0 commit comments