Lighting in Unity 5

Unity has become more and more popular among aspiringgame programmers. This is due to the fact that Unity directly supports a multitude of platforms such as mobile, desktop, and console environments. Moreover, it is free to use for lower revenue developers or studios.

Lighting in Unity 5

Unity supports several technologies and components. Some of the key components are the lighting and illumination techniques. In Unity you can illuminate a scene by simulating the complex behaviour of lights or by applying a simple lighting model.

Lighting in Unity 5

This tutorial will focus on explaining how lighting works in Unity 5, the lighting types and properties, and how to use them to create rich illumination effects.

Prerequisites

First, ensure you have the latest version of Unity. In this tutorial we’re using version 5.5.0f3. Make sure that you are using the latest Unity version; otherwise you may have problems following the tutorial and using the physics joints.

Next, download the LightingInUnity5-Starterfile. Unzip and open the project in Unity. The Demo scene should open automatically, but if not, you can open it from the Assets folder.

Direct and Indirect Lighting

In a real-world scenario, you have two types of lighting effects: direct and indirect lighting. Direct lighting, as the name suggests, is light that comes directly from a light source (a lamp, the sun, or other). On the other hand, indirect lighting is light that comes from another object.

Let us test and implement both of them.

Direct lighting can already be seen in action in your Scene. As you probably noticed, you have a directional light in the scene called Directional Light.

Directional lights are mostly used in outdoor scenes for sun and moonlight. They affect all the surfaces of the objects in the scene. They are also the least expensive to the graphics processor.

You can select the Directional Light and view its properties in the Inspector pane. You will notice several interesting properties such as Type, Color, and Intensity. You can play with the Color and the Rotation properties and see the result in real time. By changing those values, you are directly playing with directional light.

Lighting in Unity 5

At this moment, your 3D scene does not have any indirect light. You can check that by moving the Sphere closer to the green or red wall. The Sphere colorwill not change.

Lighting in Unity 5

One way to use indirect lighting is to use static GameObjects. Keep in mind that the use of static game objects can improve the performance of your game rendering, but will also reduce the quality, so you must find the right balance for your game.

Add a box mesh box into your scene (GameObject> 3D Object> Cube), and place it next to the sphere. Name it Cube.

Lighting in Unity 5

Select the Cube, and inside the Inspectorenable the Static property.

Lighting in Unity 5

When you enable the Static property, Unity automatically creates a light map for that object and applies the correct illumination model. You can now move the camera towards the Cube and see the indirect lighting working. The green (in the image) or the red color is now spreading into the Cube.

Lighting in Unity 5

If you change the direction of the Directional Light, you will notice that Unity automatically updates the light maps as well.

The indirect lighting that is being applied to the Cube is not applied to the Sphere because the Sphere is not static. You can solve this by making the Sphere static (Inspector> Static).

At this moment, you can add and configure direct and indirect lighting to static objects. However, 3D scenes are composed of dynamic objects. Therefore, how can we apply such effects to those objects? That question leads us to the next section.

Light Probes

When your scene contains non-static objects, you need to use specific lighting techniques to correctly illuminate them so that they don’t look disconnected from the scene.

Through the use of light probes and their positions, you can sample strategic points in the scene. Each light probe can sample a specific region and then calculate the lighting in that specific region. These calculations are fast enough to be used during gameplay. The use of light probes avoids any disconnect between the lighting of moving objects and static light-mapped objects in the scene.

The Sphere is a dynamic object, therefore, and contrary to what it may seem, it is not correctly illuminated. If you turn the Directional Light off, you will notice that the only lighting affecting the Sphere is the ambient light. To correctly illuminate the Sphere, you need to use light probes.

To add light probes to an area of your scene, you go to Component > Rendering > Light Probe Group.This will create a light probe group in your scene.

Lighting in Unity 5

The next step is to put the probes of the group in the correct position. You now want to place them near each corner of the Box.

The best way to do this is to change to an orthographic view (by clicking on the cube on the upper right corner of the scene). Then, select each light probe node and drag it into each Box corner (similar to the next image).

Lighting in Unity 5

Repeat the process until you have positioned all nodes.

Lighting in Unity 5

There are scenes that require additional probes to correctly illuminate objects. In order to add more probes, you can select one probe and then under the Inspector, click on the Duplicate Selected button.

Lighting in Unity 5

After the duplication, you need to place the new node in the right place (the duplicated one will spawn in the same position as the selected one).

Lighting in Unity 5

If you take a closer look at the interface in the Inspector, you will notice that you can also add individual probes (Add Probe), delete probes (Delete Selected), or select all the probes from the group (Select All).

To see the light probes in action, select the Sphere and add a Rigidbody to it (Add Component >Rigidbody). Then, assign a material to the collider. You can use the rubber material.

Lighting in Unity 5

Now, place the Spherenear the top of the Box and press Play. You can now see the proper illumination of the Sphere. To see the differences, disable the light probes and Play the scene again.

Lighting in Unity 5

Point Light

Point lights are the most common light in games. They’re usually used for explosions and light bulbs. Since they emit light in all directions, they have an average cost on the graphics processor. However, calculating shadows when using point lights is more expensive.

Add a point light by selecting GameObject > Light > Point Light. Next, position the Point lightinside the Box near the top.

Lighting in Unity 5

Select the Point light and take a look at the parameters in the Inspector.

Lighting in Unity 5

