Clipping Shader Post-Proccess

Started by
3 comments, last by pristondev 8 years, 1 month ago

Hey guys, I need a help to make a circular clipping with shaders in postprocess.

Something like that:

clip_image002_thumb.jpg?w=488&h=383

I using this mask to make this:

ba5b3e36a7.png

Post Process:


sampler ColorSampler : register(s0);

And that to call the texture inside shader:


texture MaskTexture;
sampler2D MaskSampler=sampler_state 
{
    Texture = <MaskTexture>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

Some friends told me to use clip function, but I dont found it in any place, so I dont have idea how to use that, if someone can help me I will be grateful.

Thank you guys!

Advertisement

'clip' is documented Here.

It kills the pixel if the number passed to it is less than 0. In your case, sample the texture (which I presume is white / 1.0f), subtract 0.5 from it and pass that value to cilp.

If the sampled value was black / 0.0, then subtracting 0.5 from it will make that value negative and thus kill the pixel. If the sampled texel was white / 1.0f, then subtracting 0.5 from it will leave the value at 0.5 and the pixel won't be killed.

Of course, if you only want circles then it would perhaps be easier to mathematically calculate the distance in the pixel shader and pass -1 to clip if the distance is greater than the radius of the circle. No texture required.

The stencil buffer is ideally suited for this sort of thing if you intend to repeatedly draw to the same target with lots of draws, but since you'd first have to populate the stencil buffer with the circle first by some means, your approach isn't too bad if this is a one-off draw.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

'clip' is documented Here.
Of course, if you only want circles then it would perhaps be easier to mathematically calculate the distance in the pixel shader and pass -1 to clip if the distance is greater than the radius of the circle. No texture required.


Ye is just circles... Can give some example how do this with math, no textures in hlsl?

Sorry but im not pro in dx for now.

Anyway thanks for ur help :D

Say uv of (0.5, 0.5) is the middle of your screen, and the corners are at (0,0) and (1,1)...

Lets change it so that the middle is (0,0) and the corners are at (-1,-1) and (1,1)

float2 temp = uv * 2 - 1;

Then let's get the distance to the middle:

float distance = length(temp);

Then decide what the radius of your circle should be, e.g.

float radius = 0.91;

Then remove any pixels outside of that radius:

clip( radius - distance );

Thank you man!

I got it.

This topic is closed to new replies.

Advertisement