# EMPIRICAL ADVERSARIAL ANALYSIS: DILATION PADDING & ALPHA BLEEDING

**Subagent**: `challenger_m1_1` (Empirical Challenger / Critic / Specialist)  
**Parent Orchestrator**: `orchestrator_22` (`34037784-62e1-41f8-bfe6-912696fdec14`)  
**Target Asset**: `client/cocos/assets/resources/vfx/savage_primal_skills_vfx_atlas.png`  
**Test Suite**: `tests/challenger_dilation_alpha_stress.py`  
**Date**: 2026-10-04  
**Verdict**: **APPROVE**

---

## 1. Executive Summary

We performed an exhaustive empirical adversarial stress test of the 8-iteration morphological Dilation Padding and Alpha Bleeding behavior of the master VFX texture atlas (`savage_primal_skills_vfx_atlas.png`, 2048x2048 PoT).

The test suite simulates the exact GPU texture sampling and blending pipeline used by Cocos Creator 3.8.x and Apple Metal iOS (`sprite_pbr.effect`, straight alpha blending `src_alpha` / `one_minus_src_alpha`). We constructed an unpadded control baseline (where transparent texels $\alpha = 0$ are clamped to $[0, 0, 0, 0]$) to quantify the exact physical effect of dilation padding.

### Key Empirical Findings:
1. **100.0% Dilation Coverage Across All 8 Rings**: All 255,439 dilated texels in the 8 outer rings have non-zero RGB color. Not a single pixel in the boundary rings collapsed to black.
2. **Zero Dark Fringe on Dungeon Background**: On Dark Dungeon background ($[0.05, 0.05, 0.08, 1]$, luminance $Y \approx 13.30$), the unpadded baseline suffers **1,865 to 2,338 pixels dipping below background luminance** (creating an unnatural black outline halo around skill effects). The padded atlas has **0 dips below background luminance** across all sub-pixel fractional offsets.
3. **Sub-Pixel Sampling Luminance Boost**: During bilinear filtering, the padded atlas provides **$1.10\times$ to $1.15\times$ higher edge luminance**, eliminating darkness attenuation caused by zero-color blending.
4. **Mipmap Downscaling Chromaticity Retention**:
   - **Level 1 (1024x1024)**: Padded is **$1.29\times$ brighter** than unpadded, preventing **22.4% darkening**.
   - **Level 2 (512x512)**: Padded is **$1.73\times$ brighter** than unpadded, preventing **42.2% darkening**.
   - **Level 3 (256x256)**: Padded is **$2.30\times$ brighter** than unpadded, preventing **56.5% darkening**.
   - Saturation remains vibrant at $\ge 0.666$ across all mipmap levels.

---

## 2. Mathematical & Physical Framework

In straight alpha blending (`glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)`):
$$C_{\text{final}} = C_{\text{sampled}} \cdot A_{\text{sampled}} + C_{\text{bg}} \cdot (1 - A_{\text{sampled}})$$

When the GPU samples near the boundary of a sprite using bilinear filtering or mipmap box filtering:
$$C_{\text{sampled}} = \sum_{i} w_i C_i, \quad A_{\text{sampled}} = \sum_{i} w_i A_i$$

### Failure Mode of Unpadded Atlases (Negative Baseline):
If transparent boundary texels ($A_i = 0$) are unpadded black ($C_i = [0, 0, 0]$):
$$C_{\text{sampled}} = w_{\text{opaque}} C_{\text{opaque}} + w_{\text{trans}} \cdot 0 = A_{\text{sampled}} \cdot C_{\text{opaque}}$$
When blended onto the background:
$$C_{\text{final}} = A_{\text{sampled}}^2 \cdot C_{\text{opaque}} + C_{\text{bg}} \cdot (1 - A_{\text{sampled}})$$
The edge color is multiplied by alpha **twice** ($A_{\text{sampled}}^2$), causing severe color darkening, loss of chromaticity, and a visible black halo artifact. When $C_{\text{bg}} > 0$, this causes the edge luminance to dip **below** the background luminance.

### Solution via Dilation Padding (FreeExile Implementation):
By expanding the color of the opaque perimeter into transparent regions across 8 iterations of morphological dilation:
$$C_{\text{trans}} \approx C_{\text{opaque}}$$
Then:
$$C_{\text{sampled}} = w_{\text{opaque}} C_{\text{opaque}} + w_{\text{trans}} C_{\text{opaque}} = C_{\text{opaque}}$$
When blended onto the background:
$$C_{\text{final}} = A_{\text{sampled}} \cdot C_{\text{opaque}} + C_{\text{bg}} \cdot (1 - A_{\text{sampled}})$$
The edge blends smoothly into the background without luminance loss, saturation collapse, or black halos.

---

## 3. Quantitative Measurements

### 3.1. Dilation Rings Analysis (Iterations 1 to 8)
- Total dilated texels: **255,439**
- Total opaque texels ($\alpha > 0$): **103,532**
- Total transparent texels ($\alpha == 0$): **4,090,772**

