8
$\begingroup$

I am working with very old model files that have BMP textures. The designers did not use a transparency in the textures or a separate alpha b/w texture. They used a specific color value that was then removed by the engine to create a transparency, similar to a chroma key. In this case, all textures used a bright purple/pink color.

I cannot modifying the original texture images by converting them to PNG and add an alpha. I would like to figure out how to do it all in nodes.

The goal is to have this value keyed out and transparent. I have been messing around in the shader editor and cannot figure this out. I've also been looking around online and in this forum, but the only search terms I can think of are chroma-keying which only results in Compositing results, which I don't believe is what I'm looking for.

Here is an example of the texture (its very small) and it's context:

chain texture

chain in context

Any help or direction would be much appreciated!

$\endgroup$
7
  • $\begingroup$ This may not be possible in Blender. Use external dedicated software. $\endgroup$ Commented Apr 30, 2020 at 12:01
  • 1
    $\begingroup$ looks like its very much possible @Lukasz-40sth! phew! $\endgroup$ Commented Apr 30, 2020 at 20:11
  • 1
    $\begingroup$ @Lukasz-40sth Name calling and personal attacks will not be tolerated here. Your previous post had already been removed, mind your attitude $\endgroup$ Commented May 1, 2020 at 23:02
  • 2
    $\begingroup$ It's possible there's a language barrier happening. "phew" in the US is an expression of relief after being concerned. You said it may not be possible to do this. I received a bunch of good answers that all worked. I was expressing: "I'm so relieved I could do this inside Blender like I had hoped. What a relief! (phew)" Hopefully that's clear! Here's an example of "phew" in practice by an American president: gph.is/2pbOBPQ $\endgroup$ Commented May 2, 2020 at 19:49
  • 1
    $\begingroup$ I didn't know that. So we have another difference between UK and US. Quite significant one as most dictionaries say it means disgust, similar to Polish Fuj! pron. Fooy $\endgroup$ Commented May 3, 2020 at 11:59

6 Answers 6

15
$\begingroup$

You can use a "Mix RGB" node set to "Subtract" and invert the resulting mask to get your alpha value.

enter image description here

Note : "Compare" node is a "Math" node with the operation set to "Compare"

Note : "Length" node is a "Vector Math" node with the operation set to "Length". Why it is important.

Result :

enter image description here

You can tweak the third input of the compare node if your magenta isnt pure, to add a threshold. This will also progressively get rid of colors that are closer to the input color. Example :

enter image description here

Also real-life example (Green screen) :

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Oh man, this is amazing :). It works just like color range command in Photoshop. Already saved it as a node group. imgur.com/vkKX4Na $\endgroup$ Commented Apr 30, 2020 at 8:16
  • 1
    $\begingroup$ @JachymMichal Nice ! Do you plan on plugging something in your color ramp when using this ? $\endgroup$
    – Gorgious
    Commented Apr 30, 2020 at 11:27
  • 2
    $\begingroup$ Not really, I just used it as a slider. Anyways, nice Sméagol :). $\endgroup$ Commented Apr 30, 2020 at 12:03
  • 2
    $\begingroup$ love this answer. honestly, i love all these answers. seeing the same thing achieved different ways is such a terrific way to learn. I used your approach, but i also changed my texture's interpolation to CLOSEST instead of the default linear. it kept the hard edges on the key $\endgroup$ Commented Apr 30, 2020 at 20:40
6
$\begingroup$

You could simply push the contrast of your image with a ColorRamp and use this as a mask between the original and a Transparent node. As suggested by Jachym Michal, when using Principled BSDF, just plug the ColorRamp into the Alpha input.

enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ Great answer :). Also, when using Principled BSDF, just add a colorramp imgur.com/l4JhuKn $\endgroup$ Commented Apr 30, 2020 at 7:26
  • $\begingroup$ Use cycles, does not work in eevee. $\endgroup$
    – node
    Commented May 29, 2021 at 21:48
  • 1
    $\begingroup$ @node in Eevee you need to go in the Materials panel > Settings > Blend Mode > Alpha Blend instead of Opaque, is it your problem? $\endgroup$
    – moonboots
    Commented May 30, 2021 at 1:06
  • $\begingroup$ got it to work in eevee too,, blend mode / alpha clip . $\endgroup$
    – node
    Commented May 31, 2021 at 21:32
6
$\begingroup$

Based entirely on Gorgious' answer, I've put together an easy-to-use nodegroup.
It's similar to Color Range selection in Photoshop.

  1. Pick a color to select
  2. Adjust the color Range
  3. Invert the colors if needed

It outputs a black-and-white mask, useful as alpha or fac input.

enter image description here enter image description here enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ No problem, have a nice day :) $\endgroup$ Commented Jun 6, 2021 at 9:40
5
$\begingroup$

Pure purple should be RGB(1,0,1) so you can make a mask with Math node, Greater Than operation. Red value should be greater than some threshold value and blue value should be greater than some threshold as well so they should be multiplied:

enter image description here

In this particular case you probably do not want to use anti-aliasing since it will not work well with this technique, so maybe set interpolation of the texture to "Closest". The sample texture could work anyway, but if you have more colourful textures, keying the purple might become an issue with other types of interpolation.

$\endgroup$
5
$\begingroup$

@Gorgious 's answer, because of the implicit conversion of color > scalar, compares the luminance of the colors (using the higher tree below). It selects the colors in the middle, highlighted B/W band in the illustration, when compared to the mid-green:

enter image description here

Although, as he shows, this works perfectly adequately, for accuracy, you may want to compare the 3D distance between the colors in RGB space by introducing a vector math Length node, (as in the lower tree,) which selects the colors in the bottom B/W band.

$\endgroup$
3
  • $\begingroup$ This is awesome ! All the answers could benefit from it. The "correctness' of the spectrum mask brought by the length node is indeed invaluable when dealing with compressed images and random colors shifts start popping everywhere. Thanks for the tip ;) $\endgroup$
    – Gorgious
    Commented Sep 14, 2020 at 13:45
  • 2
    $\begingroup$ @Gorgious Phew! I thought I might be being undiplomatic .. To be fair to you, there are places where, despite having a wider/more scattered spectrum, Luminance might be a better discriminator. $\endgroup$
    – Robin Betts
    Commented Sep 14, 2020 at 14:01
  • 2
    $\begingroup$ Don't sweat it :) I think only dummies get angry when someone corrects them, especially on the context of this site... Have a nice day $\endgroup$
    – Gorgious
    Commented Sep 14, 2020 at 18:45
4
$\begingroup$

You can have anti-aliasing and transparency and use only nodes, but that won't be a small node tree.

test render

  1. Original image, linear interpolation
  2. closest interpolation, background removed similar way as other answers nodes
  3. linear interpolation, otherwise same node network as 2.
  4. monster node network emulating linear interpolation, screenshot would be unusable, check linked blend file

My recommendations, in order:

  • reevaluate if you really need to not convert source files,
  • use closest interpolation,
  • check if you maybe could somehow do the conversions in python in blender in a way that meets your constraints,
  • or emulate interpolation mode you want using monster node network

also, the advantage of emulation is you could use weird interpolation mode that blender does not have normal way

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .