Check out a free preview of the full Game Development with Unity for Web Developers course:
The "Lighting & Reflection" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick discusses how the lighting in Unity is intuitive by allowing the materials to access lighting data to determine an object's shading. Adding a reflection probe to capture a spherical view which is then used by shaders to generate convincing reflections is also covered in this segment.

Get Unlimited Access Now

Transcript from the "Lighting & Reflection" Lesson

[00:00:00]
>> So let's go back and talk a little bit about lighting very briefly, the lighting is already set up in our scene the way we want it. But I just wanted to touch on this really quick. Lights are a little bit more intuitive than materials, just like in the real world lights in Unity contribute to how surfaces are rendered.

[00:00:22] So the shaders on each material are able to access lighting data and make parts of a surface brighter or darker depending on the angle of the light to that pixel. And it also takes into account the parameters of each light source. So each light can have a shape, It has a position, there's the intensity of the light, the color and so forth.

[00:00:53] So if we just switch over to unity, there's nothing we actually need to do here, but if we look at this directional light and look at it in the scene view, You can see that I've got this light that's just Just kind of pointing in a direction. In this case, a directional light is just kind of a uniform light that gets applied to the same, It gets applied the same way to every part of the scene.

[00:01:26] So directional lights are really good for things like the sun. Light sources that are very large and very far away can be represented as a more simplistic directional light rather than using something like say a spotlight or another type of light that has kind of more directionality to it.

[00:01:52] There's also point lights, which are just a sphere of light that emit in all directions. So you can have some fun kind of playing around with the lighting here. One thing I will do I'll just put the scene view side by side with the game view here, and I'll use the rotate tool.

[00:02:10] You can rotate the light and you can see how that changes the lighting in the scene. So again, this is a little bit more intuitive the materials and you can actually see the shadows of the player and the opponent kind of moving across the game board there. So I'll hit command Z to undo that.

[00:02:30] I'll stack my game view and scene view again. And let's get back to the slides. So there's something else that we need to do with our lighting here. We want to create some reflections on the material and right now that isn't really present, we are kind of just reflecting the sky box, but there's more localized reflections, that we could benefit from.

[00:02:57] So in computer graphics, reflections are actually pretty costly, because you have to kind of render the scene twice. And then apply it to the thing that you want to be reflected. And then it gets even more complicated if you want say a reflection within a reflection, and so forth.

[00:03:18] That's why offline rendering and ray tracing gets these things looking so nice, whereas in real time environments like games, we have to rely on pre computed data. We can't calculate these reflections every frame I mean we can hardware is getting faster and faster but generally reflections are sampled beforehand and they are baked.

[00:03:41] So we wanna use what's called a reflection probe, and that captures a spherical view of its surroundings, and then any shaders in its area of influence can use the image data to generate convincing reflections. So this is pretty cool. I got a little bit of scene set up here.

[00:04:03] So we're going to add a reflection probe. And we're going to position it just a little bit above the game board at a y height of 1.5. And we're going to set a few other settings there mostly for performance reasons and quality reasons. So let's get back to Unity and in the hierarchy here we're going to create a light called a reflection probe.

[00:04:32] It's not really a light but it's under the light menu. And we can leave the default name for that, we can just leave it as reflection probe, and we're going to set that to a position of 0, 1.5 and 0. And then got the preview here. I don't need that just yet, but for the settings we want box projection, excuse me.

[00:05:01] We want box projection to be true. And then we want to set the resolution of a reflection a little bit higher, we'll set that to 512. And then for the shadow distance because this is a baked reflection probe, it's not being rendered in real time every frame. We can set the shadow distance to be really far.

[00:05:23] I'll just set it to a value of 1000. And now, I'll switch over to the game view so we can kind of see how this changes if at all. Now we'll hit the bake button on the reflection probe. So that should take just a second and it didn't really change a whole lot and the immediate view but if I go ahead and start the game, and allow these more metallic objects to drop in.

[00:05:57] You'll see that they do look a little bit different, they're using the influence of the reflection probe. I do wonder if I actually turn that on and off, if it will change much. Not really. But you can see some of the reflections being applied to there, so that's pretty cool.

[00:06:20] So reflection probes they allow you to kind of capture a lot more of the, I guess, realistic qualities that so many games are trying to chase. You know what, there is one thing I forgot I expected that to make a little bit of a bigger impact. We need to include some of these objects in the reflection.

[00:06:45] So in order to do that, we need to tell the reflection probe what objects aren't going to move, and what objects are not dynamic, so that it can include them in the reflections. So I didn't do that. There is the game board here. And then there's the ground here.

[00:07:05] So I just held down Shift to select both of those. And we need to set those game objects to be static. So in the inspector in the upper right, there's this static checkbox here. We're just going to check that. And now if we go back to our reflection probe, kind of open the game view here and hit bake to bake those static objects, that should make a bigger difference.

[00:07:36] Play, and there we go. Now we can see the game board and the ground actually reflected in that sphere there. So if I turn that on and off, that's the bigger difference that I was looking for. You can see how with it on, you can see the reflection of the world around it, looks a little bit more convincing.

[00:08:01] And again, that's not perfectly accurate, but it's enough to kind of sell the visual in a very computationally friendly way.