Effecting to some SpriteBatches ploblem.

I have just trying to add HLSL effect to SpriteBatch.Draw() method.
So,I tried like this;

        foreach (CustomComponent cr in cus)
        {
            foreach (var v in cr.func) _blurEffect.Parameters["E_" + v].SetValue(cr.funcCfg[v]);
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, _blurEffect);
            cr.Draw(spriteBatch, camera, this.GraphicsDevice);
            spriteBatch.End();
        }

note)“CustomComponent” is contains drawable objectives and “cus” is a list of its.

But, this code caused a problem, like this.
[PROBLEM IMG]

And… change the codes, this problem can be fixed.
[FIXED CODE]

        foreach (var v in cus[0].func) _blurEffect.Parameters["E_" + v].SetValue(cus[0].funcCfg[v]);
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, _blurEffect);
        foreach (CustomComponent cr in cus)
        {
            cr.Draw(spriteBatch, camera, this.GraphicsDevice);
        }
        spriteBatch.End();

[FIXED IMAGE]

I think this problem caused by spriteBatch.Draw(); inside foreach.
But, I can’t use Effect per one sprite If I using fixed code.
So, How can I solve this problem?

looks like you are using an alpha effect and it’s not using that in your hlsl code.

To make an alpha cutoff in hlsl add this line

const float alphaThreshold = 0.5f;
clip( texture.a < alphaThreshold ? -1:1 );

EDIT: Actually nvm, this is a different issue, I’ll come back to you soon. This would work if your game is pixel-perfect.

EDIT2: So the real issue is sorting, spritebatch can only sort the inside a begin() End() term. It looks like the character is drawn first and the wooden marker is drawn afterwards, but it is being ignored because the character has filled the depth already.

I am not 100% sure about the depth buffer properties of spritebatch and if it draws to it (@Jjagg) , I would have to check that out. But if you use the normal spritebatch.Begin(deferred) and sort yourself it will work.

I think by default it does not, but I’m not sure either.

thanks a lot.

float4 color = tex2D(TextureSampler,input.TextureCordinate)* input.Color;

if(E_glitch)
{
float size1 = grids * 100;
float2 uv1 = floor(input.TextureCordinate * size1 + 0.5) / size1;
color = tex2D(TextureSampler, uv1);
}
if(E_rndcolor)
{
float value = 0.299color.r + 0.587color.g + 0.114*color.b;
color.r = value;
color.g = rand;
}

const float alphaThreshold = 0.01f;
clip( color.a < alphaThreshold ? -1:1 );
return color;

Add these in last of PixelShader.

const float alphaThreshold = 0.01f;
clip( color.a < alphaThreshold ? -1:1 );

Then, it fixed.
Big thanks for kosmonautgames and Jjagg!

this will only work though if your pixels always cover a full pixel!

So you need to use samplerstate.pointsampler or make sure that your textures are scaled correctly.