Rob
Rob

Reputation: 1777

XNA SpriteBatch and BasicEffect not compatible?

I would like to control ambient lighting for a 2D SpriteBatch rendered set of graphics on a global scale. I realise I can do this by blending the color passed into SpriteBatch.Draw but I'd rather do this globally.

The BasicEffect shader contols fixed function pipeline style lighting in XNA for simple scenes.

Is the BasicEffect shader and that of the SpriteBatch incompatible? Has anyone got these working together?

Upvotes: 4

Views: 1976

Answers (4)

tooshel
tooshel

Reputation: 1564

I was having this issue too and "they are incompatible" is wrong. The spriteBatch changes things like the "DepthStencilState" and the "BlendState" on the graphics device. To fix that add this after spriteBatch.End.

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

spriteBatch changes the DepthStencilState to None and that's why 3D objects don't draw correctly. Other properties get changed too. Check these out:

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

Upvotes: 2

Joel Martinez
Joel Martinez

Reputation: 47751

Yes, they are incompatible.

This is because the spritebatch sas to use its own custom vertex shader to do the quad rendering. You can use your own custom pixel shader, and there is a sample on the xna creator's club education site that shows you how to use custom pixel shader effects with the spritebatch.

Upvotes: 0

Sekhat
Sekhat

Reputation: 4479

As far as I'm aware, sprite batch uses it's own effect internally when rendering it's quads to the screen, as you can only render with one effect at the time, my answer would be no, they are not compatible.

Upvotes: 0

bufferz
bufferz

Reputation: 3476

This isn't a direct answer to your question, but may be of some use to you. BasicEffect and SpriteBatch objects are just shaders that the XNA team bundled with the release.

The source code of these shaders is available to for you to view/modify to your liking. Doing your own shaders will keep you from running into roadblocks continuously.

Here's where you can find more info and the source code: blogs.msdn.com

Upvotes: 2

Related Questions