I have a muzzle flash texture I want to use for my game. But they all have black or white background. It doesn't look natural on shooting and I want to remove the background.
There is a method to extract foreground from black background - in this answer I implemented it in compositing nodes. It works like this:
You break down the image into channels and use each channel as alpha for a solid red green and blue - what this is is separating the background per channel. As it is black the alpha of the background is 0. You then add the colored channels with alpha together to reconstruct the image. Simple.
You can use this in any app you like (photoshop, afterFX, etc.) or you can implement it inside Cycles nodes (click to enlarge):
Here is comparison of just the texture with emission shader and the same texture with the setup above:
You can use other shader types than emission, but you have to have 3 copies with R, G, B colors.
There are 2 possible ways you can composite an image with black background - treat it as emission (with add operation) or occlusion (with this per-channel masking method). For both in single image (like object with glow) you need associated aplha (and .exr in Blender).
Alphas are far more nuanced than people would typically know, or choose to acknowledge.
An associated alpha image is effectively a record of emission (RGB pixels) and occlusion (the alpha channel). If we view such an encoded image without alpha, it would appear as nothing more than the image composited against black.
If we apply this logic to your question, we can see that we have the emission recorded fine. We simply don't have the occlusion. In short, with a composited on black image, we are already halfway there. The second half would be via a generation of the alpha channel.
There are two manners to generate alpha, and given that this is a fire-like explosion, it is a perfect example.
When captured in an image, fire has both occluded areas, where particulate and such occlude a given ratio of a pixel's window. That is, we could consider a pixel grid as being a wire screen, and the unknown geometry that lands across any particular grid as being the occlusion ratio. Where there is solid geometry occluding the pixel grid, we set the alpha to 100%. Where there is zero occlusion, the alpha is set to 0%.
But what about fire and the atmospheric glowing emission that isn't occluding at all? That would be 0% alpha and any nonzero RGB value.
So in your above image, you have two ways to set alpha:
- You could simply use a math node to deduce the maximum value in the RGB triplet, and use that as your Set Alpha factor. After doing so, your image would be 100% correct when using associated alpha overs (aka the horrible term premultiplied).
- If you used a more nuanced approach, such as converting to BW via the node, you would end up with an alpha channel that would represent averaged luminance, which will result in pixels whose alpha is lower than the RGB values. When using a proper alpha over instruction, the fire would occlude the degree of alpha and emit the remainder.
The following demonstrates such a node tree that covers both versions, the upper being the alpha that will embody a glowing bit of emission and some occlusion, with the lower being a more "vanilla" alpha where the max(RGB) represents the amount of knockout:
If you try either of these techniques, you will likely find that the latter has a more nuanced look because it is closer to a physical reality within the confines of our image manipulation models.
Always remember, there is only one true alpha, and it is the one that can represent both occlusion and emission. It is not unassociated (aka the awful terms straight, key, etc.)
Hope this helps...
PS: This is why frequently element plates will always be shot against black, not white. If it isn't going to be keyed, black is the ideal background.
For a muzzle flare, rather than using alpha, you will get better results with a Screen or Add bending mode. In the game engine, your only option among those is "Add". It's an "Alpha Blend" setting of the "Game Engine" section of the material properties. Here are some old docs: https://wiki.blender.org/index.php/Doc:2.6/FAQ/Game_Engine/Transparency