Unity Cinematics Part 2: Real-time Lighting with Unity


Now that you have everything installed from part 1, we are ready to proceed in creating a simple cinematic using the Unity game engine.
I want to mention that this tutorial was inspired by a presentation by Mathieu Muller at Unite 2017 in Austin, Texas. I have attempted to capture the essence of Mathieu’s presentation and update the processes to version 2019.2 of Unity.

The first thing we need to take care of before going any further is that annoying error at the bottom of the screen. Currently, there is an ambiguity error in the Post Processing Package MinDrawer.cs file. This must be resolved before we can view (play) any of our work. Fortunately, it is an easy fix.
Note the following errors are for version 4007 and earlier of Adam Character Pack: Adam, Guard, Lu. Hopefully, these problems will soon be resolved in the Asset Store versions.

Click on the error showing on the bottom of the page or open the console tab to see the error.

Open your choice of code editor (I am using Visual Studio Code).
Open the file. It will be in the folder where your project is saved. In my case: E:\Unity Projects\DET-Animation\Assets\Adam Character Pack\PostProcessing\PostProcessing\Editor\PropertyDrawers\MinDrawer.cs

There are three occurrences of the variable MinAttribute in the file. By changing this variable name, we will resolve the error. I changed the name to MAttribute and added a using command to finalize the name change. The changes are bolded below (I have only included the relevant portions of the file):

using UnityEngine;
using UnityEngine.PostProcessing;
using MAttribute = UnityEngine.PostProcessing.MinAttribute;

