Every game's success depends largely on its look and feel. This directly means that the game must have an eye-catching graphical display. It is not always possible to provide maximum quality graphical assets due to space and heap restrictions. So, there has to be a way to create or improvise the graphical assets at runtime for display. This necessity gave birth to the concept of shaders.
Shaders can operate on any visual element and can tweak every pixel of drawable elements before rendering. Mostly, shaders are optimized for a specific graphics processor. However, nowadays, shaders can be written to support multiple processors on multiple platforms.
Android accommodates the option to work with shaders in the Android framework itself. Additionally, OpenGL shaders can also be used and customized with the help of the Android NDK. There are many occasions where exquisite graphical quality is delivered with the help of shaders without excellent raw art assets.
We will have a discussion about shaders in this chapter from the point of view of Android game development through the following topics:
Many developers develop games on Android, but do not possess much knowledge about shaders. In most cases, developers do not need to work with shaders, or there are some pre-defined shaders inside the game development framework or engines.
In 1988, the animation studio Pixar introduced the modern concept of shaders. However, GPUs were not capable of handling shaders at that point of time. OpenGL and Direct3D are the first two graphic libraries to support shaders. GPU started supporting shaders through pixel shading at the 2D level. Soon, it was enhanced to support vertex shaders. Nowadays, geometry shaders are also supported by OpenGL 3.2 and Direct3D 10 libraries.
Let's now dive a bit deeper into shaders to understand their definition, necessity, and scope for Android games.
In simple words, a shader is an instruction set to manipulate the visual display of the input graphic assets.
Let's elaborate the definition a bit. All the instructions are basically done through programming. That's the reason the concept of a shader exists only in computer graphics. Shaders are able to perform a computation based on the instruction and input asset to produce more efficient output-displayable assets.
Typical shaders can process either a vertex or a pixel. Pixel shaders can compute on the color, depth, and alpha properties of an asset. Vertex shaders can compute the position, color, co-ordinates, depth, illuminations, and so on of a vertex. Thus, shaders can be primarily divided into two categories depending on the operational base type:
In the normal practice of game development, Android developers do not bother about shaders. But the necessity of shaders is inevitable. Initially, for small-scale games, art assets are used without improvement. Any modification to the assets is managed by the old process of updating the art asset itself.
Shaders can minimize this extra time-consuming effort. The same asset can be manipulated to create different objects on screen. For example, you can blur out the object as it goes out of focus, change the color of the sprites during gameplay to indicate different teams or players, create masks of art assets, and so on.
Shaders have the following benefits:
However, using shaders may lead to some negative consequences:
Only the first two are actual direct consequences of using shaders. The rest of the problems can occur only if the developer uses a badly written shader or faulty shader. Therefore, it is extremely necessary to choose the perfect shader for a specific task.
Sometimes, the shader process takes a long time, resulting in poor FPS output. A few old GPUs do not support all kinds of shaders. Therefore, the developer should check and confirm the hardware platform on which the shader is to perform.
Shaders can be used in a variety of sectors related to computer graphics, such as image processing, photography, digital animation, video/computer/digital games, and so on.
The gaming sector is one of the largest communities that uses shaders. The Android platform is no exception. Android game developers use shaders on a large scale in both 3D and 2D games.
Frankly speaking, 2D games do not have much scope for shaders. Only a pixel shader can manipulate the color, opacity, saturation, and hue of a pixel. This is useful when the same raw assets are used for different visibility.
For example, a 2D cricket game has many teams with different outfits to distinguish between them. The developer creates all the sprite animation assets in one design and applies shaders to manipulate color differently for different teams. Thus, the output sprites have different visibility and are recognized easily by the player.
18.191.233.43