FREELessons: 13Length: 1.2 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Lighting Effects

Lighting makes everything more realistic. Good lighting is difficult, but ARKit gives you some tools for creating compelling lighting. I’ll show you how to use them in this lesson.

Related Links

2.5 Lighting Effects

Hi, and welcome back to Get Started With Augmented Reality for iOS. In this lesson I'm going to talk a little bit about lighting, and what ARKit has in store for you for that topic. Lighting is a very complex topic, and in ARKit, you can't just guess the lighting sources from the window view. Well, you can, but it only works with the face tracking configuration, which is only available on the iPhone X, which I don't have. What I've seen many developers do is to estimate that somewhere high above is a light. But that's not always true, of course, all we can do is make educated guesses. But that doesn't mean that we can't do anything with lighting. The ARKit framework gives us two important measurements to improve the situation. It automatically measures the ambient light intensity in the video, as well as the color temperature. We can use that to adjust our own lights. Let's jump into the code project. The first step is to give the cube a material. We can chose a lighting model for the material, and I'm not getting into the mathematics of the different models, but just choose physically based. But the lighting models still are using the different components of a material, like diffuse, spectrum, or metalness, and calculate a color perplex depending on the angles of light shining on the object. We can also add a diffuse color of orange, as well as locking the ambient and diffuse colors together. Then we can add the material to the queue. To dynamically change the lighting, we need an environment map, which is basically a sphere around our world, a texture that represents the surroundings. You can get them for free from, like this museum interior. To add it in the project, just drag it on the Assets file, name it appropriately, and then you are ready to use it. There is a lightingEnvironment property on the scene, which we can set to this environment image. Now, let's try it out. With the material and the environment, it already looks much better. What would be cool, though, is if I turn off the lights, that the cubes will also get darker, because there is less light. This means we need to enable lightEstimate. We can do that in the tracking configuration. This will add a lightEstimate property to the current frame. Let's add a new callback function to the view controller called granular updated time. This gets called very frequently, and we need that to adjust the light beam fluidly. I'm adding a guard clause that grabs the light estimate from the session's current frame. Because if there isn't any, you don't need to set the value. For a lighting environment, I can set the temperature. And while preparing this course, I couldn't find an obvious example to really show off the color temperature, the results are very subtle. The lightingEnvironment expects an intensity between 0 and 1. But the ambientIntensity that is returned has much higher values. You need to divide it by 1000, well, actually you don't need this line of code at all. The R sceneView is smart enough to automatically apply this value to your lighting environment. You just need to do it if you have custom lights. Now, when I run the scene again, the cubes adjust to changing lighting condition, either by turning the light on or off, or by simply putting the finger in front of the camera. To recap, lighting is also an important part of augmented reality, and a good application matches the lighting conditions of the environment. ARKit can estimate the color temperature and intensity of the ambient light of the camera. You can use environment maps to make your scenes look better, because they provide a nonuniform surrounding. ARKit automatically updates the lighting environment of the scene when estimation is enabled. In our next section, we are going to create a useful little example project, a measuring app, from start to finish. I'm going to give you a demonstration and overview in the next lesson, see you there.

Back to the top