I am working on a 2D game, now doing the graphics engine, and reached the infamous transparency/depth testing issue, which I intend to solve by breaking up the scene into a few overlapping layers, so I don't ne开发者_如何学Pythoned to depth-sort everything.
Should I separate the objects into different containers depending on which layer their on, or should I use some kind of a buffer to draw them on separate layers, then blending them together in the end? If I use the latter, what should I use or how should I do it exactly?
Thanks for the help!
It sounds like you have a pretty good handle on what you are going to do already. I guess the decision between per object sorting, or rendering everything to layers then compositing is a bit arbitrary. If you are going to apply different effects per layer, or else try to reuse certain graphics (for example if you have a background layer that rarely gets updated, or can run at lower fps), then doing a layer based system makes more sense. On the other hand if you are going to just be redrawing everything each frame anyway and don't need fancy per layer tricks, you would be better off just sorting the objects since it would use less GPU memory/fill rate to blit all the textures around.
I'd like to quote the conclusion of this article:
The upshot of this is simply that you can't simply render translucent objects in any order without special consideration. If you have enough translucent surfaces moving around in a sufficiently complex manner, you will find it very hard to avoid errors with acceptable realtime algorithms.
It's largely a matter of what you are prepared to tolerate and what you know a priori about your scene content.
I agree, most of it depends on what you are drawing.
Keep in mind that fragment shaders has the discard keyword which is your friend: if you are alphing objects using textures, this would aid you to avoid sorting. I would notice that you could have the same problem in the future by rendering translucent/transparent objects in the same layer.
Maybe, it would be better to implement sorted object tree in order to avoid to sort every object every frame. But it all depends on the objects organization and application constraints.
精彩评论