Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


How to draw lots of billboard sprites


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 net8floz   Members   -  Reputation: 139

Like
2Likes
Like

Posted 30 January 2013 - 07:55 PM

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.

 

(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:



Sponsor:

#2 qorthos   Members   -  Reputation: 165

Like
3Likes
Like

Posted 30 January 2013 - 08:54 PM

Instancing!

 

Try reading this:http://www.float4x4.net/index.php/2011/07/hardware-instancing-for-pc-in-xna-4-with-textures/



#3 net8floz   Members   -  Reputation: 139

Like
1Likes
Like

Posted 30 January 2013 - 09:14 PM

Oh looks good I'll try this out soon. Thanks.



#4 net8floz   Members   -  Reputation: 139

Like
0Likes
Like

Posted 31 January 2013 - 03:27 PM

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.



#5 Tasaq   Members   -  Reputation: 1258

Like
1Likes
Like

Posted 31 January 2013 - 05:11 PM

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).



#6 net8floz   Members   -  Reputation: 139

Like
0Likes
Like

Posted 31 January 2013 - 05:31 PM

Wow your billboarding matrix works great. Way simpler than my attempts.

Could you explain more about your "inferred 3 layered shading" and "OIT", I'm unfamiliar with

these and I am having trouble finding docs on it.



#7 Tasaq   Members   -  Reputation: 1258

Like
0Likes
Like

Posted 03 February 2013 - 04:26 AM

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, 03 February 2013 - 04:27 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS