开发者

Render multiple layers in XNA

开发者 https://www.devze.com 2022-12-26 03:31 出处:网络
I\'m using XNA, and I\'ve run into a little problem. I need to support multiple layers, each with a distinct z order (I call these \"viewports\"). A picture is worth a thousand words, so here\'s what

I'm using XNA, and I've run into a little problem. I need to support multiple layers, each with a distinct z order (I call these "viewports"). A picture is worth a thousand words, so here's what it should look like:

http://www.charlesstrahan.com/so_files/viewports.png

There are several things to notice here. Sprites do not render outside of their viewport, as you can see with Sprite B. Also, notice how the viewports are rendered - it's very similar to "layers" in Photoshop. Although Sprite C is has a z order of -1000, C still renders above Sprite A because its viewport's z-order is a greater than A's viewport's z-order.

There's one last detail that I couldn't display very well in the above picture. Each viewport needs to optionally render a color over its region of the screen - you could think of it as a "tinting" affect.

I'm completely at a loss when it comes to doing this the best way in XNA, so I could really use a short snippet of C#/VB.NET code that demonstrat开发者_如何学Goes this in action. Any help would be greatly appreciated.


You can easily do this with RenderTargets :-) There are lots of resources on how to use them on the web (example).

If you're just starting yoru project though, consider installing the XNA 4.0 (in CTP right now via the windows phone SDK). It's gotten a lot easier in the new version ... from an article that Shawn Hargreaves put out recently, RenderTarget changes in XNA Game Studio 4.0.

List<Texture2D> textures = new List<Texture2D>();

for (int i = 0; i < 100; i++)
{
    RenderTarget2D rt = new RenderTarget2D(...);

    GraphicsDevice.SetRenderTarget(rt);
    DrawCharacterAnimationFrame(i);
    GraphicsDevice.SetRenderTarget(null);

    textures.Add(rt);
}

And the "tinting" feature you were asking about is braindead easy with this because you can just call the Clear method when you go to render each render target with whatever color you want.


You can just sort your objects into groups then render each group in order with clipping

device.ClipPlanes[0].Plane = plane;
device.ClipPlanes[0].IsEnabled = true;
0

精彩评论

暂无评论...
验证码 换一张
取 消