Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualnet8floz

Posted 30 January 2013 - 08:19 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:


#1net8floz

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:


PARTNERS