namespace UnityEditor.PostProcessing {
    [CustomPropertyDrawer (typeof (MAttribute))]
    sealed class MinDrawer : PropertyDrawer {
        public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
            MAttribute attribute = (MAttribute) base.attribute; 

Once you resolve that error, there is another set of errors to resolve in the CurveEditor.cs file located at \Assets\Adam Character Pack\PostProcessing\PostProcessing\Editor\Utils\CurveEditor.cs
The CurveEditor has 3 obsolete errors.

Note: The obsolete errors did not occur when I was demonstrating this process on a Mac. The error only occurs within Windows versions of Unity.

The first obsolete command is EventType.repaint. It has been replaced with EventType.Repaint. Use the Find command of your editor to locate EventType.repaint; it is the second occurrence if you do not have case sensitivity on. The error was on line 379 of my file.

The second problem is EventType.keyDown. It has been replaced with EventType.KeyDown. The error was on line 585 of my file.

EventType.mouseDown has the same problem. It has been replaced with EventType.MouseDown. The error was on line 425, 456, and 502.

After you save the changes to CurveEditor.cs and MinDrawer.cs, give Unity a few minutes to recompile the files and resolve the next set of errors that popup.
Now we are ready to begin our project!

To begin, let’s clean up the Example project that loads with HDRP. We are going to start with a clean project and walk through the entire process.

Right-click on Example Assets in the Hierarchy panel and select Delete.

Repeat the process for Default Post-process, Scene Post-process, Rendering Settings, and Directional Light.
You should only have the Main Camera in the Hierarchy panel.

Now let’s swing the view around so that we are viewing the environment from the angle of the current camera. You can move around in the Scene by holding the right mouse button, then moving the mouse to change the angle of your view, or the AWSD keys to move within the environment. The center mouse button can be used to zoom.

We need a ground for our character to walk around on, so let’s add a plane. Using the menu, click on Game Object > 3D Object > Plane

Hmmm, it’s a little dark. Let’s get a light back in there! Click on GameObject Menu > Light > Point LIght

Let’s give the light the ability to cast shadows. With the Point Light selected in the Hierarchy, look in the Inspector panel and change Shadow Type to Soft Shadows

Note that you can also change the color of the light by clicking on the white space beside Color (my students really liked that feature).

Let’s add something for the light to bounce off of and make a shadow. Right-click in the Hierarchy panel and select 3D Object > Cube. Raise the Cube so that it is sitting on the plane that we added earlier using the move controller below the menu bar.

You should see a shadow on the far side of the cube.
Now, let’s add some color to the cube.

Right-click in the Project Assets panel, then select Create > Material

Then assign the New Material a name. I’m going to name mine Red.
In the Inspector panel, click on the white square beside Albedo (albedo is the reflected light of an object; i.e., it’s color or textured appearance) and select the red color from the color picker panel that opens.

Now that we have created a red material, we can apply it to the cube. Drag the red material from the Assets panel on to the cube.

We’ve got a great object, now we need to set up how the cube and other objects will handle lighting.
As of Unity 2019.2, lightmapping is handled a little differently than previous versions. Previously, if you set static lightmapping to on, Unity would pre-calculate all of the light for the scene’s static objects. This involved creating a lightmap of every static object including objects that were too small to worry about like wires or objects that were only a few pixels in size. This was expensive in processing.

As of Unity 2019, you have the ability to determine what objects should have lightmapping (by default, objects are NOT light-mapped).
Select the cube and plane in the hierarchy (hold the CTRL button as you click on each one). With both objects selected, in the Inspector under Mesh Render, make sure that Cast Shadows is On and that the Contribute Global Illumination is checked.
Receive Global Illumination should be set to Lightmaps. If the object was small and you didn’t want to use lightmaps, you could select the second object, Light Probes.

Checking the Contribute Global Illumination will open the Lightmapping options. For now, they are fine but remember that they are available. You might want to tweak them for a future project.

One last change while we have both the cube and plane selected: Currently both are trying to use Probes that do not exist, which creates some strange lighting. Change the Light Probes to Off.

As a part of this project, we want to bounce the light off of the cube. To accomplish this, adjust the Point Light Indirect Multiplier from 1 to 2.
(Select Point Light in the Hierarchy, then change Indirect Multiplier in the Inspector to 2). Also, change the Mode from Realtime to Mixed so that baked lighting shows better.

You should see an immediate increase in the reflectivity of the cube.
Here I have cranked the Indirect Multiplier to 11 (just in case you need to give it that extra boost):

To save on real-time computing, we can turn Real-time Global Illumination off. Select the Lighting tab. Under Realtime Lighting, uncheck the Realtime Global Illumination.
The last settings to change (at least for now) is in the Lightmapping Settings. If you have a good graphics card, change the Lightmapper to Progressive GPU (Preview). This will take advantage of that expensive graphics card in your computer and speed up baking the lightmaps.

Make sure the Auto Generate is checked at the bottom of the Lightmapping Settings. This will generate your lightmaps every time a change is made to the scene lighting. If you find it too time-consuming (i.e., your system is constantly baking the lighting with every change you make), you can turn this setting back off until you are ready to bake the lights into your scene.

For the next step, modify the cube so that it is more of a wall. Use the scale tool then move the cube to the far right of the plane.

Duplicate (CTRL-D) the cube twice and move the new cubes to the left of the original cube.

Let’s add some more interest. Create two new materials for the new cubes. I’m going to use Blue and Green. Then apply the new materials to the new cubes.
The advantage of duplicating the new cubes is that all of the previous settings and changes that we made to our first cube are carried on to the new cubes.

Time to add our actor/avatar/character!

In the Project panel, open the Adam Character Pack. Select Adam folder, then select Adam from the Assets.

Drag Adam into your scene. Rotate him so that his side is to the cubes and that he is beside the red cube.

Notice that Adam is already casting shadows on the cubes. This is due to configuring the Lighting and that we did earlier.

We now have everything fixed and configured. This process took approximately 45 minutes in class (I had to stop to help a few people and answer questions). In Mathieu’s video, he accomplished all of this in four and a half minutes (without the code debugging).
In Part 3, we will tweak the reflection lighting and then begin using the timeline to animate Adam and get some action happening.

If you are interested in seeing the development process, it is on YouTube:

Dr. Burton is a teacher and professor with over twenty-five years of experience, teaching game design, CGI, Information Technology, Computer Science, and MIS.

Recent Posts