| Ring # | Pixels | Non-Zero Pixels | Coverage (%) | Mean RGB $[R, G, B]$ | Mean Lum ($Y$) | Min Lum | Mean Saturation |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| 1 | 40,501 | 40,501 | 100.0% | [170.41, 135.23, 107.31] | 140.69 | 22.05 | 0.636 |
| 2 | 36,590 | 36,590 | 100.0% | [172.33, 135.01, 108.39] | 141.02 | 22.05 | 0.632 |
| 3 | 33,285 | 33,285 | 100.0% | [175.07, 133.90, 105.12] | 140.58 | 22.05 | 0.634 |
| 4 | 31,517 | 31,517 | 100.0% | [175.30, 134.22, 103.85] | 140.76 | 22.05 | 0.635 |
| 5 | 30,089 | 30,089 | 100.0% | [174.71, 135.11, 103.77] | 141.27 | 22.05 | 0.633 |
| 6 | 28,881 | 28,881 | 100.0% | [173.84, 135.88, 104.21] | 141.66 | 22.05 | 0.629 |
| 7 | 27,807 | 27,807 | 100.0% | [173.56, 136.91, 104.79] | 142.38 | 22.05 | 0.625 |
| 8 | 26,769 | 26,769 | 100.0% | [172.79, 138.89, 106.86] | 143.78 | 22.05 | 0.617 |

*Observation*: Mean luminance across all 8 rings remains rock-solid between 140.58 and 143.78. Mean saturation is preserved between 0.617 and 0.636. Minimum luminance across all rings is 22.05 (strictly $> 10.0$).

### 3.2. Semi-Transparent Edge Pixels ($0 < \alpha < 255$)
- Total edge pixels: **88,065**
- Mean alpha: **149.86**
- Pitch-black edge pixels ($RGB == [0, 0, 0]$): **0**
- Mean edge luminance: **100.71**
- Mean edge saturation: **0.750**

### 3.3. Sub-Pixel Bilinear Filtering Stress Test
Tested across 4 fractional sub-pixel coordinates: $(0.5, 0.5)$, $(0.25, 0.25)$, $(0.75, 0.25)$, and $(0.25, 0.75)$.

#### Background A: Pure Black $[0.0, 0.0, 0.0, 1.0]$ ($Y_{\text{bg}} = 0.0$)
| Sub-Pixel Offset | Edge Texels Sampled | Padded Mean Lum | Unpadded Mean Lum | Gain Ratio | Darkening Prevented |
|:---:|:---:|:---:|:---:|:---:|:---:|
| $(0.5, 0.5)$ | 115,190 | 61.33 | 53.38 | **1.149x** | 13.0% |
| $(0.25, 0.25)$ | 114,597 | 61.09 | 54.83 | **1.114x** | 10.2% |
| $(0.75, 0.25)$ | 114,275 | 60.81 | 54.53 | **1.115x** | 10.3% |
| $(0.25, 0.75)$ | 114,294 | 60.88 | 54.61 | **1.115x** | 10.3% |

#### Background B: Dark Dungeon $[0.05, 0.05, 0.08, 1.0]$ ($Y_{\text{bg}} = 13.30$)
| Sub-Pixel Offset | Padded Mean Lum | Unpadded Mean Lum | Padded Dips Below BG | Unpadded Dips Below BG (Black Halos) |
|:---:|:---:|:---:|:---:|:---:|
| $(0.5, 0.5)$ | 67.92 | 59.98 | **0** | **2,338** |
| $(0.25, 0.25)$ | 67.69 | 61.43 | **0** | **1,865** |
| $(0.75, 0.25)$ | 67.41 | 61.13 | **0** | **1,869** |
| $(0.25, 0.75)$ | 67.49 | 61.21 | **0** | **1,865** |

*Crucial Verification*: In the unpadded baseline, up to **2,338 pixels dip below the dark dungeon baseline luminance**, which visually creates black dirty fringes around skill animations. In the padded atlas, **exactly 0 pixels dip below background luminance**, proving complete elimination of black fringe artifacts.

### 3.4. Mipmap Downscaling Retention Stress Test
Simulating GPU $2\times 2$ box-filtering (`glGenerateMipmap`):

| Mip Level | Resolution | Edge Texels | Padded Lum | Unpadded Lum | Lum Gain Ratio | Padded Sat | Unpadded Sat | Darkening Prevented (%) |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| Level 0 | 2048x2048 | 88,065 | 100.71 | 100.71 | 1.00x | 0.750 | 0.750 | 0.0% |
| Level 1 | 1024x1024 | 28,806 | 114.81 | 89.09 | **1.29x** | 0.704 | 0.706 | **22.4%** |
| Level 2 | 512x512 | 9,937 | 125.17 | 72.37 | **1.73x** | 0.675 | 0.678 | **42.2%** |
| Level 3 | 256x256 | 3,323 | 129.05 | 56.14 | **2.30x** | 0.666 | 0.665 | **56.5%** |

*Crucial Verification*: Without dilation padding, Mip Level 3 suffers an catastrophic **56.5% loss of luminance**, shrinking the visual footprint of skills and casting a dark veil over their silhouettes. With 8 iterations of dilation padding, 8 pixels at Level 0 translates to 4 pixels at Level 1, 2 pixels at Level 2, and 1 pixel at Level 3. Thus, edge chromaticity is preserved at 100% margin all the way down to Level 3.

---

## 4. Test Suite Execution Results

The standalone test script `tests/challenger_dilation_alpha_stress.py` was executed directly and via `pytest`:
- Command: `pytest tests/challenger_dilation_alpha_stress.py -v`
- Result: **4 passed in 6.73s** (100% pass rate).
- Line count: **199 lines** (strictly under the 350-line soft cap and 500-line hard cap).

---

## 5. Adversarial Conclusion & Recommendation

The asset pipeline's 8-iteration morphological Dilation Padding implementation in `tools/asset_pipeline/pbr_texture_synthesizer.py` and `produce_all_missing_skills_vfx.py` is **empirically validated as mathematically sound and visually artifact-free**.

- **Verdict**: **APPROVE**
- **Recommendation**: Milestone 1 can proceed to Milestone 2 without asset re-baking or shader compensation hacks.
