[Unity] Part 5: Lighting, Reflections, and Game Optimization

Information
Step 1
In this lesson we'll get into Lighting, how to do reflections, and optimizing it so we don't get frame drops!
Step 2
Our first step is to put a very basic real-time light on the player, the same color as your bloom glow!
  • Same color as your bloom glow. A shortcut is to use the eyedropper tool and take the color from the material.

  • Also set the intensity to 2

  • If it's not set, set the Shadow type to Soft Shadows.

Check Your Work
Step 3
Click play and move your player around, it should now have a light that also casts shadows off from cubes.
Step 4
Let's change our environment a bit!

Try creating this T-shaped room with only duplicates of our Plane!

Hint: I got this all to match up perfectly, by always holding down CTRL when I rotate and move the planes around.

Check Your Work
Step 5
Play the game and your map layout should look similar to mines. We should be able to see though the backside of planes.
Step 6
Now we'll create some Quads, and click and drag any one of your Emission materials to it
Step 7
Let's also attach a light to this Quad, the same color as your Emission Material.

This time with Hard Shadows!

Challenge
Step 8
Resize the Quad and place them all around like this.
Check Your Work
Step 9
In game mode, we want to enable the Stats button and we'll be looking at the FPS (Frames per second)

Important to learn:

Now the FPS (Frames per second) will depend on what kind of hardware you have.

If you have a Gaming Computer then it should run fine but if it's not a gaming computer, then we may get less than 60FPS when we look at these lights and we get back to 60+ FPS when we look away.

Information
Step 10
So why do real time lights lower your frame rate?

If you think about it. Every time we move near a light or move the light near something.

We always check for shadows, we always have to update the color on our object to match the color of the light.

So this all done in Real time. So think of each light as a while() loop.

Step 11
So when a light isn't moving, we shouldn't use real time light, we can bake lights into a lightmap.

So let's select all those Quads, Right click the word Light, then Remove Component and then remove the real time light.

Step 12
Now to use baked lighting we first need to make everything that's not moving as static.

So first select all of your non-moveable objects which should be those emission quads and all your planes.

And then in the Inspector, on the top right there's a check box for checking them all as static

Step 13
We also want to see light in more detail so let's disable our Directional Light for now.
Step 14
Right now environment lighting is calculating real time, so we need to turn off Auto-Generate in Lighting settings.
Step 15
To get our light map we will now have to manually click on "Generate Lighting" every time we add more static objects.

Click on Generate Lighting, and wait for it to finish generating.

Step 16
We still don't see a difference yet, because our Emission isn't illuminating as Baked.

So while all Quads are still selected, change this option to Baked in the material settings.

And then click Generate Lighting again.

Check Your Work
Step 17
After you finished generating lighting, you should see something new under that button!

We generated a few light maps! It doesn't have to be 5, depending on what you have, it could be a different number.

Click Global Maps at the top to see what your lightmaps look like!

Check Your Work
Step 18
But what about our scene? Nothing looks different? Actually, try disabling all your quads.

There is still some light on the floor even when we removed the quads! This is because the lightmap saves that light cast into the lightmap and paints it onto all our static objects.

Challenge
Step 19
This lightmap seems too weak at emission 2.5 on our quads. So try emission 7, bake, then set back to 2.5
Check Your Work
Step 20
You did it! You optimized your lights by baking them into a lightmap!

Before we test to check our game. Let's learn using a new optimization tool. Go to Window > Profiler. And then click Play

This tool will come in handy when you have frame drops somewhere but don't know why. It can pinpoint what takes up most of the CPU such as UI, rendering, scripts, etc.

Step 21
Let's move onto reflections!

Turn the Directional Light back on, so we'll be able to see what we're doing with reflections.

Planning
Step 22
Let's add reflections to the Black material of our player!
Step 23
Select the player, and set the Metallic and smoothness from the black material to 1, which is the maximum.
Check Your Work
Step 24
You should notice there should be now reflections on your player.

However, what we are reflecting is only our skybox but now everything else around it.

Here we don't see the cube's glow in the surfaces of your player.

Information
Step 25
To do this, we need something called a Reflection Probe.

What is a Reflection Probe ?

A reflection probe is a location with a box range where everything in the box is reflected onto the reflection probe, which looks like a marble.

And then the reflection data from the reflection probe can be put on surfaces such as our player!

Step 26
If we want our player to reflect everything around it, then we need to place our reflection probe inside the player.

Make sure the Reflection probe is placed inside the Player and is at position (0,0,0)

Step 27
Next we want to change our Material settings, and the Reflection probe settings

On the player's Outside Material or the part where you want to be reflective. Set the Smoothness and Metallic to Maximum.

Then on the reflection probe, make sure it has these 3 settings:

Check Your Work
Step 28
Let's see if our player is reflective in Play mode!

You should now see that the colors match depending on where the ball is!

Challenge
Step 29
Can you do the same with a regular sphere?
Planning
Step 30
However, Since this updates every frame, it's very costly in performance. So for all other objects, we'll use Baked.
Step 31
To do this, we do the same, except used baked on another object.

Create a cube or any other object shape of your choice.

Just like our player, create another material for this cube and set Smoothness and Metallic to Maximum

After attaching a Reflection probe onto this cube. You can actually hit "Bake" on the Reflection probe. And this is faster because it won't calculate the lightmaps and only the reflection probe maps.

Check Your Work
Step 32
If you test right now, it's going to look weird. Because the cube is also using the player's same reflection probe!

Because our player is moving, the reflection on the cube is also moving. We cannot disable using the player's reflection probe in the cube, but we can separate by adding another reflection probe on the cube and change the weight/importance.

Planning
Step 33
What is Reflection Probe weight/importance?

When there is more than 1 reflection probe that are colliding the weights will tell which one to show, which one not to show, or how we want to blend between each reflection probe.

The two reflections probes will always add each other if they are colliding, so then how do we make them each their own separate probe?

Step 34
To do this we need to use an anchor override to [Lock] the reflection probe only to itself

So have each mesh renderer of the player and the cube, to grab their own reflection probe.

  1. Click and drag the Player's reflection probe into the Player's mesh renderer's anchor override.

  2. Click and drag the Cube's reflection probe into the Cubes's mesh renderer's anchor override.

Check Your Work
Step 35
It show now work when you Play your game.
  • The cube should not be a real time reflection, so it shouldn't move.
  • The player's reflections should be moving and matching the environment that its in.
Challenge
Step 36
Can you create a Baked Reflection Mirror?

Hints:
- The reflection probe for this is not attached to anything and is away from the plane/quad.
- The 3 colored objects in the mirror is actually still moveable on Playmode, so how is it baked on there if it's not in static at first?

Information
Step 37
You're finished with this lesson! Next we'll start with a brand new scene for an Introduction to HLSL programming!
[Unity] Part 5: Lighting, Reflections, and Game Optimization Info

Account

MVCode

Created By

tina nguyen

Course:

Epic Roll a Ball

Access Level

public

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach