| 1 | import { applyMagentaTintToImageData, applyDualChannelTintToImageData } from "/shared/render/tint.js"; |
| 2 | import { layersForHairPreview } from "/shared/appearance/hair.js"; |
| 3 | import { PIVOT_FROM_BOTTOM_PX } from "/shared/protocol/constants.js"; |
| 4 | |
| 5 | /** |
| 6 | * Draw stacked character layers (body + hair + optional gear) onto a canvas. |
| 7 | * Magenta hair/gear tints applied in 2D for UI previews. |
| 8 | * Layers may differ in size (e.g. body 16×24, shirt equip 20×24); they share |
| 9 | * the world pivot (horizontal centre, `PIVOT_FROM_BOTTOM_PX` from the bottom). |
| 10 | */ |
| 11 | export async function paintCharacterPreview (canvas, { |
| 12 | sex, |
| 13 | bag = null, |
| 14 | hair = null, |
| 15 | hideHeadgear = true, |
| 16 | resolveSpriteUrl, |
| 17 | scale = 4 |
| 18 | } = {}) |
| 19 | { |
| 20 | if (!canvas) |
| 21 | { return; } |
| 22 | const layers = layersForHairPreview(sex, bag, hair, { hideHeadgear }); |
| 23 | const images = []; |
| 24 | for (const layer of layers) |
| 25 | { |
| 26 | const url = resolveSpriteUrl?.(layer.sprite); |
| 27 | if (!url) |
| 28 | { continue; } |
| 29 | const img = await loadHtmlImage(url); |
| 30 | images.push({ img, tint: layer.tint ?? null }); |
| 31 | } |
| 32 | if (!images.length) |
| 33 | { return; } |
| 34 | |
| 35 | let maxW = 1; |
| 36 | let maxH = 1; |
| 37 | for (const entry of images) |
| 38 | { |
| 39 | maxW = Math.max(maxW, entry.img.naturalWidth || entry.img.width || 1); |
| 40 | maxH = Math.max(maxH, entry.img.naturalHeight || entry.img.height || 1); |
| 41 | } |
| 42 | |
| 43 | canvas.width = maxW * scale; |
| 44 | canvas.height = maxH * scale; |
| 45 | const ctx = canvas.getContext("2d"); |
| 46 | ctx.imageSmoothingEnabled = false; |
| 47 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 48 | |
| 49 | const pivotX = maxW * 0.5; |
| 50 | const pivotY = maxH - PIVOT_FROM_BOTTOM_PX; |
| 51 | for (const entry of images) |
| 52 | { |
| 53 | const source = entry.tint ? tintedCanvas(entry.img, entry.tint) : entry.img; |
| 54 | const sw = source.naturalWidth || source.width || 1; |
| 55 | const sh = source.naturalHeight || source.height || 1; |
| 56 | const ox = (pivotX - sw * 0.5) * scale; |
| 57 | const oy = (pivotY - (sh - PIVOT_FROM_BOTTOM_PX)) * scale; |
| 58 | ctx.drawImage(source, ox, oy, sw * scale, sh * scale); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Sync composite of live `appearanceLayers` onto a fixed-size canvas. |
| 64 | * Crops a top-aligned square from each frame (e.g. 16×16 from a 16×24 body) |
| 65 | * then scales that crop up to `size` (e.g. 64) without squishing the tall frame. |
| 66 | * `resolveImage(name)` should return a Framework `image` (with `g_image` / |
| 67 | * `getSubImage`) or a plain HTMLImageElement / canvas already in memory. |
| 68 | */ |
| 69 | export function paintAppearanceSnapshot (canvas, layers, { |
| 70 | resolveImage, |
| 71 | size = 64, |
| 72 | frame = 0 |
| 73 | } = {}) |
| 74 | { |
| 75 | if (!canvas) |
| 76 | { return false; } |
| 77 | const draws = []; |
| 78 | for (const layer of layers ?? []) |
| 79 | { |
| 80 | const name = layer?.sprite; |
| 81 | if (!name) |
| 82 | { continue; } |
| 83 | const entry = resolveImage?.(name); |
| 84 | if (!entry) |
| 85 | { continue; } |
| 86 | draws.push({ entry, tint: layer.tint ?? null, frame }); |
| 87 | } |
| 88 | return paintDrawListSnapshot(canvas, draws, { size }); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Composite the live world render stack (body GameObject + overlays + tints). |
| 93 | * Matches what the player sees in-world, including hair overlays. |
| 94 | */ |
| 95 | export function paintLiveEntitySnapshot (canvas, entity, { size = 64 } = {}) |
| 96 | { |
| 97 | if (!canvas || !entity?.object?.renderer?.sprite) |
| 98 | { return false; } |
| 99 | const draws = [{ |
| 100 | entry: entity.object.renderer.sprite, |
| 101 | tint: null, |
| 102 | frame: entity.object.renderer.subImage | 0 |
| 103 | }]; |
| 104 | const overlays = entity.overlays ?? []; |
| 105 | const tints = entity.overlayTints ?? []; |
| 106 | const dualTints = entity.overlayDualTints ?? []; |
| 107 | for (let index = 0; index < overlays.length; index++) |
| 108 | { |
| 109 | const sprite = overlays[index]?.renderer?.sprite; |
| 110 | if (!sprite) |
| 111 | { continue; } |
| 112 | draws.push({ |
| 113 | entry: sprite, |
| 114 | tint: tints[index] ?? null, |
| 115 | dualTint: dualTints[index] ?? null, |
| 116 | frame: overlays[index].renderer.subImage | 0 |
| 117 | }); |
| 118 | } |
| 119 | return paintDrawListSnapshot(canvas, draws, { size }); |
| 120 | } |
| 121 | |
| 122 | function paintDrawListSnapshot (canvas, draws, { size = 64 } = {}) |
| 123 | { |
| 124 | if (!canvas || !draws?.length) |
| 125 | { return false; } |
| 126 | canvas.width = size; |
| 127 | canvas.height = size; |
| 128 | const ctx = canvas.getContext("2d"); |
| 129 | ctx.imageSmoothingEnabled = false; |
| 130 | ctx.clearRect(0, 0, size, size); |
| 131 | let drew = false; |
| 132 | for (const draw of draws) |
| 133 | { |
| 134 | const entry = draw.entry; |
| 135 | const html = entry?.g_image ?? entry; |
| 136 | if (!html || !(html.naturalWidth || html.width)) |
| 137 | { continue; } |
| 138 | let sx = 0; |
| 139 | let sy = 0; |
| 140 | let sw = html.naturalWidth || html.width; |
| 141 | let sh = html.naturalHeight || html.height; |
| 142 | if (typeof entry?.getSubImage === "function") |
| 143 | { |
| 144 | const sub = entry.getSubImage(draw.frame | 0); |
| 145 | sx = sub.sx; |
| 146 | sy = sub.sy; |
| 147 | sw = sub.sw; |
| 148 | sh = sub.sh; |
| 149 | } |
| 150 | // Top-aligned square crop of the frame (e.g. 16×16 from 16×24). |
| 151 | const crop = Math.min(sw, sh); |
| 152 | const cropSx = sx + Math.floor((sw - crop) / 2); |
| 153 | const cropSy = sy; |
| 154 | let source = null; |
| 155 | if (draw.dualTint) |
| 156 | { source = dualTintedSubCanvas(html, cropSx, cropSy, crop, crop, draw.dualTint); } |
| 157 | else if (draw.tint) |
| 158 | { source = tintedSubCanvas(html, cropSx, cropSy, crop, crop, draw.tint); } |
| 159 | if (source) |
| 160 | { ctx.drawImage(source, 0, 0, size, size); } |
| 161 | else |
| 162 | { ctx.drawImage(html, cropSx, cropSy, crop, crop, 0, 0, size, size); } |
| 163 | drew = true; |
| 164 | } |
| 165 | return drew; |
| 166 | } |
| 167 | |
| 168 | function loadHtmlImage (url) |
| 169 | { |
| 170 | return new Promise((resolve, reject) => |
| 171 | { |
| 172 | const img = new Image(); |
| 173 | img.onload = () => resolve(img); |
| 174 | img.onerror = () => reject(new Error(`Failed to load ${url}`)); |
| 175 | img.src = url; |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | function tintedCanvas (sourceImg, tintRgb) |
| 180 | { |
| 181 | const canvas = document.createElement("canvas"); |
| 182 | canvas.width = sourceImg.naturalWidth || sourceImg.width; |
| 183 | canvas.height = sourceImg.naturalHeight || sourceImg.height; |
| 184 | const ctx = canvas.getContext("2d"); |
| 185 | ctx.imageSmoothingEnabled = false; |
| 186 | ctx.drawImage(sourceImg, 0, 0); |
| 187 | const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 188 | applyMagentaTintToImageData(imageData, tintRgb); |
| 189 | ctx.putImageData(imageData, 0, 0); |
| 190 | return canvas; |
| 191 | } |
| 192 | |
| 193 | function tintedSubCanvas (sourceImg, sx, sy, sw, sh, tintRgb) |
| 194 | { |
| 195 | const canvas = document.createElement("canvas"); |
| 196 | canvas.width = sw; |
| 197 | canvas.height = sh; |
| 198 | const ctx = canvas.getContext("2d"); |
| 199 | ctx.imageSmoothingEnabled = false; |
| 200 | ctx.drawImage(sourceImg, sx, sy, sw, sh, 0, 0, sw, sh); |
| 201 | const imageData = ctx.getImageData(0, 0, sw, sh); |
| 202 | applyMagentaTintToImageData(imageData, tintRgb); |
| 203 | ctx.putImageData(imageData, 0, 0); |
| 204 | return canvas; |
| 205 | } |
| 206 | |
| 207 | function dualTintedSubCanvas (sourceImg, sx, sy, sw, sh, dualTint) |
| 208 | { |
| 209 | const canvas = document.createElement("canvas"); |
| 210 | canvas.width = sw; |
| 211 | canvas.height = sh; |
| 212 | const ctx = canvas.getContext("2d"); |
| 213 | ctx.imageSmoothingEnabled = false; |
| 214 | ctx.drawImage(sourceImg, sx, sy, sw, sh, 0, 0, sw, sh); |
| 215 | const imageData = ctx.getImageData(0, 0, sw, sh); |
| 216 | applyDualChannelTintToImageData(imageData, dualTint); |
| 217 | ctx.putImageData(imageData, 0, 0); |
| 218 | return canvas; |
| 219 | } |