Project 3: Barrel Bouncer AR Edition
Part 6: Adjusting for Environmental Lighting
As we discussed in class (and as you saw in the AR Best Design Practices Activity), it is important to consider the lighting attributes of the real-world environment when creating an AR app as it makes the virtual objects fit-in better and feel more realistic. This is because the simulated lighting would hit them in the same way real lighting would, and this would also provide shadows in the correct direction.
The underlying ARCore and ARKit platforms can analyze each individual image that is captured by the device in order to predict properties of the lighting in the real world. AR Foundation provides access to much of this light estimation data if available, including values for brightness, color temperature, and color correction, which you can then use to manipulate properties of the Light component on the Directional Light GameObject in your scene. It also gives you the main direction that the light is facing. This means that you can appropriately set the Directional Light’s Transform’s rotations to match this. Note that you do not need to adjust the position of the directional light, since directional light originates from the skybox anyway (and thus its position does not affect any lighting-related calculations).
For this subsection, you will use a script to gather the four aforementioned lighting properties (brightness, color temperature, color correction, and direction) and then apply them to the light in your scene each time your phone’s camera receives new data. As part of the unitypackage, we have already provided you with a starter script called Light Estimation. It contains a function called ChangeLighting
which is subscribed to the camera manager’s frameReceived
event. It is inside this function where all of your code for modifying the Directional Light’s properties must go (i.e. you cannot add any helper methods or outside fields). Before you do so, complete the following setup:
- On your AR Camera GameObject, find the AR Camera Manager component and change its Light Estimation setting to Everything.
- Add the Light Estimation script onto your Directional Light GameObject.
- In the slot for the Camera Manager property, drag in the reference to your AR Camera’s AR Camera Manager component.
- In the slot for the Light property, drag in the reference to your Direction Light’s Light component.
- Open up the script in your favorite code editor and write up your implementation for the
ChangeLighting
method. Make sure to check if each piece of data is actually available before actually assigning it to the Light’s properties. Additionally, your light should take on the values from the main light that is detected, if possible.
Note that you may find the documentation for ARCameraFrameEventArgs, ARLightEstimationData, and Light helpful for this part of the project, as well as the Lighting Estimation scene from the AR Foundation samples that we described in Part 0.