1
$\begingroup$

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.

the muzzle flash texture
This is the texture I had in mind

$\endgroup$
  • 1
    $\begingroup$ Can you include said texture? $\endgroup$ – Shady Puck Sep 5 '16 at 12:23
  • $\begingroup$ Try using a screen or add Blend Mode (this won't work for a white background) $\endgroup$ – TARDIS Maker Sep 5 '16 at 13:19
2
$\begingroup$

There is a method to extract foreground from black background - in this answer I implemented it in compositing nodes. It works like this:

enter image description here

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):

enter image description here

Here is comparison of just the texture with emission shader and the same texture with the setup above:

enter image description here

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).

enter image description here

$\endgroup$
  • 1
    $\begingroup$ There is no "extraction" required when an object is against black. It is fundamental alpha theory, as per Porter Duff and Ray Smith's earliest work. $\endgroup$ – troy_s Sep 10 '16 at 20:15
  • 2
    $\begingroup$ @troy_s let's not jump into word nit-picking. I just called the process "extraction" and it only does the correct math to set black into transparent - it is no extraction you are right.. If you have a node setup that does the math faster and renders faster please post it. $\endgroup$ – Jaroslav Jerryno Novotny Sep 10 '16 at 20:27
  • $\begingroup$ When you use a software like Photoshop or GIMP (both use unassociated alpha internally) you just can't composite these kind of effects properly only with alpha over. It's just impossible. With those programs you have to use some specific blending modes or even more than one layer with different compositing operations (one for the occlusion with alpha over, one for the luminous pixels with add). Producing an unassociated RGBA image for alpha compositing from a plate like this one won't work. ever. $\endgroup$ – Gez Sep 11 '16 at 22:40
  • $\begingroup$ @Gez the goal of this is not to export an image with associated alpha to use somewhere but to use an image without any alpha directly in target app with correct transparency results (also in pipelines using unassociated way). This works only because the BG is black. If the BG would be any color, you would have to have that file with associated alpha. $\endgroup$ – Jaroslav Jerryno Novotny Sep 12 '16 at 10:04
  • $\begingroup$ @Jerryno: That's just nonsense. If the goal is using an image without any alpha, then just add it to the background, as the answer below states. If it's only adding light and not ocluding anything from the background, just add. If there is any oclussion, you need an alpha channel. If you need both, the ONLY possible way to achieve a correct result with a single operation is via associated alpha. Full stop. $\endgroup$ – Gez Sep 13 '16 at 17:11
5
$\begingroup$

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: Node Demonstration

And the result, in animated form to demonstrate differences: Animated Alpha Demonstration

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.

$\endgroup$
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – David Sep 14 '16 at 20:07
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ They all work. Im now using it in my game.Very helpful indeed. Thank you all. $\endgroup$ – Innocent Baraka Sep 15 '16 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.