Answer the question
In order to leave comments, you need to log in
How to define transparent areas of a sprite using a mask?
We have two images: the
original one, with a black background:
and its mask, with a white background:
I need to:
Get transparency instead of a black background so that if the sprite has black outlines, they are displayed. Because if you just perform a chroma key on a black background, then the contours will also be transparent. It is known that it is possible to get around the situation by painting the outlines SLIGHTLY lighter than black (for example, the background is #000000 and the outlines are #000001), but this is not suitable, because the project is planned for custom sprites. Transparency should appear exactly where the white background on the mask matches the black background on the original.
+ =
No need for an alpha channel.
I use the SDL library in C ++ for work.
________________________________________________________________________
Although there is an assumption:
Color on the mask: should subtract the color from the alpha channel of the corresponding pixel in each image, but on the test software I saw that if a non-black color fell under the mask, it starts to mix with the background and dirt is obtained:
Experiment in the test software with the wrong mask:
Source and mask:
Result:
UPD: After a while, I found out what algorithm for drawing such images is actually used:
For this, the bit mask algorithm was used .
those.:
Answer the question
In order to leave comments, you need to log in
A shader in openGL solves this elementarily.
Pass both textures to the shader. From the first texture we take r, g, b. Then we calculate the brightness of the second texture at the point and put one minus this value in the transparency.
vec4 tex1 = texture2D(texture1, position);
vec4 tex2 = texture2D(texture2, position);
float luma2 = (tex2.r + tex2.g + tex2.b)/3.0;
gl_FragColor = vec4(tex1.r, tex1.g, tex1.b, 1.0 - luma2);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question