sprite-segment-3x3grid
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSprite Color Grid Analysis
Sprite 颜色网格分析
Purpose
用途
This skill analyzes Sprite textures by segmenting them into a 3x3 grid and outputting a text representation based on color matching with the center cell.
本功能通过将 Sprite 纹理分割为 3x3 网格,并基于各单元格与中心单元格的颜色匹配情况输出文本表示,以此分析 Sprite 纹理。
Parameters
参数
Match Threshold
匹配阈值
The match threshold determines what percentage of pixels in a non-center cell must match the center's majority color for that cell to be considered a "match" ().
.| Threshold | Description | Use Case |
|---|---|---|
| 50% | At least half the cell's pixels match center color | Lenient matching for sprites with mixed regions |
| 75% | At least three-quarters match center color | Moderate matching for mostly solid regions |
| 90% | Nearly all pixels must match center color | Strict matching for very uniform sprites |
Default: 75%
匹配阈值决定了非中心单元格中需要有多大比例的像素与中心单元格的主色匹配,该单元格才会被视为“匹配”()。
.| 阈值 | 描述 | 适用场景 |
|---|---|---|
| 50% | 至少一半的单元格像素与中心颜色匹配 | 适用于混合区域较多的 Sprite 的宽松匹配 |
| 75% | 至少四分之三的像素与中心颜色匹配 | 适用于大部分为实心区域的 Sprite 的中等匹配 |
| 90% | 几乎所有像素都必须与中心颜色匹配 | 适用于非常均匀的 Sprite 的严格匹配 |
默认值:75%
Algorithm
算法
- Segment the Sprite: Divide the Sprite's texture region into a 3x3 grid (9 cells total)
- Identify Center Color: Calculate the majority (most frequent) color in the center cell (position [1,1])
- Compare Each Cell: For each of the 8 surrounding cells, calculate what percentage of pixels match the center's majority color. If the percentage meets or exceeds the match threshold, the cell is a match (); otherwise it is not (
.)X - Generate Output: Create a text representation using the legend below
- 分割 Sprite:将 Sprite 的纹理区域划分为 3x3 网格(共 9 个单元格)
- 识别中心颜色:计算中心单元格(位置 [1,1])的主色(出现频率最高的颜色)
- 比较每个单元格:对周围的 8 个单元格,分别计算其与中心主色匹配的像素占比。如果占比达到或超过匹配阈值,则该单元格为匹配状态(),否则为不匹配(
.)X - 生成输出:使用下方图例创建文本表示
Output Legend
输出图例
| Symbol | Meaning |
|---|---|
| Cell does NOT meet the match threshold (insufficient pixels match center color) |
| Cell MEETS the match threshold (enough pixels match center color) |
| The center cell itself (always position [1,1] in the grid) |
| 符号 | 含义 |
|---|---|
| 单元格未达到匹配阈值(与中心颜色匹配的像素不足) |
| 单元格达到匹配阈值(有足够多的像素与中心颜色匹配) |
| 中心单元格本身(始终位于网格的 [1,1] 位置) |
Output Format
输出格式
The output is a single line with three groups of three characters, separated by . Each group represents one row of the grid.
/Order: Top to Bottom rows, Left to Right within each row.
Format:
[TopRow] / [MiddleRow] / [BottomRow]Each row contains 3 characters separated by spaces:
[Left] [Center] [Right]输出为单行内容,包含三组三个字符,组与组之间用 分隔。每组代表网格的一行。
/顺序:行从上到下,每行内从左到右。
格式:
[TopRow] / [MiddleRow] / [BottomRow]每行包含 3 个用空格分隔的字符:
[Left] [Center] [Right]Grid Position Mapping
网格位置映射
Grid Layout: Output Order:
[0,0] [1,0] [2,0] 1 2 3 -> First group
[0,1] [1,1] [2,1] 4 5 6 -> Second group (5 is always *)
[0,2] [1,2] [2,2] 7 8 9 -> Third groupGrid Layout: Output Order:
[0,0] [1,0] [2,0] 1 2 3 -> First group
[0,1] [1,1] [2,1] 4 5 6 -> Second group (5 is always *)
[0,2] [1,2] [2,2] 7 8 9 -> Third groupExamples
示例
Example 1: Center matches top-left and bottom-right only
. X X / X * X / X X .Grid interpretation:
. X X
X * X
X X .Example 2: Center matches all surrounding cells
. . . / . * . / . . .Example 3: Center matches none of the surrounding cells
X X X / X * X / X X XExample 4: Center matches bottom row only
X X X / X * X / . . .示例 1:中心仅与左上角和右下角匹配
. X X / X * X / X X .网格解读:
. X X
X * X
X X .示例 2:中心与所有周围单元格都匹配
. . . / . * . / . . .示例 3:中心与周围所有单元格都不匹配
X X X / X * X / X X X示例 4:中心仅与底部行匹配
X X X / X * X / . . .Implementation Guidance
实现指南
Calculating Center's Majority Color
计算中心单元格的主色
For the center cell:
- Sample all pixels within the cell's bounds
- Group pixels by color (consider using a color tolerance threshold for similar colors)
- The majority color is the color with the highest pixel count
- For tie-breaking, use the first color encountered
对于中心单元格:
- 采样单元格边界内的所有像素
- 按颜色对像素分组(对于相似颜色,可考虑使用颜色容差阈值)
- 主色即像素数量最多的颜色
- 若出现数量相同的情况,以最先遇到的颜色为准
Matching Non-Center Cells
匹配非中心单元格
For each of the 8 surrounding cells:
- Count the total number of pixels in the cell
- Count how many pixels match the center's majority color (using color tolerance)
- Calculate the match percentage:
matchingPixels / totalPixels - If match percentage >= threshold, cell is a match (); otherwise (
.)X
对于周围的 8 个单元格:
- 统计单元格内的总像素数
- 统计与中心主色匹配的像素数量(使用颜色容差)
- 计算匹配百分比:
matchingPixels / totalPixels - 若匹配百分比 >= 阈值,则单元格为匹配状态(),否则为(
.)X
Color Tolerance
颜色容差
When comparing pixel colors:
- Consider using a tolerance threshold (e.g., RGB distance < 10) for "matching"
- Alternatively, for sprites with limited palettes, exact matching may be appropriate
- Account for alpha channel if relevant to the use case
比较像素颜色时:
- 可考虑使用容差阈值(例如 RGB 距离 < 10)来判定“匹配”
- 另外,对于调色板有限的 Sprite,采用精确匹配可能更合适
- 如果使用场景相关,需考虑 alpha 通道
Sprite Bounds
Sprite 边界
Use the Sprite's property to determine the pixel region to analyze:
rect- ,
sprite.rect.xfor the originsprite.rect.y - ,
sprite.rect.widthfor dimensionssprite.rect.height - Divide width and height by 3 to get cell dimensions
使用 Sprite 的 属性确定要分析的像素区域:
rect- 原点为 、
sprite.rect.xsprite.rect.y - 尺寸为 、
sprite.rect.widthsprite.rect.height - 将宽度和高度除以 3,得到每个单元格的尺寸
References
参考资料
Code Template: "scripts/SpriteGridAnalysis.cs"
代码模板:"scripts/SpriteGridAnalysis.cs"
Use Cases
适用场景
- Documentation: Describe sprite patterns in a compact text format
- Testing: Verify expected sprite structure in automated tests
- Analysis: Quickly identify sprites with similar color distributions
- Debugging: Understand why sprites look different than expected
- Asset Cataloging: Generate searchable metadata for sprite assets
- 文档编写:以紧凑的文本格式描述 Sprite 图案
- 测试:在自动化测试中验证预期的 Sprite 结构
- 分析:快速识别颜色分布相似的 Sprite
- 调试:了解 Sprite 外观与预期不符的原因
- 资产编目:为 Sprite 资产生成可搜索的元数据
Notes
注意事项
- This analysis works best with sprites that have distinct color regions
- For sprites with gradients or many colors, consider increasing color tolerance
- The Y-axis is flipped from Unity's texture coordinates to produce top-to-bottom output
- Transparent pixels can be treated as a distinct "color" or ignored based on use case
- 本分析最适用于颜色区域分明的 Sprite
- 对于带有渐变或颜色较多的 Sprite,可考虑增大颜色容差
- Y 轴与 Unity 的纹理坐标相反,以实现从上到下的输出顺序
- 透明像素可视为一种独立的“颜色”,也可根据使用场景忽略