param( [string]$WorkspaceRoot = (Split-Path -Parent $PSScriptRoot), [int]$Padding = 10 ) Add-Type -AssemblyName System.Drawing $helperSource = @' using System; using System.Collections; using System.Drawing; using System.Drawing.Imaging; public sealed class RigCorrectionComponent { 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 RigCorrectionExtractor { public static RigCorrectionComponent[] ExtractAll(string path, int alphaThreshold, int minimumArea, int padding) { using (var source = new Bitmap(path)) { int width = source.Width, 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, 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], 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 RigCorrectionComponent { Label = nextLabel, Area = area, Bounds = Rectangle.FromLTRB(minX, minY, maxX + 1, maxY + 1) }); } } } var result = new RigCorrectionComponent[components.Count]; for (int i = 0; i < components.Count; i++) result[i] = (RigCorrectionComponent)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\rig-corrections\xinyin-front-right-rig-corrections-v2.png' $characterRoot = Join-Path $WorkspaceRoot 'assets\images\2d-game-characters\02-xinyin' $outputDirectory = Join-Path $characterRoot 'parts\front-right\rig-corrections-v1' $previewPath = Join-Path $characterRoot 'previews\xinyin-front-right-rig-corrections-v1.png' $manifestPath = Join-Path $outputDirectory 'rig-corrections-manifest.json' New-Item -ItemType Directory -Force -Path $outputDirectory | Out-Null $parts = @( @{ Id=1; Name='upper-arm-a'; Label='上臂 A'; Category='arm'; Pivot=@(.50,.06); Next=@(.50,.72); Note='肩点到肘点;不含独立银羽肩甲' }, @{ Id=2; Name='upper-arm-b'; Label='上臂 B'; Category='arm'; Pivot=@(.50,.06); Next=@(.50,.72); Note='肩点到肘点;不含独立银羽肩甲' }, @{ Id=3; Name='ponytail-root'; Label='马尾根部'; Category='hair'; Pivot=@(.34,.30); Next=@(.82,.52); Note='含绑点与银花;根部体积偏大,先用于链条验证' }, @{ Id=4; Name='ponytail-middle'; Label='马尾中段'; Category='hair'; Pivot=@(.50,.06); Next=@(.53,.88); Note='上下两端均预留重叠' }, @{ Id=5; Name='ponytail-tip'; Label='马尾末端'; Category='hair'; Pivot=@(.50,.06); Next=@(.55,.92); Note='上端与中段重叠,末端不再挂接子节' }, @{ Id=6; Name='forearm-b'; Label='前臂 B'; Category='arm'; Pivot=@(.50,.06); Next=@(.50,.92); Note='肘点到腕点;不含手' }, @{ Id=7; Name='forearm-a'; Label='前臂 A'; Category='arm'; Pivot=@(.50,.06); Next=@(.50,.92); Note='肘点到腕点;不含手' }, @{ Id=8; Name='hand-fan-grip'; Label='持扇手'; Category='hand'; Pivot=@(.82,.80); Next=$null; Note='腕点位于右下袖口' }, @{ Id=9; Name='hand-relaxed-b'; Label='放松手 B'; Category='hand'; Pivot=@(.50,.08); Next=$null; Note='腕点位于上端' }, @{ Id=10; Name='hand-relaxed-a'; Label='放松手 A'; Category='hand'; Pivot=@(.50,.08); Next=$null; Note='腕点位于上端' }, @{ Id=11; Name='hand-throw-open'; Label='投掷张手'; Category='hand'; Pivot=@(.18,.34); Next=$null; Note='腕点位于左上袖口' } ) $components = [RigCorrectionExtractor]::ExtractAll($sourcePath, 16, 120, $Padding) if ($components.Count -ne 11) { throw "Expected 11 components, detected $($components.Count)." } $componentById = @{} foreach ($component in $components) { $componentById[$component.Id] = $component } $manifestParts = @() try { foreach ($part in $parts) { $component = $componentById[$part.Id] $fileName = ('fr-rig-c{0:d2}-{1}.png' -f $part.Id, $part.Name) $filePath = Join-Path $outputDirectory $fileName $component.Image.Save($filePath, [Drawing.Imaging.ImageFormat]::Png) $rootX = $Padding + [Math]::Round($component.Bounds.Width * $part.Pivot[0], 2) $rootY = $Padding + [Math]::Round($component.Bounds.Height * $part.Pivot[1], 2) $nextJoint = $null if ($null -ne $part.Next) { $nextJoint = [ordered]@{ contentNormalized=[ordered]@{ x=$part.Next[0]; y=$part.Next[1] } outputPixels=[ordered]@{ x=$Padding+[Math]::Round($component.Bounds.Width*$part.Next[0],2) y=$Padding+[Math]::Round($component.Bounds.Height*$part.Next[1],2) } } } $manifestParts += [ordered]@{ componentId=$part.Id; name=$part.Name; labelZh=$part.Label; category=$part.Category; file=$fileName sourceRect=[ordered]@{ x=$component.Bounds.X; y=$component.Bounds.Y; width=$component.Bounds.Width; height=$component.Bounds.Height } outputSize=[ordered]@{ width=$component.Image.Width; height=$component.Image.Height } rootPivot=[ordered]@{ contentNormalized=[ordered]@{ x=$part.Pivot[0]; y=$part.Pivot[1] } outputPixels=[ordered]@{ x=$rootX; y=$rootY } } nextJoint=$nextJoint notes=$part.Note } } [ordered]@{ characterId='02-xinyin'; direction='front-right'; version=1; status='correction-parts-art-validated' source='assets/images/2d-game-characters/02-xinyin/source/rig-corrections/xinyin-front-right-rig-corrections-v2.png' generatedWith='built-in image generation tool, identity-constrained reference generation plus targeted hair edit' padding=$Padding coordinateSystem=[ordered]@{ origin='top-left'; xPositive='right'; yPositive='down'; units='pixels' } warnings=@( 'A/B screen-side ownership is validated for front-right assembly v4 and motion v3.', 'Ponytail root is larger than an ideal production root segment but passed the current chain-motion test.', 'Root pivots and next joints are art-side validated; engine interpolation remains untested.' ) partCount=$manifestParts.Count; parts=$manifestParts } | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $manifestPath -Encoding utf8 $columns=4; $cellWidth=500; $cellHeight=360 $rows=[Math]::Ceiling($parts.Count/$columns) $sheet=[Drawing.Bitmap]::new($columns*$cellWidth,$rows*$cellHeight,[Drawing.Imaging.PixelFormat]::Format32bppArgb) $g=[Drawing.Graphics]::FromImage($sheet) $g.SmoothingMode=[Drawing.Drawing2D.SmoothingMode]::AntiAlias $g.InterpolationMode=[Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $g.PixelOffsetMode=[Drawing.Drawing2D.PixelOffsetMode]::HighQuality $g.Clear([Drawing.Color]::FromArgb(255,18,22,31)) $border=[Drawing.Pen]::new([Drawing.Color]::FromArgb(255,66,82,111),2) $rootPen=[Drawing.Pen]::new([Drawing.Color]::FromArgb(255,255,87,115),3) $nextPen=[Drawing.Pen]::new([Drawing.Color]::FromArgb(255,69,211,255),3) $titleBrush=[Drawing.SolidBrush]::new([Drawing.Color]::FromArgb(255,241,245,252)) $metaBrush=[Drawing.SolidBrush]::new([Drawing.Color]::FromArgb(255,151,171,201)) $fontTitle=[Drawing.Font]::new('Segoe UI Semibold',16) $fontMeta=[Drawing.Font]::new('Segoe UI',11) for ($i=0; $i -lt $parts.Count; $i++) { $part=$parts[$i]; $component=$componentById[$part.Id]; $record=$manifestParts[$i] $col=$i%$columns; $row=[Math]::Floor($i/$columns); $x=$col*$cellWidth; $y=$row*$cellHeight $g.DrawRectangle($border,$x+8,$y+8,$cellWidth-16,$cellHeight-16) $maxW=330.0; $maxH=260.0 $scale=[Math]::Min($maxW/$component.Image.Width,$maxH/$component.Image.Height); $scale=[Math]::Min(1.0,$scale) $dw=[int][Math]::Round($component.Image.Width*$scale); $dh=[int][Math]::Round($component.Image.Height*$scale) $dx=$x+[int](($cellWidth-$dw)/2); $dy=$y+45+[int](($maxH-$dh)/2) $g.DrawImage($component.Image,$dx,$dy,$dw,$dh) $rx=$dx+[single]($record.rootPivot.outputPixels.x*$scale); $ry=$dy+[single]($record.rootPivot.outputPixels.y*$scale) $g.DrawLine($rootPen,$rx-7,$ry,$rx+7,$ry); $g.DrawLine($rootPen,$rx,$ry-7,$rx,$ry+7) if ($null -ne $record.nextJoint) { $nx=$dx+[single]($record.nextJoint.outputPixels.x*$scale); $ny=$dy+[single]($record.nextJoint.outputPixels.y*$scale) $g.DrawEllipse($nextPen,$nx-6,$ny-6,12,12) } $g.DrawString(('C{0:d2} {1}' -f $part.Id,$part.Name),$fontTitle,$titleBrush,$x+18,$y+14) $g.DrawString("$($part.Label) | red root / cyan next",$fontMeta,$metaBrush,$x+18,$y+326) } $sheet.Save($previewPath,[Drawing.Imaging.ImageFormat]::Png) $fontMeta.Dispose(); $fontTitle.Dispose(); $metaBrush.Dispose(); $titleBrush.Dispose() $nextPen.Dispose(); $rootPen.Dispose(); $border.Dispose(); $g.Dispose(); $sheet.Dispose() } finally { foreach ($component in $components) { if ($null -ne $component.Image) { $component.Image.Dispose() } } } Write-Output "Exported $($parts.Count) rig-correction parts." Write-Output $outputDirectory Write-Output $manifestPath Write-Output $previewPath