I searched some tutorials to learn HLSL and every tutorials I found talked about VertexShader and PixelShader. (the tutorials used a 3D game as example).
So I don’t know if vertexShader is usefull for 2D games.
As I know, a 2D game doesn’t have any vertex right ?
I have to use shader as post-processing.
My question is : Could you tell me if I have to learn VertexShader for my 2D game ?
2D graphics in XNA (and Direct3D, OpenGL, etc) are achieved by rendering what is essentially a 3D scene with an orthographic projection that basically disregards the depth of the vertices that it projects (things don’t get smaller as they get further away). You could create your own orthographic projection matrix with
Matrix.CreateOrthographic.While you could render arbitrary 3D geometry this way, sprite rendering – like
SpriteBatch– simply draws camera-facing “quads” (a square or rectangle made up of 2 triangles formed by 4 vertices). These quads have 3D coordinates – the orthographic projection simply ignores the Z axis.A vertex shader basically runs on the GPU and manipulates the vertices you pass in, before the triangles that they form are rasterised. You certainly can use a vertex shader on these vertices that are used for 2D rendering.
For all modern hardware, vertex transformations – including the above-mentioned orthographic projection transformation – are usually done in the vertex shader. This is basically boilerplate, and it’s unusual to use complicated vertex shaders in a 2D game. But it’s not unheard of – for example my own game Dark uses a vertex shader to do 2D shadow projection.
But, if you are using
SpriteBatch, and all you want is a pixel shader, then you can just omit theVertexShaderfrom your.fxfile – your pixel shader will still work. (To use it, load it as anEffectand pass it to this overload ofSpriteBatch.Begin).If you don’t replace it,
SpriteBatchwill use its default vertex shader. You can view the source for it here. It does a single matrix transformation (the passed matrix being an orthographic projection matrix, multiplied by an optional matrix passed toBegin).