Use of shaders in games

Shaders are vastly used in games and animation, especially when creating dynamic lighting, changing tints, and making dynamic visual improvements. Sometimes, the world environment is created with shaders.

Shaders in a 2D game space

Only pixel shaders can be used in 2D games. Each pixel of a digital image is considered a fragment. This is the reason why pixel shaders are also called fragment shaders. Pixel shaders can only perform color changes, tiling, and masking.

BitmapShader, ComposeShader, LinearGradient, RadialGradient, and SweepGradient are the variants of Android 2D shaders.

A 2D game world is created with images. Developers often choose to create different assets to give the same object a different look and feel. In this process, developers end up making a bigger APK with almost the same use set.

Sprites can also be a field where shaders can hold a significant role. When using the same sprite to create different objects, the colors of certain fragments need to change dynamically. Pixel shaders can be very useful here.

Shaders in a 2D space are used to change color, blur segments, change brightness, change opacity, tint images, and so on.

Shaders in a 3D game space

The most common use of shaders in 3D games is for dynamic shadow. In modern game development, a shadow is an inevitable element to improve the game experience. 3D models look real after applying a texture.

In Android, a 3D shader is applied through OpenGL. We have already discussed an example:

Shaders in a 3D game space

A raw model with only vertex information

This is a simple model without any lightening or shaders. Let's apply some shaders to give it a solid 3D look:

Shaders in a 3D game space

A simple flat shader applied

Now, the developer can apply any texture or color to give it a different feel. In this part, the developer can choose to restrict this with color or texture. Generally, textures are used in this kind of scenarios in order to make the model visually real. However, this costs more than just color manipulation.

We will see a color and lighting change here to get a completely different feel of the same object. There are different procedures to handle different scenario requirements for the game.

This example, however, is just a visual representation of how shaders can manipulate 3D models for a different look and feel:

Shaders in a 3D game space

Shaders and game performance

Shaders are usually process-heavy. A fragment shader processes each fragment of a texture and manipulates its data. A large texture may lead to a visible delay in the game loop.

We can see shaders from different perspectives to create an idea of performance. Large textures decrease performance, and many small textures also affect performance. There has to be a balance between them to have a feasible real-time use of shaders.

Creating shadows is one of the extensive uses of shaders. However, the quality of shadow processing is inversely proportionate to performance. In high-quality games, we can experience real-time shadow. Shaders map the object vertices and process it according to the light direction. It is then projected on the X-Z plane to create shadow. Shadows are merged with objects on the plane and with other shadows.

Shaders can be used to improve world visibility with different lights, materials, and colors.

Here are some pros of using shaders in games:

  • Complete flexibility when rendering assets
  • Fewer asset packages and increased reusability
  • Dynamic visual effects
  • Dynamic lighting and shadow
  • Sprite manipulation on the fly

There are few disadvantages of using shaders:

  • Comparatively low frame rate
  • Performance drop
  • Required supported hardware platforms and graphic drivers

In spite of the few disadvantages, shaders have proved enough to be an intrinsic part of game development. Any performance drop is handled by upgrading the hardware and graphic drivers.

Nowadays, shaders are being optimized for embedded devices with limited resources. This even opens up the chance to increase the use of shaders on almost every platform, without affecting the performance significantly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.139.8