Sign in to follow this  
GodBeastX

3D World and 2D Sprites

Recommended Posts

I've read as many articles on this that I could possibly find. Many use techniques like billboarding and such, but this won't work in the case that I need it. Here's the problem. The sprites render in the correct positions, but their Z depth causes them to jut into the terrain if they are near objects. Screenshot Here As you can see that isn't an option to allow that to happen. So I'm curious if anyone knows some techniques to get the Z correct for the sprites.

Share this post


Link to post
Share on other sites
Would it be sufficient to simply disable Z-test? Or maybe better still, clear the Z-buffer after rendering all 3D stuff, so that 2D stuff is still properly depth-tested against other 2D stuff, but not at all against 3D.

Share this post


Link to post
Share on other sites
Well, clearing the Z buffer would make the sprites render regardless. I need them to be behind any terrain that would be in front of them. I think I might have came up with an idea of how to fix the problem. If I project the sprite onto a flat surface (and not draw the surface), it would be in a perfect Z upwards position, which should fix the problem. Just gotta make sure I position the flat surface coordinates correctly before projecting on it.

Share this post


Link to post
Share on other sites
Perhaps you should be using cylindrical billboarding instead of spherical, meaning basically that your sprites could be aligned towards the camera but with a constrained up vector. If the camera were directly over them, they would appear flat, but at an angle they would simply appear to be standing on the terrain. You could also encode depth information in your textures and use a fragment shader to write to the depth buffer, making your sprites actually have some contour, but this might be overkill, and wouldn't really solve the problem you're having now anyway. Just an additional suggestion that might be nice :-)

Share this post


Link to post
Share on other sites
The problem with the cylinder method is the camera is on an angle all the time, so the sprites won't look quite right since they are drawn to be on an angle. I think projection is the only method, unless I used stencil buffers, but I think that's overkill too.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why could'nt you just slap a texture and alpha map on a quad polygon. If your engine already has 3d incorperated, then your sorting would already be implimented for other 3d objects in the scene right?

Share this post


Link to post
Share on other sites
That screenshot is 3D quads with alpha'd textures. The problem with sprites is they always have to face the camera to look correct. If they don't they won't look correct and can even end up skewed. The problem is, facing the camera doesn't always mean they will fit in the geometry of the map correctly (as the screenshot shows). So the question is, how do you make the quad shape itself to fit in the geometry correctly and make the texture not look skewed. I was thinking that if I project the quad's coordinates so that they remain the same shape/size in the view but end up straight up and down along the Z, that would fix the problem. It requires intersection tests though for each quad, which means even on screen sprite would be taking intersect tests. I'm looking to see if someone invented a better method.

Share this post


Link to post
Share on other sites
Maybe you can adjust the Z bias when rendering your sprites based on the camera angle.

It goes like this... your sprite has a location on the ground, I assume. Before rendering the sprite, translate that position vector by the character's height in the height axis (either Z or Y, depends on what you're working on) and check the distances between that top position and the ground position to the camera, and then get the difference between them.

Use that to push your z-bias further close to the camera so the sprite top area will not intersect things behind it.

If that doesn't work, then there is no other way: you will have to disable z-reads for the sprites, and sort all objects back-to-front, like in the old days. Considering how your game looks, that'll not affect the performance that much, if node right.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you use an isometric or axionometric view,
you can constrain your vertical walls to be of constant Z.
So you wouldn't have any problem with your sprite being in a parallel plane. Of course you would still have to constrain the movement of your sprites near walls anyway since whatever your sprite/wall orientation there is still a case which will end up in both penetrating (that is also true of 3d objects.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here's what google found about axonometric projection:

http://www.ul.ie/~rynnet/keanea/HomePage.html

Of course the choice of the axis alignment is purely arbitrary (and can be changed dynamically) but what is important for you is that vertical lines are of constant Z so that your two d plane at constant Z won't interpenetrate an object placed further.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this