Estimating light direction

Google provides us with a robust solution for estimating the amount of light in an AR scene with ARCore. As we learned, light direction is an equally important part of scene lighting. Google didn't intentionally ignore estimating light direction with ARCore; it's just that that problem is really difficult to do right. However, Google did provide us with just enough tools in ARCore to be able to estimate light direction, providing some simple assumptions. First, we need to assume that our user, for now anyway, will remain in the same room or area. Second, our user will need to look in at least an 180 degree arc across their vision, or more simply put, the user just needs to look around. Third, it works best if the real-world environment is lit from a distant single bright source, such as the sun. Based on those assumptions, we can simply store the direction the user saw the brightest image in and use that to reverse calculate our light direction. This may sound more complex than it is, so hopefully, the following diagram can explain this further:

Calculating light direction from camera pixel intensity

Now, this technique may sound quite complicated, but it isn't. We can actually accomplish this with just a few lines of code. Open up Unity and follow along to write our directional light detector:

  1. Ensure that the HelloAR scene of the sample app is loaded.
  2. Select the Environmental Light object in the Hierarchy window.
  1. Click on the Gear icon beside the Environmental Light (Script) component in the Inspector window and select Edit Script from the context menu.
  2. Just beneath the class declaration, add the following lines to declare new variables:
public class EnvironmentalLight : MonoBehaviour
{ //after me
public GameObject SceneCamera;
public GameObject SceneLight;
private float maxGlobal = float.MinValue;
private Vector3 maxLightDirection;
  1. These variables will hold a reference to the scene camera, light, the max global intensity we find, and the direction we find it.
  2. Scroll down in the code until you see the identified line in the Update method, and add the following lines:
const float Inclination = 0.4f; //after me

var pi = Frame.LightEstimate.PixelIntensity;
if(pi > maxGlobal)
{
maxGlobal = pi;
SceneLight.transform.rotation = Quaternion.LookRotation(-SceneCamera.transform.forward);
}

  1. All this code does is use Frame.LightEstimate.PixelIntensity to read the light intensity for the current camera direction. Then, we check whether this value is higher than any previous seen value (maxGlobal). If it is, we set a new maximum value and rotate the light (SceneLight) in the opposite direction of the camera, which means that the light will face toward the camera.
Be careful when you edit code outside of the #if UNITY_EDITOR directive. This code won't be compiled until a build is run for the platform, which means that any errors in the code will be identified as build errors. This can be confusing, so be careful to avoid syntax errors when coding these sections.
  1. Save the file; that's all the code we need to write in order to adjust the light direction. If you recall from the last section, the diffuse shader we are using doesn't account for light direction. However, ARCore has provided us with another shader that does.
  1. Return to the editor to find and select the AndyMaterial in the Assets/GoogleARCore/HelloARExample/Materials/Andy folder.
  2. Change the material to use the ARCore/SpecularWithLightEstimation shader. This material shows the direction of light better.
  3. Select the Environmental Light object in the Hierarchy window. Note how we have two new properties added to the Environmental Light (Script) component. These new properties (Scene Camera and Scene Light) were added because we declared them as public fields in the class.
  4. Click on the icon that resembles a bullseye next to the Scene Camera property. Then, as shown in the following excerpt, select the First Person Camera object from the Select GameObject dialog:
 Setting the Scene Camera and Scene Light properties of the component
  1. Close the Select GameObject dialog.
  2. Repeat the same process for setting the Directional Light as the Scene Light.
  3. Connect your device and build and run. Run the app in an area with a single bright light source and see how Andy looks after you place it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.75