Stencil Alpha Problem [solved .. sort of]

English isn’t my mother tongue so i hope i can explain my problem:

I’ve read this article about stencil:
http://scott-franks.com/xna-masking-sprites-without-shaders/

And I copied that into a new project to test it.
It doesn’t work like i wanted and I dont know why.

Those are the two pictures I used:

I want to mask the “B” with the lines in so that I have a “B” with lines ^^ …

But i get this (second with reversed stencil):

I want this:

Can anybody help me?

Does it work with the original pictures?

Do you scale the images?

Can you confirm that the mask has transparent pixels?
Also check the importer, I think it should be set to premultiply-alpha.

there are no original pictures to download there
i made my own

no scale. pictures are 200200 px and rendered 200200 px

yes I can confirm it. If i draw the pictures with monogame without stencil functions, every alpha pixels are shown as intended. (used photoshop to make the pictures without background)

I found an other example at github


This one works with my pictures:

I have to compare what this one does right and the other wrong.

Oh, i had to tweak the code a little bit (mainly deleted the halfoffset pixels for the alphatest) and deleted the stuff about the key input

Edit: Got another problem:
Is it normal that this stencil distinguish only between alpha 0 (no pixel) and 1 (draw pixel) and nothing in between like 50% alpha (0.5)?

For understanding
I want something like this:

what I now get is something like that:


Can anybody help me with a solution with the stencil option?

Stencil on that matter has binary behavior, either it will display given pixel or cut it off completely. As you can seen in your case - every pixel on map with value under exact 1.0 is being displayed.

Why don´t you use shader and alpha blend?

Ok thanks for that response
So it isnt possible with stencil =)
I will try a pixel shader

maybe this is what i want … I will try during the weekend
http://www.syntaxwarriors.com/2012/xna-alpha-mapping-with-pixel-shaders/

just need to write a shader with coordinate input because the mask has to move in runtime ^^
maybe this can help too

do you know some good tutorial or example I can read?

The build-in DualTextureEffect will be a good starting point for something like that.
It depends on what you want to achieve. There are complete tutorials & libraries on 2D lighting if that’s what you are after.

I want to have a movable mask for a texture in my game. For example for blend in effects with a gradient or variable overlay effects. The best sollution for what i Plan is to use texture masks. And i think now pixel shader is the right way… will try during weekend.

If you want to move the mask interdependently, you need to pass a second texture UV. Something similar to https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Vertices/VertexPosition.cs but in your case it would be ‘VertexPositionPosition’.
In your shader you can use something like

struct VSInputTx2
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float2 TexCoord2 : TEXCOORD1;
};