Today, let's take a dive into the history of mirrors in video games and learn how modern-day developers handle reflective surfaces in their games.
Image Credit: CDPR, Cyberpunk 2077
As many of our gamedev-savvy readers already know, recreating certain real-life objects and phenomena within video games can sometimes prove to be an exceptionally challenging task. Simple everyday items like doors, stairs, water bottles, and many others have long puzzled game developers, often leading to sleepless nights as they strive to discover the most effective means of integrating these elements into their projects.
Of course, with the advancement of technology over time, many of these challenges have been overcome. Nevertheless, certain objects still pose a formidable challenge for implementation even today, and if you've read the title, you already know which specific object we'd love to explore today – mirrors.
Image Credit: LeoChoax/IO Interactive, Hitman 3
To understand how mirrors work in video games, it's essential to first learn how they function in real life. Unless you've been sleeping through your physics classes, you probably know that mirrors work not by magic but by reflecting incoming light waves into the surrounding space.
In the real world, mirrors typically feature a smooth surface with a reflective layer on the back, often made of silver or aluminum. When light strikes the mirror, it follows the law of reflection, where the angle of incidence equals the angle of reflection. This process results in the creation of a virtual image behind the mirror, giving the appearance of the reflected object.
The key word in all this word salad is "light". Since traditional light waves do not exist in the digital realm, developers must employ alternative techniques to simulate reflections, and the effectiveness of the approaches can vary significantly. Here's a great meme from Reddit user Tara_is_a_Potato that illustrates some of these methods perfectly:
In older games mirror reflections typically consist of lower-resolution duplicates of environments where the action takes place, as well as duplicate player character models that spawn whenever the player should appear.
A perfect example is the mirror room from Super Mario 64, where thanks to the game's low resolution it was possible to simply copy the level and make it look like it has a mirror. Here's a great video from TheRPGLORD RPGLOVER YouTube channel that provides a behind-the-scenes look at this level:
And here's the earliest instance of somewhat accurate reflections in 3D video games that I was able to find:
A similar approach, more commonly employed than the previously mentioned method, is referred to as Planar Reflections. In the case of Planar Reflections, developers incorporate an extra rendering pass for the entire environment. Essentially, this involves crafting a room and introducing a virtual camera to function as a reflective mirror.
This methodology is particularly effective when dealing with uncomplicated, minimalistic spaces that don't feature an abundance of intricate details, as highly complex scenes prove to be less compatible with planar reflections. Attached below are a couple of easy-to-understand guides on how to set up Planar Reflections in Unreal Engine 5 and Unity.
Another technique, albeit somewhat antiquated, that can still be effectively utilized is Cube Maps. Cube Maps are a texture mapping method used in 3D computer graphics and computer games to replicate the surroundings of an object or scene. They comprise six 2D images, each representing a view from a specific direction, corresponding to the six sides of a cube.
Cube Maps are commonly employed to simulate reflective surfaces, such as water, glass, or metallic objects. By sampling the cube map, the renderer can determine what should be reflected on the surface of the object. If you'd like to learn more about cube mapping, we recommend checking out these great tutorials:
One more method that strikes a good balance between visual quality and performance is Screen Space Reflections (SSR). This method is employed to simulate the reflection of objects in mirrors, water, or other reflective surfaces by analyzing the screen's content and calculating reflections based on what the camera currently sees.
Because SSR operates within the screen space, it only takes into account what's currently visible on the screen. It doesn't generate reflections for objects that are off-screen, which can be a limitation compared to other reflection techniques, like the previously mentioned Cube Maps. Here are some great guides that explain the nuances, advantages, and limitations of Screen Space Reflections in more depth:
Finally, the most widely discussed approach to implementing in-game mirrors nowadays is undoubtedly Ray Tracing. Ray tracing is a performance-taxing rendering technique employed in computer graphics to create lifelike images by mimicking the interaction of light with objects in a scene, thus enabling the generation of hyper-realistic lighting and reflections.
In Ray Tracing, the key element is, you guessed it, the "ray." These rays are imaginary lines that represent the path of light as it moves through a scene. Typically, rays are cast from the camera (the viewer's perspective) into the scene and then traced as they interact with objects. When a ray is cast into the scene, the renderer checks for intersections with objects in the 3D environment.
Ray Tracing simulates how light behaves when it encounters reflective and refractive surfaces. When a ray hits a reflective surface, it produces a reflection ray, continuing the simulation of light bouncing off the surface. Similarly, when a ray strikes a refractive surface, it generates a refraction ray to simulate the change in the ray's direction as it passes through the surface. Attached below are some insightful guides we recommend watching to learn everything about Ray Tracing.