为虚拟对象添加逼真的光照

了解如何使用测光

前提条件

确保您了解 AR 基础概念 以及如何在继续之前配置 ARCore 现场录像

每会话使用适当模式配置一次 API

针对要使用的模式,在每次会话中配置一次光照估计。

JavaKotlin
// Configure the session with the Lighting Estimation API in ENVIRONMENTAL_HDR mode.
Config config = session.getConfig();
config
.setLightEstimationMode(LightEstimationMode.ENVIRONMENTAL_HDR);
session
.configure(config);

// Configure the session with the Lighting Estimation API in AMBIENT_INTENSITY mode.
Config config = session.getConfig();
config
.setLightEstimationMode(LightEstimationMode.AMBIENT_INTENSITY);
session
.configure(config);

// Configure the session with the Lighting Estimation API turned off.
Config config = session.getConfig();
config
.setLightEstimationMode(LightEstimationMode.DISABLED);
session
.configure(config);
// Configure the session with the Lighting Estimation API in ENVIRONMENTAL_HDR mode.
Config config = session.config
config
.lightEstimationMode = LightEstimationMode.ENVIRONMENTAL_HDR
session
.configure(config)

// Configure the session with the Lighting Estimation API in AMBIENT_INTENSITY mode.
Config config = session.config
config
.lightEstimationMode = LightEstimationMode.AMBIENT_INTENSITY
session
.configure(config)

// Configure the session with the Lighting Estimation API turned off.
Config config = session.config
config
.lightEstimationMode = LightEstimationMode.DISABLED
session
.configure(config)

配置ENVIRONMENTAL_HDR模式

如需配置 ENVIRONMENTAL_HDR 模式,请获取每一帧的光照估算量, 然后获取想要使用的环境 HDR 照明组件。

JavaKotlin
void update() {
 
// Get the current frame.
 
Frame frame = session.update();

 
// Get the light estimate for the current frame.
 
LightEstimate lightEstimate = frame.getLightEstimate();

 
// Get intensity and direction of the main directional light from the current light estimate.
 
float[] intensity = lightEstimate.getEnvironmentalHdrMainLightIntensity(); // note - currently only out param.
 
float[] direction = lightEstimate.getEnvironmentalHdrMainLightDirection();
  app
.setDirectionalLightValues(intensity, direction); // app-specific code.

 
// Get ambient lighting as spherical harmonics coefficients.
 
float[] harmonics = lightEstimate.getEnvironmentalHdrAmbientSphericalHarmonics();
  app
.setAmbientSphericalHarmonicsLightValues(harmonics); // app-specific code.

 
// Get HDR environmental lighting as a cubemap in linear color space.
 
Image[] lightmaps = lightEstimate.acquireEnvironmentalHdrCubeMap();
 
for (int i = 0; i < lightmaps.length /*should be 6*/; ++i) {
    app
.uploadToTexture(i, lightmaps[i]);  // app-specific code.
 
}
}
fun update() {
 
// Get the current frame.
 
val frame = session.update()

 
// Get the light estimate for the current frame.
 
val lightEstimate = frame.lightEstimate

 
// Get intensity and direction of the main directional light from the current light estimate.
 
val intensity = lightEstimate.environmentalHdrMainLightIntensity
 
val direction = lightEstimate.environmentalHdrMainLightDirection
  app
.setDirectionalLightValues(intensity, direction) // app-specific code.

 
// Get ambient lighting as spherical harmonics coefficients.
 
val harmonics = lightEstimate.environmentalHdrAmbientSphericalHarmonics
  app
.ambientSphericalHarmonicsLightValues = harmonics // app-specific code.

 
// Get HDR environmental lighting as a cubemap in linear color space.
 
val lightMaps = lightEstimate.acquireEnvironmentalHdrCubeMap();
 
for ((index, lightMap) in lightMaps.withIndex()) { // 6 maps total.
    app
.uploadToTexture(index, lightMap); // app-specific code.
 
}
}

配置 AMBIENT_INTENSITY 模式

如果您打算使用 AMBIENT_INTENSITY 模式的色彩校正组件,请先通过重复使用共享分配来避免在每个帧上分配色彩校正。

JavaKotlin
 // Avoid allocation on every frame.
float[] colorCorrection = new float[4];
val colorCorrection = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f)

获取每一帧的光照估值,然后获取环境强度分量 资源。

JavaKotlin
void update() {
 
// Get the current frame.
 
Frame frame = session.update();

 
// Get the light estimate for the current frame.
 
LightEstimate lightEstimate = frame.getLightEstimate();

 
// Get the pixel intensity of AMBIENT_INTENSITY mode.
 
float pixelIntensity = lightEstimate.getPixelIntensity();

 
// Read the pixel color correction of AMBIENT_INTENSITY mode into colorCorrection.
  lightEstimate
.getColorCorrection(colorCorrection, 0);
}
fun update() {
   
// Get the current frame.
 
val frame = session.update()

 
// Get the light estimate for the current frame.
 
val lightEstimate = frame.lightEstimate

 
// Get the pixel intensity of AMBIENT_INTENSITY mode.
 
val pixelIntensity = lightEstimate.pixelIntensity

 
// Read the pixel color correction of AMBIENT_INTENSITY mode into colorCorrection.
  lightEstimate
.getColorCorrection(colorCorrection, 0)
}

通过 Environmental HDR API 确保节能

节能原则是指从物体表面反射的光线 它的强度从未像表面一样。此规则 在基于物理的渲染中会强制执行,但旧版中通常会省略 视频游戏和移动应用中使用的渲染管道

如果您将基于物理的渲染管道与 Environmental HDR 搭配使用 只需确保所用的材料是基于物理材料 虚拟对象。

但是,如果您不使用基于物理的流水线 选项:

  • 对此,最好的解决方案是迁移到基于物理的流水线。

  • 不过,如果无法做到这一点,一个不错的权宜解决方法是将非基于物理的材质的反射率值乘以能量守恒系数。这样至少可以确保 BRDF 着色模型 可以转换为物理存储。每个 BRDF 都有一个不同的因素 -- 例如,对于漫射反射,其值为 1/Pi。