Sign in to follow this  
Theodore Enderby

How to draw lots of billboard sprites

Recommended Posts

Howdy first post!



So I have all these "gameSprites" that contain "BillboardSprites".

They are pngs that are chopped up

and billboarded to be in 3D space.

I'm using them as characters and other things and I'd like to be able to draw a ton of them.

 

http://www.youtube.com/watch?v=duSshpK5GtE

(video of game before adding a lot more sprites!)


They are drawn in there own buffers whith their own indices and vertices arrays.





This is very slow and reading suggests that the graphics card would prefer if I told it about all

the gamesprites in one buffer instead.



I can add all their vertices and indices together, and draw them all I'd imagine.

But they are also drawn with an effect file, and that is passed its world matrix(Scale and position etc),

plus the billboarding effect and textures.



So how would I keep my ability to individually change scaling,positioning,texture and other things

in each gamesprite while still being able to draw all of them in one batch?



I'm new so I don't know if my understanding of xna is that great but this is where I'm at!


If someone could just give me some ideas or theories or examples that would be great.


 


public void Draw(GraphicsDevice graphics, Camera camera)
        {
            setVertexBuffer(graphics);
            
            //Gets texture from sprite sheet for animation.
            StopAtCurrentFrame(graphics);
            //set previous states
            RasterizerState prevRasterizerState = graphics.RasterizerState;
            BlendState prevBlendState = graphics.BlendState;

            Matrix billboardMatrix = Matrix.CreateConstrainedBillboard(WorldMatrix.Translation, camera.Position, Vector3.Up, camera.Forward, Vector3.UnitZ);
            Matrix ScalePositionFix = Matrix.CreateTranslation(Position * -scale);
            Matrix YFix = Matrix.CreateTranslation(new Vector3(0, (HEIGHT / WIDTH) * 2, 0));
            Matrix AppliedWorldMatrix = WorldMatrix * ScalePositionFix * YFix;
            //First Pass:
            //Render the non-transparent pixels of the billboards and store their depths
            billBoardEffect.Parameters["world"].SetValue(billboardMatrix * AppliedWorldMatrix);
            billBoardEffect.Parameters["view"].SetValue(camera.View);
            billBoardEffect.Parameters["projection"].SetValue(camera.Projection);
            billBoardEffect.Parameters["billboardSize"].SetValue(0);
            billBoardEffect.Parameters["alphaTestDirection"].SetValue(1);
            billBoardEffect.Parameters["colorMap"].SetValue(texture);

            graphics.BlendState = BlendState.Opaque;
            graphics.DepthStencilState = DepthStencilState.Default;
            graphics.RasterizerState = RasterizerState.CullNone;
            foreach (EffectPass pass in billBoardEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                     0,
                                                     0,
                                                     vertexBuffer.VertexCount,
                                                     0,
                                                     (int)(vertexBuffer.VertexCount * 0.5f));
            }
            graphics.Indices = null;
            graphics.SetVertexBuffer(null);

            //Second pass
            billBoardEffect.Parameters["alphaTestDirection"].SetValue(-1.0f);
            graphics.BlendState = BlendState.NonPremultiplied;
            graphics.DepthStencilState = DepthStencilState.DepthRead;
            //graphics.SamplerStates[0] = SamplerState.PointWrap;
            setVertexBuffer(graphics);

            foreach (EffectPass pass in billBoardEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                     0,
                                                     0,
                                                     vertexBuffer.VertexCount,
                                                     0,
                                                     (int)(vertexBuffer.VertexCount * 0.5f));
            }


            graphics.SetVertexBuffer(null);
            graphics.Indices = null;
            //set previous states


            graphics.BlendState = prevBlendState;
            graphics.RasterizerState = prevRasterizerState;
            setBounds();

 


Here's my draw for the GameSprite code, excuse the mess, still new:

Share this post


Link to post
Share on other sites

Hey so I've successfully implemented instancing for my sprites and I can draw a ton of them, thank you.

Now I've got to figure out how to put back in billboarding and the alpha test.

 

In my effects file I can probably using the view and projection matrices to make the sprite face the camera, correct?

As for how to do the multipass aplha test I was using I have no idea.

So back google.

 

If anyone has any tips I'd be happy to hear them.

Share this post


Link to post
Share on other sites

For bilboarding in my instanced particles I use folowing code, works like a charm:

            if (this.style == OrientStyle.Yonly)
            {
                Matrix billboardWorld = Matrix.Identity;
                billboardWorld.Forward = Vector3.Normalize(camera.Position - this.Position);
                billboardWorld.Right = Vector3.Normalize(Vector3.Cross(billboardWorld.Forward, Vector3.Up));
                billboardWorld.Translation = this.Position;
                Transform = Matrix.CreateScale(this.size) * billboardWorld;
            }
            else
            {
                var billboardWorld = Matrix.Invert(camera.ViewMatrix);
                billboardWorld.Translation = this.Position;
                Transform = Matrix.CreateScale(size * (life / 3000.0f)) * billboardWorld;
            }


For transparecy I used inferred shading's 3 layered transparency, but I am curious about forward approaches too (I have no idea how I can efficiently sort back to front instanced geometry, so i believe OIT is a must).

Share this post


Link to post
Share on other sites
As for Inferred I believe You would have to rewrite your code if you use forward rendering (since inferred is different redering pipeline). OIT stands for order independent transparency. You could use depth peeling for instance. If you want to use perfect transparency just use clip(x) in pixel shader.
Partial transparency is very difficult task when it comes to real time rendering.

Some linkts for you:
http://www.eng.utah.edu/~cs5610/lectures/OrderIndependentTransparency.pdf
http://developer.download.nvidia.com/SDK/10/opengl/src/dual_depth_peeling/doc/DualDepthPeeling.pdf
http://mynameismjp.wordpress.com/2010/01/10/inferred-rendering/ Edited by Tasaq

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