AlfonsAberg

I want to create 2D sprite scenes in GSE the way you work with layers in Photoshop.

This is an example of a scenario of what I would like to achieve:

Say, I have 2 layers of background and 1 layer of foreground. The first background layer is a sky with a sun. I want to blur this layer with a PixelShader. The second background layer is trees. This layer I also want to blur, but not as much as the layer beneath it, for a Depth of Field effect. The foreground layer contains the grassy ground and the player sprite. On top of all layers I want to put a glow effect from the sun also with a PixelShader. The glow effect should use a Glow Alpha texture that will be "deformed" by the layers, if something is covering the sun those pixel in the Alpha texture should be black.

This is the "code-flow" I imagine.

Clear
Draw Background(BG) 1st layer
Apply blur effect
Save texture to BGLayer1Texture
Clear
Draw BG 2nd layer
Apply blur effect
Save texture to BGLayer2Texture with alpha channel
Clear
Draw Glow Alpha
Draw BGLayer2Texture, black with AlphaBlending
Draw FG Layer black with AlphaBlending
Save texture to GlowAlphaLayerTexture
Clear
Draw BGLayer1Texture
Draw BGLayer2Texture
Draw FG Layer
Apply Glow effect using GlowAlphaLayerTexture

Ok, how do I go about implementing this I think I can find the pixelshaders(adapt FX Composers .fx), but how do I use SetRenderTarget and ResolveRenderTarget Or is there a better way then RenderTargets

Cheers
Alfons


Re: XNA Game Studio Express Post-Processing Shaders and Layers?

dczraptor

I'm by no means an expert with shaders, but I've been reading a bit and I think you might be able to pass a value (the distance away from the camera) to the pixel shader, so you won't need to do the depth-of-field in two passes with two screen clears, which will save a lot of rendering time. The more screen clears and render targets you make in a single frame, the slower your game becomes by a large magnitude.




Re: XNA Game Studio Express Post-Processing Shaders and Layers?

Jon Watte

Here is some code that uses render targets. The main thing to remember is that Xbox render targets are implemented as copy operations from framebuffer, so you should resolve one rendertarget before setting another, else you may lose your rendered bits.

Also, single-pass depth of field is not as good as multi-pass, because the edge between "near" and "far" neads special treatment. The passes outlined originally will likely work fine, look good (assuming a proper separated gaussian blur kernel is used) and run well on Xbox.



// set up inside LoadContent.
// note the hack to get better frame rates on
// my low-end laptop :-)

float ww = screenWidth_;
float hh = screenHeight_;
#if DEBUG
// make debug mode less sensitive to fill rate on smaller
// screens (assuming larger screens == faster graphics card)
if (ww <= 1024 && !fullRez_)
{
ww /= 2;
hh /= 2;
}
#endif
renderTarget_ = new RenderTarget2D(dev_, (int)ww, (int)hh, 1, SurfaceFormat.Color);




public void Draw(GameTime gameTime)

{
RenderTarget oldTarget = dev_.GetRenderTarget(0);
dev_.SetRenderTarget(0, renderTarget_);
try
{
Matrix view = curCamera_.ViewMatrix
dev_.Clear(Color.CornflowerBlue);
// do a bunch of rendering here ...
dev_.ResolveRenderTarget(0);
}
finally
{
dev_.SetRenderTarget(0, oldTarget as RenderTarget2D);
}
// use the texture -- here I'm drawing a full-screen quad
presentEffectAmount_.SetValue(grayscaleAmount_);
Texture2D tex = renderTarget_.GetTexture();
presentEffectTexture_.SetValue(tex);
dev_.Vertices[0].SetSource(screenQuadVb_, 0, 20);
dev_.VertexDeclaration = screenQuadVdecl_;
presentEffect_.Begin(SaveStateMode.None);
foreach (EffectPass ep in presentEffect_.CurrentTechnique.Passes)
{
ep.Begin();
dev_.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
ep.End();
}
presentEffect_.End();






Re: XNA Game Studio Express Post-Processing Shaders and Layers?

AlfonsAberg

Do I have to render a 3D scene I thought I could pass in a texture directly to a PixelShader without a going through a VertexShader. Like Michael Schuld writes in his blog, tutorial 8.

You all have to excuse me for being such a newbie. But programming shaders is really hard for me because of the syntax, lack of documentation and good tutorials. C# is easy to read, but .fx files are usually just t0, r0, c0; that for me doesn't make sense...yet.




Re: XNA Game Studio Express Post-Processing Shaders and Layers?

Jon Watte

You can do it many ways -- that's just how I did it (because the stuff I render is already 3D).

You will need to pass texture coordinates and screen coverage coordinates, but you can use the BasicEffect instead of writing your own effect just fine.

I'm hoping the code is some help anyway!

Btw: the FX files I write have no "r0" or "t0" or "c0" -- I write in HLSL , and use names for the semantics, which makes it more readable. But maybe not more comprehensible :-)





Re: XNA Game Studio Express Post-Processing Shaders and Layers?

Jeff Weber

Shawn discusses how to apply a pixel shader to a sprite here.