Thanks to the development of technology, developers have been able to create graphics in games close to photorealism. However, there are still a lot of problems in the implementation of the visual component that specialists face during the production of projects. One of them is often mirrors.
Why it’s hard to create mirrors in games
The main problem with creating mirrors and any reflections in games is not the methodology. There are plenty of implementation options, but choosing the best one for the specifics of a particular project and guaranteeing the right level of optimization is the main challenge. And there is no single unified solution.
For a correct and truthful reflection, the mirror should show the character, the environment, and the objects behind. At the same time, the image should change depending on the angle at which the camera or character is located and show movements. That is, mirrors should respond to changes in space in real time.
In order to show the environment behind the character in the reflection, it needs to be rendered, the angle of entry and exit from each point within the coverage needs to be calculated, and everything needs to be reflected in the mirror. At the same time, it was necessary to take into account the intensity and direction of lighting, visual effects, and light-reflecting surfaces.
The simplest example is the first-person view. In games that use this camera option, the protagonist’s model can be adapted in some way for ease of control or certain mechanics. The player does not see this, but mirrors need to show the character correctly and without changing the proportions of the body.
On top of that, there is the need to guarantee high-quality optimization in the presence of mirrors, and this is the most difficult task. Each reflection for a fair reflection slows down the frame calculation by a fraction of a second.
In addition, scenes are usually rendered gradually, meaning that assets are loaded a moment before the player sees them. In certain situations, the presence of a mirror makes it necessary to adjust this approach and load twice as many assets. This adds complexity to the optimization. As a result, rendering programmers, who are usually responsible for creating mirrors, have to try to make everything work smoothly.
Screen Space Reflection (SSR)
Let’s start with a method that is often used to implement mirrors in modern games. Screen Space Reflection works with information already embedded in the scene. The engine takes a snapshot of the current frame and analyzes it to determine the objects to be reflected. Next, a simplified, low-resolution version of the scene is created, known as a display buffer or G-buffer. It contains only the objects and surfaces that will be shown in the reflection.
This buffer is used to calculate all the reflections for the final version of the display. Using it, the engine projects rays from the camera position in the screen space. The latter are used to process the buffer and further determine the color and intensity of the reflection.
Planar Reflections
This method involves additional rendering of the entire scene, which is applied to a separate plane. From this plane, the image is projected onto reflections. An important condition: these surfaces must be parallel to each other.
When using Planar Reflections, the reflection shows an exact copy of the environment. However, this method has several important limitations. It can perfectly reflect the scene along one axis. This is good for realizing mirrors and reflections on flat surfaces, but Planar Reflections cannot be used for other geometric shapes.
Cubemaps
Cubemaps was first introduced by NVDIA in 1999 to realize reflections in car mirrors in racing games on the PlayStation 1. That is, the technology is quite old, but it has not lost its relevance.
The name implies that the method uses a special cube around which the stage is located. 2D images of the environment are projected on its surface, and this creates a panoramic view of the environment.