The first parameter isType. Here you can set the type of light you want to use. You can either select Spot, Directional, Point, or Area. Each provides a specific light effect. You can select any of the options and see the results in real time. However, for this tutorial you will use the Point option. This will create a light bulb effect (light shines in all directions equally).

The second parameter is Baking. You can set it to Realtime, Baked, or Mixed. Leave the value as default. The Range parameter defines how far the light is emitted from the center of the Point light. The Color parameter defines the emitted light color.

Intensitydefines the brightness of the light, and Bounce Intensity defines the indirect light intensity multiplier. The Shadow Type defines the shadow properties and shadow types. You can either set it to No Shadows, Hard Shadows, or Soft Shadows. Remember that point light shadows are the most expensive for the engine, so be careful when selecting this option.

If you selectHard ShadowsorSoft Shadows, you will see the shadow produced by the Sphere and the Point light. When you change the Strength value, the shadow will also attenuate or accentuate. The Resolution lets you define the detail level of the shadows. Finally,Bias andNormalBiaslet you configure the offset used when comparing the pixel position in light space with the value from the shadow map.

The Cookie is an optional parameter that represents an alpha channel of a texture used as a mask that determines how bright the light is in different positions. Since this is a point light, the texture used must be a Cubemap.

The Draw Halo option only renders a halo around the light source.Flare defines a reference to a flare to be rendered at the light’s position. Flare and Draw Halo can be useful when debugging the 3D scene and possible bottlenecks.

Render Mode defines how important lighting is when the renderer is rendering the scene. The more important, the more intensive the rendering will be. The Render Mode can be set toAuto,Important, orNon-Important. Finally, Culling Mask is used to select or exclude groups of objects that are affected by the point light.

Now that you’ve seen all the point light properties, let’s talk about direct and indirect lighting when using point lights. When you create a game, one of the main characteristics is the use of lighting and illumination effects. However, a heavy lighting scenario will lead to heavier loading times, FPS bottlenecks, and more work for the CPU. Therefore, you are advised to play with ShadowsType and Baking properties to balance your scene performance.

For example, if you set the Baking to Realtime and the Shadow Type to Hard Shadows or Soft Shadows, you will have a very nice, realistic shadow effect between the Sphere and the Box. However, this setup can be heavy for the CPU if your scene is composed of many lights.

How to solve that issue? You can initially set the Bakingparameter to Bake.Once you do this, Unity will automatically create lightmaps for this light, giving you a performance boost. However, this performance boost has its cost: now you don’t have shadows being cast by the point light into dynamic objects, like the Sphere.

The best practice is to analyze each light individually, and then proceed accordingly. For example, in this scene it would make more sense to have Realtime Baking and Hard Shadows, because only one light source is present inside the box.

However, if your light source is a torch in a wall, it would be better to set Baking to Bake and let Unity produce the lightmaps.

Recall that it is up to the developer to manage the quality and performance of the game. Therefore, keep in mind that you have to be careful in keeping that balance, especially if you are developing for mobile devices.

Spot Light

Spot lights emit light from a light source targeting a specific region. They only illuminate objects within a specific region, a region delimited by a 3D cone. Basically, they work like the headlights of a car. As you may imagine, they are perfect for flashlights, car headlights, or lamp posts. They are also the most expensive light type for the graphics processor.

Let us now change the Point light into a spot light. Select the Point light you created earlier and change its Type to Spot. Next, Rotate the light so that it can illuminate the floor of the Box. Rename it to Spot light.

Lighting in Unity 5

As you may have noticed, inside the Inspector, the Spot parameters are similar to the Point ones. However, you have a new one called Spot Angle.

Lighting in Unity 5

The Spot Angle determines the angle of the light cone in degrees.

Regarding direct and indirect lighting, spot lights work exactly like point lights. You have the same limitations and advantages. Therefore, you have to be careful when setting up the balance of lights in your game.

Note that you can always play with shadows and baking in order to balance looks and performance.

Area Light

An area light is light is emitted in all directions on one side of a rectangular area of a plane. This rectangle is defined in bothWidth and Height properties. The area lights are only available during light map baking, which means that they have no effect on objects at runtime.

Select the Spot lightand change its Type to Area and its name to Area light. Next, position the light inside your box, and in the Inspector, change the Width and the Height, in order to cover the whole area inside the Box.

Lighting in Unity 5

By looking at the Area light parameters, you’ll notice that most of the parameters are similar to the previous ones, for example Type, Draw Halo, Flare, Render Mode, and Culling Mask.

The real new parameters are the Width and Height.Bothare used to set the size of the rectangular light area.

Lighting in Unity 5

If you press Play, you will see that the area light is casting its light on all objects within its range. The size of the rectangle is determined by the Width and Height properties. The side on which light is being cast is the plane’s normal, which is the same as the light’s positive Z direction. The light is emitted from the whole surface of the rectangle. Because of this, shading and shadows from the affected object tend to be much softer than with point or directional light sources.

The lighting calculations for an area light are quite processor-intensive, so they are not available at runtime and can only be Baked into light maps.

Conclusions

This concludes the tutorial about lighting in Unity 5. You learned about several lighting effects and configurations. With this knowledge, you can now apply several lighting effects to your game or application.

Unity has an active economy. There are many other products that help you build out your project. The nature of the platform also makes it a great option from which you can better your skills. Whatever the case, you can see everything we have available in the СodeHolder Marketplace.

If you have further questions or comments, as always, feel free to drop a line in the comments section.