1

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

gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
Innocent Baraka
  • 107
  • 1
  • 6

4 Answers4

5

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.

troy_s
  • 12,268
  • 2
  • 36
  • 75
2

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

Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218
  • 1
    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. – troy_s Sep 10 '16 at 20:15
  • 2
    @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. – Jaroslav Jerryno Novotny Sep 10 '16 at 20:27
  • 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. – Gez Sep 11 '16 at 22:40
  • @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. – Jaroslav Jerryno Novotny Sep 12 '16 at 10:04
  • @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. – Gez Sep 13 '16 at 17:11
  • @Gez "If there is any occlusion, you need an alpha channel" - and that's wrong. You don't need alpha, if the BG is black, you just need 3 occlusion masks from RGB channels to do the occlusion compositing! And you can do this compositing in any software you like. Try exporting TGA from Troy Sobotka's answer and import in inside Blender again and composite it over black with Blender's default settings. You will get this: http://i.stack.imgur.com/gDOAz.png (only with EXR you will get correct result out of the bag). – Jaroslav Jerryno Novotny Sep 13 '16 at 18:13
  • What are you talking about? "you don't need alpha"??? What do you think you "3 occlussion masks from RGB channels" are? Go check your example and see what you're doing. – Gez Sep 13 '16 at 18:46
  • @Gez It's per-channel masking, be amazed: http://i.stack.imgur.com/i6OTh.png. In my answer I work with transparency. But you don't need that to occlude some semi-transparent object on black over some background. – Jaroslav Jerryno Novotny Sep 13 '16 at 18:55
  • Dude! Mind blown! You're a wizard, I'm sorry I doubted your methods. And all this time I've been doing the same you just showed with a simple add. How blind I was. http://imgur.com/a/zEKIL – Gez Sep 13 '16 at 19:37
  • @Jerryno Note I said to use TIFF and you (wrongly) griped at TIFF. 99.95% of imagers that know what they are doing use EXR, DPX, and TIFF. Try Nuke. The simple answer is 1) Use real tools 2) Use real file formats and understand alpha. This is Brinkman or Okun level one sort of stuff, not complex. Not random. Add is not occlusion. Occlusion can only be achieved via a scaled value, which means multiply. Maybe go read Digital Compositing or the VES Handbook? Better, instead of barfing up a rubbish node chain, why not instead do a proper alpha over via nodes if your crap software doesn't? – troy_s Sep 13 '16 at 22:33
  • @Gez You are blind because Add does something different. Though the node network is wrong, I didn't account for the bg values when adjusting it to use no transparency. Here is corrected version: http://i.stack.imgur.com/blxuv.png. And I did both emission and occlusion so we are clear. – Jaroslav Jerryno Novotny Sep 14 '16 at 07:06
  • @troy_s Yeah, Nuke is fine. The point is not to point out where and with what formats does it work - the point is to show where and with what formats it doesn't work. The answer must work generally. Else you can write your "perfect" answer but add - Oh it will work only with EXR in Blender or get Nuke or if that doesn't work and it only raised more questions go elsewhere to learn because it is not covered here. It needs to work for a monkey. Which this answer will. – Jaroslav Jerryno Novotny Sep 14 '16 at 07:15
1

i usually use an easier method where i plug the texture to a shader(usually emission) and use shader to rgb node, then a color ramp and give it to the factor of a mix shader between transparent bsdf and the first shader we gave the texture to. I get good results usually just playing with the color ramp handles.

1

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

tschundler
  • 243
  • 1
  • 6