param( [string]$WorkspaceRoot = (Split-Path -Parent $PSScriptRoot), [int]$Padding = 8 ) Add-Type -AssemblyName System.Drawing $helperSource = @' using System; using System.Collections; using System.Drawing; using System.Drawing.Imaging; public sealed class AlphaMaskedComponent { public int Id { get; set; } public int Label { get; set; } public int Area { get; set; } public Rectangle Bounds { get; set; } public Bitmap Image { get; set; } } public static class AlphaMaskExtractor { public static AlphaMaskedComponent[] ExtractAll(string path, int alphaThreshold, int minimumArea, int padding) { using (var source = new Bitmap(path)) { int width = source.Width; int height = source.Height; var foreground = new bool[width * height]; var labels = new int[width * height]; var queue = new int[width * height]; for (int y = 0; y < height; y++) { int row = y * width; for (int x = 0; x < width; x++) foreground[row + x] = source.GetPixel(x, y).A >= alphaThreshold; } var components = new ArrayList(); int[] dx = { -1, 0, 1, -1, 1, -1, 0, 1 }; int[] dy = { -1, -1, -1, 0, 0, 1, 1, 1 }; int nextLabel = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int start = y * width + x; if (labels[start] != 0 || !foreground[start]) continue; nextLabel++; int head = 0, tail = 0, area = 0; int minX = x, minY = y, maxX = x, maxY = y; labels[start] = nextLabel; queue[tail++] = start; while (head < tail) { int index = queue[head++]; int px = index % width; int py = index / width; area++; if (px < minX) minX = px; if (px > maxX) maxX = px; if (py < minY) minY = py; if (py > maxY) maxY = py; for (int i = 0; i < 8; i++) { int nx = px + dx[i]; int ny = py + dy[i]; if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue; int next = ny * width + nx; if (labels[next] != 0 || !foreground[next]) continue; labels[next] = nextLabel; queue[tail++] = next; } } if (area >= minimumArea) { components.Add(new AlphaMaskedComponent { Label = nextLabel, Area = area, Bounds = Rectangle.FromLTRB(minX, minY, maxX + 1, maxY + 1) }); } } } var result = new AlphaMaskedComponent[components.Count]; for (int i = 0; i < components.Count; i++) result[i] = (AlphaMaskedComponent)components[i]; Array.Sort(result, (a, b) => { int rowCompare = a.Bounds.Top.CompareTo(b.Bounds.Top); return rowCompare != 0 ? rowCompare : a.Bounds.Left.CompareTo(b.Bounds.Left); }); for (int i = 0; i < result.Length; i++) { var component = result[i]; component.Id = i + 1; var bitmap = new Bitmap(component.Bounds.Width + padding * 2, component.Bounds.Height + padding * 2, PixelFormat.Format32bppArgb); for (int py = component.Bounds.Top; py < component.Bounds.Bottom; py++) { for (int px = component.Bounds.Left; px < component.Bounds.Right; px++) { int sourceIndex = py * width + px; if (labels[sourceIndex] != component.Label) continue; bitmap.SetPixel(px - component.Bounds.Left + padding, py - component.Bounds.Top + padding, source.GetPixel(px, py)); } } component.Image = bitmap; } return result; } } } '@ $compilerReferences = @( [System.Drawing.Bitmap].Assembly.Location, [System.Drawing.Color].Assembly.Location, [System.Reflection.Assembly]::Load('System.Private.Windows.GdiPlus').Location, [System.Reflection.Assembly]::Load('System.Private.Windows.Core').Location, [System.Reflection.Assembly]::Load('System.Collections').Location, [System.Reflection.Assembly]::Load('System.Runtime').Location, [System.Object].Assembly.Location ) | Select-Object -Unique Add-Type -TypeDefinition $helperSource -ReferencedAssemblies $compilerReferences $sourcePath = Join-Path $WorkspaceRoot 'assets\images\2d-game-characters\02-xinyin\source\xinyin-front-right-parts-sheet-v1.png' $characterRoot = Join-Path $WorkspaceRoot 'assets\images\2d-game-characters\02-xinyin' $outputDirectory = Join-Path $characterRoot 'parts\front-right\v1' $referenceDirectory = Join-Path $characterRoot 'reference' $previewDirectory = Join-Path $characterRoot 'previews' $manifestPath = Join-Path $outputDirectory 'parts-manifest.json' $contactSheetPath = Join-Path $previewDirectory 'xinyin-front-right-extracted-parts-v1.png' $referencePath = Join-Path $referenceDirectory 'xinyin-front-right-assembled-reference-v1.png' New-Item -ItemType Directory -Force -Path $outputDirectory, $referenceDirectory, $previewDirectory | Out-Null # Coordinates come from the alpha-component diagnostic. Component 11 is the assembled reference. # Pivot values are normalized inside the non-padding content bounds, not the exported canvas. $parts = @( @{ Id=1; Name='hair-back-main'; Label='后发主体'; Category='hair'; Rect=@(836,23,305,270); Pivot=@(.50,.08); Layer=-100; Notes='两个马尾方案被细发丝粘连;首次装配验证判定为参考件,不直接进入骨骼' }, @{ Id=2; Name='hair-back-segment-02'; Label='后发分片 02'; Category='hair'; Rect=@(1130,39,201,247); Pivot=@(.40,.08); Layer=-98; Notes='方向归属待装配确认' }, @{ Id=3; Name='hair-back-segment-04'; Label='后发分片 04'; Category='hair'; Rect=@(1484,53,140,187); Pivot=@(.45,.08); Layer=-94; Notes='首次装配 v2 选用的后发主片' }, @{ Id=4; Name='hair-front'; Label='前发与侧鬓'; Category='hair'; Rect=@(522,54,130,179); Pivot=@(.50,.12); Layer=35; Notes='位于面部之前' }, @{ Id=5; Name='hair-side-segment-01'; Label='侧发分片 01'; Category='hair'; Rect=@(666,62,90,171); Pivot=@(.50,.08); Layer=-88; Notes='方向归属待装配确认' }, @{ Id=6; Name='hair-back-segment-03'; Label='后发分片 03'; Category='hair'; Rect=@(1330,66,132,186); Pivot=@(.45,.08); Layer=-96; Notes='首次装配 v2 选用的后发辅片' }, @{ Id=7; Name='head-face'; Label='头部与面部'; Category='body'; Rect=@(427,73,86,115); Pivot=@(.50,.91); Layer=30; Notes='枢轴靠近颈部连接点' }, @{ Id=8; Name='hair-side-segment-02'; Label='侧发分片 02'; Category='hair'; Rect=@(778,76,56,157); Pivot=@(.50,.07); Layer=-86; Notes='方向归属待装配确认' }, @{ Id=9; Name='hair-ornament-long'; Label='长发饰'; Category='ornament'; Rect=@(1629,96,50,147); Pivot=@(.50,.08); Layer=42; Notes='含花饰与垂坠' }, @{ Id=10; Name='hair-ornament-short'; Label='短发饰'; Category='ornament'; Rect=@(1695,96,30,98); Pivot=@(.50,.08); Layer=41; Notes='含花饰与流苏' }, @{ Id=12; Name='neck'; Label='颈部'; Category='body'; Rect=@(449,211,43,45); Pivot=@(.50,.75); Layer=5; Notes='头部和躯干之间' }, @{ Id=13; Name='torso-upper'; Label='上半身躯干'; Category='body'; Rect=@(401,260,124,156); Pivot=@(.50,.92); Layer=10; Notes='根枢轴靠近腰线' }, @{ Id=14; Name='waist-guard'; Label='腰封与短裙'; Category='body'; Rect=@(536,285,183,186); Pivot=@(.50,.18); Layer=4; Notes='上端连接躯干' }, @{ Id=15; Name='upper-arm-sleeve-a'; Label='袖片与上臂 A'; Category='arm'; Rect=@(843,301,120,202); Pivot=@(.58,.08); Layer=14; Notes='左右归属待装配确认' }, @{ Id=16; Name='pelvis'; Label='骨盆与内层短裤'; Category='body'; Rect=@(720,303,107,102); Pivot=@(.50,.16); Layer=2; Notes='角色主根节点候选' }, @{ Id=17; Name='upper-arm-sleeve-b'; Label='袖片与上臂 B'; Category='arm'; Rect=@(996,304,106,211); Pivot=@(.42,.08); Layer=14; Notes='左右归属待装配确认' }, @{ Id=18; Name='forearm-hand-a'; Label='前臂与手 A'; Category='arm'; Rect=@(1110,321,59,222); Pivot=@(.50,.08); Layer=16; Notes='肘部连接点在上端' }, @{ Id=19; Name='arm-straight-variant'; Label='直臂替换件'; Category='arm'; Rect=@(1275,336,107,197);Pivot=@(.34,.08); Layer=16; Notes='备用动作变体,装配时择一使用' }, @{ Id=20; Name='forearm-hand-b'; Label='前臂与手 B'; Category='arm'; Rect=@(1184,340,83,207); Pivot=@(.50,.08); Layer=16; Notes='肘部连接点在上端' }, @{ Id=21; Name='weapon-fan-open'; Label='展开羽刃扇'; Category='weapon'; Rect=@(1634,363,99,165); Pivot=@(.50,.88); Layer=50; Notes='枢轴位于柄部' }, @{ Id=22; Name='weapon-hand-fan'; Label='持扇手替换件'; Category='arm'; Rect=@(1444,382,93,133); Pivot=@(.42,.88); Layer=48; Notes='动作替换件,含手与武器' }, @{ Id=23; Name='weapon-blade-single'; Label='单枚羽刃'; Category='weapon'; Rect=@(1563,389,29,130); Pivot=@(.50,.82); Layer=50; Notes='投掷物或手持物' }, @{ Id=24; Name='hand-open'; Label='张开手替换件'; Category='arm'; Rect=@(1391,409,55,77); Pivot=@(.50,.08); Layer=18; Notes='腕部连接点在上端' }, @{ Id=25; Name='leg-full-variant'; Label='整腿替换件'; Category='leg'; Rect=@(437,419,111,426); Pivot=@(.50,.04); Layer=-12; Notes='备用动作变体,装配时择一使用' }, @{ Id=26; Name='thigh-b'; Label='大腿 B'; Category='leg'; Rect=@(727,425,94,169); Pivot=@(.50,.05); Layer=-8; Notes='左右归属待装配确认' }, @{ Id=27; Name='thigh-a'; Label='大腿 A'; Category='leg'; Rect=@(552,471,76,133); Pivot=@(.50,.05); Layer=-8; Notes='左右归属待装配确认' }, @{ Id=28; Name='shoulder-armor-b'; Label='肩甲 B'; Category='armor'; Rect=@(1002,510,97,86); Pivot=@(.50,.18); Layer=20; Notes='左右归属待装配确认' }, @{ Id=29; Name='shoulder-armor-a'; Label='肩甲 A'; Category='armor'; Rect=@(887,512,91,85); Pivot=@(.50,.18); Layer=20; Notes='左右归属待装配确认' }, @{ Id=30; Name='shin-boot-a'; Label='小腿与靴 A'; Category='leg'; Rect=@(561,592,90,242); Pivot=@(.50,.05); Layer=-7; Notes='膝部连接点在上端' }, @{ Id=31; Name='coat-panel-03'; Label='衣摆分片 03'; Category='cloth'; Rect=@(1262,594,90,252); Pivot=@(.50,.04); Layer=-37; Notes='方向归属待装配确认' }, @{ Id=32; Name='coat-panel-05'; Label='衣摆分片 05'; Category='cloth'; Rect=@(1405,595,64,219); Pivot=@(.50,.04); Layer=-35; Notes='方向归属待装配确认' }, @{ Id=33; Name='coat-panel-06'; Label='衣摆分片 06'; Category='cloth'; Rect=@(1475,595,86,258); Pivot=@(.50,.04); Layer=-34; Notes='方向归属待装配确认' }, @{ Id=34; Name='coat-panel-02'; Label='衣摆分片 02'; Category='cloth'; Rect=@(1136,596,117,262);Pivot=@(.50,.04); Layer=-38; Notes='方向归属待装配确认' }, @{ Id=35; Name='coat-panel-01'; Label='衣摆分片 01'; Category='cloth'; Rect=@(1084,597,94,240); Pivot=@(.50,.04); Layer=-39; Notes='方向归属待装配确认' }, @{ Id=36; Name='coat-panel-08'; Label='衣摆分片 08'; Category='cloth'; Rect=@(1673,598,92,201); Pivot=@(.50,.04); Layer=-32; Notes='方向归属待装配确认' }, @{ Id=37; Name='coat-panel-07'; Label='衣摆分片 07'; Category='cloth'; Rect=@(1565,599,175,239);Pivot=@(.50,.04); Layer=-33; Notes='方向归属待装配确认' }, @{ Id=38; Name='shin-boot-b'; Label='小腿与靴 B'; Category='leg'; Rect=@(753,600,64,246); Pivot=@(.50,.05); Layer=-7; Notes='膝部连接点在上端' }, @{ Id=39; Name='coat-panel-04'; Label='衣摆分片 04'; Category='cloth'; Rect=@(1331,601,80,237); Pivot=@(.50,.04); Layer=-36; Notes='方向归属待装配确认' }, @{ Id=40; Name='front-drape'; Label='前腰布与垂饰'; Category='cloth'; Rect=@(861,614,129,266); Pivot=@(.50,.04); Layer=6; Notes='位于腰封前侧' }, @{ Id=41; Name='waist-pouch'; Label='腰包与流苏'; Category='accessory'; Rect=@(1002,620,95,143); Pivot=@(.28,.08); Layer=22; Notes='连接腰侧' } ) $source = [Drawing.Bitmap]::new($sourcePath) $maskedComponents = [AlphaMaskExtractor]::ExtractAll($sourcePath, 16, 80, $Padding) $maskedById = @{} foreach ($component in $maskedComponents) { $maskedById[$component.Id] = $component } $manifestParts = @() $exported = @() try { foreach ($part in $parts) { $rect = [Drawing.Rectangle]::new($part.Rect[0], $part.Rect[1], $part.Rect[2], $part.Rect[3]) $canvas = $maskedById[$part.Id].Image $fileName = ('fr-c{0:d2}-{1}.png' -f $part.Id, $part.Name) $filePath = Join-Path $outputDirectory $fileName $canvas.Save($filePath, [Drawing.Imaging.ImageFormat]::Png) $pivotPxX = $Padding + [Math]::Round($rect.Width * $part.Pivot[0], 2) $pivotPxY = $Padding + [Math]::Round($rect.Height * $part.Pivot[1], 2) $manifestParts += [ordered]@{ componentId = $part.Id name = $part.Name labelZh = $part.Label category = $part.Category file = $fileName sourceRect = [ordered]@{ x=$rect.X; y=$rect.Y; width=$rect.Width; height=$rect.Height } outputSize = [ordered]@{ width=$canvas.Width; height=$canvas.Height } pivot = [ordered]@{ contentNormalized = [ordered]@{ x=$part.Pivot[0]; y=$part.Pivot[1] } outputPixels = [ordered]@{ x=$pivotPxX; y=$pivotPxY } } suggestedLayer = $part.Layer notes = $part.Notes } $exported += [ordered]@{ Part=$part; Image=$canvas; FilePath=$filePath } } $reference = $maskedById[11].Image $reference.Save($referencePath, [Drawing.Imaging.ImageFormat]::Png) $manifest = [ordered]@{ characterId = '02-xinyin' characterNameZh = '汐月心音' direction = 'front-right' version = 1 status = 'extracted-unvalidated' source = 'assets/images/2d-game-characters/02-xinyin/source/xinyin-front-right-parts-sheet-v1.png' sourceSize = [ordered]@{ width=$source.Width; height=$source.Height } padding = $Padding alphaExtraction = [ordered]@{ connectivity=8; alphaThreshold=16; minimumComponentArea=80; mode='connected-component-mask' } coordinateSystem = [ordered]@{ origin = 'top-left' xPositive = 'right' yPositive = 'down' units = 'pixels' pivotBasis = 'content bounds before export padding' } warnings = @( 'Pivot values and suggested layers are initial authoring recommendations and have not been validated in an engine rig.', 'A/B limb ownership and numbered hair/coat segment ownership must be confirmed during first assembly.', 'Do not mirror asymmetric left/right art automatically.' ) assembledReference = 'assets/images/2d-game-characters/02-xinyin/reference/xinyin-front-right-assembled-reference-v1.png' partCount = $manifestParts.Count parts = $manifestParts } $manifest | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $manifestPath -Encoding utf8 $columns = 5 $cellWidth = 460 $cellHeight = 300 $rows = [Math]::Ceiling($exported.Count / $columns) $sheet = [Drawing.Bitmap]::new($columns * $cellWidth, $rows * $cellHeight, [Drawing.Imaging.PixelFormat]::Format32bppArgb) $sheetGraphics = [Drawing.Graphics]::FromImage($sheet) $sheetGraphics.SmoothingMode = [Drawing.Drawing2D.SmoothingMode]::AntiAlias $sheetGraphics.InterpolationMode = [Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $sheetGraphics.PixelOffsetMode = [Drawing.Drawing2D.PixelOffsetMode]::HighQuality $sheetGraphics.Clear([Drawing.Color]::FromArgb(255, 20, 24, 33)) $borderPen = [Drawing.Pen]::new([Drawing.Color]::FromArgb(255, 60, 74, 98), 2) $pivotPen = [Drawing.Pen]::new([Drawing.Color]::FromArgb(255, 255, 88, 112), 2) $titleBrush = [Drawing.SolidBrush]::new([Drawing.Color]::FromArgb(255, 241, 245, 252)) $metaBrush = [Drawing.SolidBrush]::new([Drawing.Color]::FromArgb(255, 154, 172, 201)) $fontTitle = [Drawing.Font]::new('Segoe UI Semibold', 14) $fontMeta = [Drawing.Font]::new('Segoe UI', 10) for ($i = 0; $i -lt $exported.Count; $i++) { $col = $i % $columns $row = [Math]::Floor($i / $columns) $x = $col * $cellWidth $y = $row * $cellHeight $sheetGraphics.DrawRectangle($borderPen, $x + 8, $y + 8, $cellWidth - 16, $cellHeight - 16) $image = $exported[$i].Image $maxWidth = 310.0 $maxHeight = 215.0 $scale = [Math]::Min($maxWidth / $image.Width, $maxHeight / $image.Height) $scale = [Math]::Min(1.0, $scale) $drawWidth = [int][Math]::Round($image.Width * $scale) $drawHeight = [int][Math]::Round($image.Height * $scale) $drawX = $x + [int](($cellWidth - $drawWidth) / 2) $drawY = $y + 42 + [int](($maxHeight - $drawHeight) / 2) $sheetGraphics.DrawImage($image, $drawX, $drawY, $drawWidth, $drawHeight) $pivot = $manifestParts[$i].pivot.outputPixels $pivotX = $drawX + [float]$pivot.x * $scale $pivotY = $drawY + [float]$pivot.y * $scale $sheetGraphics.DrawLine($pivotPen, $pivotX - 6, $pivotY, $pivotX + 6, $pivotY) $sheetGraphics.DrawLine($pivotPen, $pivotX, $pivotY - 6, $pivotX, $pivotY + 6) $title = ('C{0:d2} {1}' -f $exported[$i].Part.Id, $exported[$i].Part.Name) $sheetGraphics.DrawString($title, $fontTitle, $titleBrush, $x + 18, $y + 14) $meta = ('{0} | {1}x{2}px' -f $exported[$i].Part.Label, $image.Width, $image.Height) $sheetGraphics.DrawString($meta, $fontMeta, $metaBrush, $x + 18, $y + 267) } $sheet.Save($contactSheetPath, [Drawing.Imaging.ImageFormat]::Png) $fontMeta.Dispose() $fontTitle.Dispose() $metaBrush.Dispose() $titleBrush.Dispose() $pivotPen.Dispose() $borderPen.Dispose() $sheetGraphics.Dispose() $sheet.Dispose() } finally { foreach ($component in $maskedComponents) { if ($null -ne $component.Image) { $component.Image.Dispose() } } $source.Dispose() } Write-Output "Exported $($parts.Count) parts." Write-Output $outputDirectory Write-Output $manifestPath Write-Output $contactSheetPath Write-Output $referencePath