CHROMA KEY NOTES / 006
Using a Transparent PNG as a Sprite in Unity, Godot and Unreal
A keyed PNG imports cleanly as a sprite when you set the texture type to 2D or Sprite, keep the alpha-border dilation switch on, and use transparent blending instead of alpha testing. The dark or green outline you see is not a bad key: it is the RGB value still stored under fully transparent pixels being averaged back into the visible edge when the engine filters the texture or builds mipmaps. On the two cutouts shipped with this site we measured 489 and 473 opaque-to-transparent edge pairs, and every single one produced a halo. Fix those import settings before you touch the cutout again.
Open the green screen remover ↗
What a keyed PNG actually contains
Before blaming the importer, it helps to know what the file says. We decoded the two cutouts bundled with this site and one RGBA frame written by our own keying pipeline, using a pure-Python PNG reader that handles all five filter types. No image library, no engine, no guesswork. All three files are color type 6, which means 8-bit RGBA: four values per pixel, one of them alpha. None of the three is a power-of-two size. The bundled sample is 640 by 420, the bundled tiger is 610 by 345, and our own frame is 640 by 420.
The alpha histograms matter more than the file sizes. In the 640 by 420 sample, 208,648 of 268,800 pixels (77.62 percent) are fully transparent, 59,590 are fully opaque, and 562 sit in between across 94 distinct alpha levels from 3 to 254. The tiger cutout has 167,035 fully transparent pixels (79.37 percent) and 1,292 partial pixels spread over 249 distinct levels. Our own pipeline output has 209,122 fully transparent pixels (77.8 percent) and 1,630 partial pixels over 84 levels. That thin band of partial alpha is the anti-aliased silhouette the keyer worked to produce, and it is exactly what several import defaults throw away.
Here is the detail that causes the most confusion. A PNG stores straight, or unassociated, alpha: the RGB channels are not multiplied by alpha. A fully transparent pixel can therefore hold any RGB value at all, and the viewer will never show it. In the two shipped cutouts, the exporter zeroed those hidden values, so all 208,648 and 167,035 clear pixels carry (0,0,0). In our own RGBA writer, they carry the original backdrop color: all 209,122 clear pixels hold a mean of (0.1, 210.0, 106.0), which is green. One developer hit the first behavior and asked about it directly: "Whenever I set a pixel value to have an alpha of 0, the corresponding RGB channels are also zeroed upon conversion to a PNG" (Stack Overflow 11601302). Both files are perfectly valid PNGs. They just fail differently downstream.
Why a correct file draws a dark or green fringe
Neither bilinear filtering nor mipmap generation knows that a transparent pixel's RGB is supposed to be invisible. Both are averaging operations over neighboring texels. Take an opaque edge texel and a clear texel that hides black RGB, and average them at t = 0.5: the result is a half-brightness version of the edge color, which reads as a dark outline. Average the same pair with a clear texel hiding green RGB, and you get a green outline. We counted every horizontal adjacent pair where an opaque texel meets a fully clear one in both shipped cutouts: 489 pairs in the sample and 473 in the tiger. All 489 and all 473 produced a color error, with RMS RGB distances of 97.6 and 176.1 respectively.
The problem is documented from the engine side too. One DirectX developer wrote: "when this method is used with transparent textures that do not have premultiplied alpha, the resulting mip levels tend to have black outlines at the transparency edges" (Game Development 92424). Another, importing a transparent pupil texture, reported "The pupil png has a Transparent background but that background shows up as Black in Unity" (Game Development 183233). A third described the same symptom from the blend side: "The transparent parts are black (the clear colour) and opaque instead though and the topmost sprite blocks the bottom sprite" (Game Development 32027). And a Gaussian-blur case notes that "when i have alpha channel with png, the result will become too dark" (Stack Overflow 35476142; the lowercase is in the original). Four different pipelines, one cause: hidden RGB being resampled.
We simulated one 2 by 2 box-downsample mip level on each file, comparing a naive straight-alpha average against an alpha-weighted average, and counted only texels that remain partly visible. On the sample, 560 of 15,266 visible texels changed color, with an RMS error of 17.7. On the tiger, 1,199 of 11,175 changed, RMS 51.4. On our own green-hiding frame, 514 of 15,138 changed, and 301 of those were pushed into a false green tint that the correct alpha-weighted version does not produce. This is why the fix belongs at import time rather than in the keyer: the exporter cannot know which engine will sample the file.
The three switches that matter, engine by engine
The names differ, but all three engines expose the same three decisions.
Unity
Set Texture Type to Sprite (2D and UI) rather than leaving it on Default, otherwise the file is treated as a 3D texture with different compression and mip behavior. Enable Alpha Is Transparency. Unity's own scripting reference states that enabling it will "dilate the color channels of visible texels into fully transparent areas" and that this "effectively adds padding around transparent areas that prevents filtering artifacts from forming on their edges." It also warns that "This property makes the color data of invisible texels undefined" - which is precisely why you should not rely on hidden RGB for anything else. Choose Filter Mode: Bilinear for photographic cutouts, Point for pixel art. Watch Max Size and the Non-Power of Two setting, because Unity will rescale an NPOT texture that exceeds the limit.
Godot
In the Import dock, process/fix_alpha_border defaults to true. Godot documents it as putting "pixels of the same surrounding color in transition from transparent to opaque areas," which "helps to reduce the outline effect" under bilinear filtering. Leave it on. process/premult_alpha defaults to false, and Godot describes it as "An alternative to fixing darkened borders" that "requires specific materials to be displayed correctly" - so switching it on without also switching the material's blend mode makes things worse. mipmaps/generate is off by default for 2D and adds roughly a third more texture memory; compress/mode set to VRAM Compressed is documented as 3D-only and should not be used for 2D elements. process/size_limit can downscale an oversized cutout at import without touching the source file.
Unreal
For a Paper 2D sprite, check the texture's Compression Settings: a UI or 2D-oriented setting preserves alpha, while a normal-map or no-alpha compression path discards it, which is the usual cause of an opaque box. Keep sRGB enabled for a color sprite, and review Mip Gen Settings if the sprite is scaled down, since that is where the fringe reappears. If the sprite is used on a masked material, the alpha threshold behaves like a cutout and will clip the feathered edge. The general rule across all three is the same: dilation for straight alpha, matching blend mode for premultiplied alpha, and no alpha test on a soft edge.
| Symptom after import | Measured cause | Setting that fixes it |
|---|---|---|
| Dark outline on edges | Clear pixels hide (0,0,0); 489/489 and 473/473 edge pairs halo | Alpha Is Transparency / Fix Alpha Border |
| Green outline on edges | Clear pixels hide (0,210,106); 301 mip texels tinted green | Dilate the border or premultiply with a matching blend |
| Solid black or white box | Blending disabled or compression dropped alpha | Transparent material, alpha-capable compression |
| Jagged, stair-stepped silhouette | Alpha test at 128 discards 382 / 546 / 204 partial pixels | Transparent blending, depth write off |
| Color bleeds between atlas frames | Tight packing with no gutter; tiger cutout has 0 gutter pixels | Extrude edge colors, add padding between frames |
| Sprite looks fine but uses 2 MiB | NPOT padded to 1024 x 512, 95 to 149 percent waste | Keep NPOT with mips off, or pack a POT atlas |
Non-power-of-two sizes and what they cost
All three of our files are NPOT. The next power of two in each direction is 1024 by 512, which is 524,288 texels. For the 640 by 420 sample that means 255,488 padded texels, or 95.05 percent waste; for the 610 by 345 tiger it is 313,838 padded texels, or 149.13 percent waste. Note that the PNG file on disk tells you almost nothing here: the sample is 18,165 bytes, the tiger is 134,084 bytes, and our own frame is 10,544 bytes, but as raw RGBA uploads they are 1.025, 0.803 and 1.025 MiB. Padded to 1024 by 512, each becomes a flat 2.0 MiB. Generating a full mip chain on top of the unpadded size adds about a third, taking the 640 by 420 files to roughly 1.367 MiB each.
This is not a hypothetical concern. One iOS-focused question argues: "Try to avoid unnecessary transparent pixels whilst ensuring a Power of 2 texture size. Since the texture will occupy the same amount of RAM whether it has its content spread across its entire surface or not, try to use as much of the available surface for the content as possible. This will improve visual quality when downsizing the texture (e.g. to generate Mipmaps)" (Game Development 138693). Our measurements put a number on that advice: the sample cutout's visible content fits inside a tight box of 322 by 342 starting at (159, 55), so 59.03 percent of the canvas is empty gutter. That empty area costs the same VRAM as the subject.
The same geometry drives atlas packing. Tight packing trims the transparent gutter to save space, which is good for memory and bad for filtering: adjacent frames then sample each other at fractional scaling. One tilemap developer found that "as soon as you introduce fractional scaling, seams appear" and that it "still happens with point filtering" (Game Development 74420). The tiger cutout is the extreme case: its subject already touches all four edges, so the tight bounding box is the entire 610 by 345 canvas and the transparent gutter is exactly zero. Pack that into an atlas without extruding edge colors first and the neighboring frame will bleed straight into it. Add a padding gutter of a few pixels and extrude the border color into it; that is the same dilation operation Unity and Godot perform automatically on a single texture.
Alpha testing, premultiplied alpha, and the soft edge
Two more import choices destroy the feathered edge. The first is alpha testing, also called a cutout or masked material. It turns alpha into a pass/fail test. At a threshold of 128, our sample loses 382 of its 562 partial pixels, the tiger loses 546 of 1,292, and our own frame loses 204 of 1,630. Those 94, 249 and 84 distinct coverage levels collapse to two states, and the silhouette becomes jagged. A shader question states the mismatch plainly: "I have a shader that is supposed to work with either alpha blending or alpha testing, but the color values being passed in are premultiplied alpha values" (Game Development 50479); another notes "I've got on or off transparency so when the mipmapping level is different the transparency can take up too much of the texture and make it invisible" (Game Development 28401). Use transparent blending with depth write disabled and sort your sprites.
The second is premultiplied alpha without a matching blend mode. Premultiplication scales RGB by alpha, which is exactly what makes filtering correct, but it changes the file: on our own 640 by 420 frame, 210,752 of 268,800 pixels would change RGB, with a total channel delta of 66,163,244, and 209,518 pixels violate the premultiplied rule (RGB must not exceed alpha) in the original straight-alpha file. If the material still uses straight-alpha blending, the result darkens. "I'm fading an object from fully visible to transparent, but the fade darkens the object" (Game Development 17732) is that mismatch in one sentence, and "most tools simply do not support it correctly" (Stack Overflow 7189417) is why we ship straight alpha. Keep the file straight, let the engine dilate the border, and only enter a premultiplied pipeline deliberately.
None of this replaces good capture. If the subject was lit green by the backdrop, no import setting will restore its color - see green spill after keying. If the backdrop was unevenly lit, the edge band will be irregular before it ever reaches the engine; the four-corner lighting diagnosis applies at capture time. And if the cutout itself has holes, why a green screen remover fails covers the symptom-to-cause chain. The broader list of jobs that belong to a different method is in when a green screen remover is the wrong tool, and why transparent video export is not available explains why an animated sprite sequence cannot be delivered as a single transparent video. The importer can only work with the alpha it was given.
One more destination breaks for the opposite reason. A game engine keeps the alpha channel and then misinterprets it; a print shop removes the alpha channel entirely, because paper and CMYK have no fourth value. Why your transparent PNG prints with a white box measures what happens when the clear pixels are flattened onto white, when the alpha is dropped and the hidden RGB shows through, and when the cutout is composited onto a real substrate. The diagram below is the short version.
A short decision sequence
Start with the file. Confirm it is RGBA with a real alpha channel and a partial-alpha edge band, not a flattened white or black box. Import it as a 2D sprite with alpha-border dilation on, transparent blending, and no alpha test. If you see a dark fringe, hidden RGB is black and dilation will fix it. If you see a green fringe, hidden RGB is green; dilation fixes it too, and re-exporting with dilated edge colors fixes it everywhere. If the whole background is opaque, blending or compression dropped the alpha. If edges are jagged, an alpha test cut the feather. If frames bleed into each other, add a gutter and extrude the border before packing. Turn mipmaps on only when the sprite is actually displayed smaller, and remember they cost about a third more memory and reintroduce the fringe if the border was not dilated. For pixel art, use Point filtering and keep mipmaps off.
Finally, keep the source cutout. This tool runs entirely on your device with no account and no upload, and it writes a straight-alpha RGBA PNG ready for any of the three engines once the import settings above are set. Details on the parameters are in about this tool. Re-keying the image will not change a single one of the import-side failures measured here.
Questions people actually ask
- Why does my transparent PNG show a black or green outline in Unity, Godot or Unreal?
- Because straight alpha keeps RGB under fully transparent pixels, and filtering averages that hidden color into the edge. Enable Alpha Is Transparency in Unity or Fix Alpha Border in Godot, and use transparent blending. We measured 489/489 and 473/473 edge pairs haloing on our two shipped cutouts, at RMS color errors of 97.6 and 176.1.
- Do non-power-of-two sprite textures cause problems?
- They can, because POT padding wastes memory and some compression paths require POT. Our 640 by 420 sample pads to 1024 by 512, adding 255,488 empty pixels and moving the upload from 1.025 MiB to 2.0 MiB. Keep NPOT with mipmaps off, or pack several sprites into one POT atlas.
- Should I enable premultiplied alpha for a keyed sprite?
- Only when the material uses a matching premultiplied blend mode. Premultiplying our own 640 by 420 frame changes 210,752 of 268,800 pixels, and Godot documents that premultiplied alpha requires specific materials to display correctly. Otherwise keep straight alpha and dilate the border instead.
- Why did my smooth cutout turn jagged after import?
- An alpha-test or cutout material reduced the feathered edge to a binary mask. Our three files carry 94, 249 and 84 distinct partial alpha levels; a threshold of 128 discards 382 of 562, 546 of 1,292 and 204 of 1,630 of those pixels. Use transparent blending with depth write disabled.
- Why do colors bleed between frames in my sprite atlas?
- Tight packing leaves no transparent gutter, so bilinear filtering samples the neighboring frame. Our tiger cutout has exactly zero gutter pixels because its subject touches all four edges. Extrude the border color into a few pixels of padding before packing, and avoid fractional scaling where possible.