Luzes de objetos virtuais realistas em uma cena

Saiba como usar a Estimativa de iluminação nos seus próprios apps.

Pré-requisitos

Entenda os conceitos fundamentais de RA e saiba como configurar uma sessão do ARCore antes de continuar.

Configure a API uma vez por sessão com o modo adequado

Configure a estimativa de iluminação uma vez por sessão para o modo que você quer usar.

Java

// 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);

Kotlin

// 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)

Configurar o modo ENVIRONMENTAL_HDR

Para configurar o modo ENVIRONMENTAL_HDR, receba a estimativa de luz de cada frame e os componentes de iluminação HDR ambiental que você quer usar.

Java

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.
  }
}

Kotlin

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.
  }
}

Configurar o modo AMBIENT_INTENSITY

Se você planeja usar o componente de correção de cor do modo AMBIENT_INTENSITY, primeiro evite a alocação de correção de cor em todos os frames reutilizando uma alocação compartilhada.

Java

 // Avoid allocation on every frame.
float[] colorCorrection = new float[4];

Kotlin

val colorCorrection = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f)

Receba a estimativa de iluminação para cada frame e, em seguida, receba os componentes de intensidade ambiente que você quer usar.

Java

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);
}

Kotlin

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)
}

Garantir a conservação de energia com as APIs HDR ambientais

A conservação de energia é o princípio de que a luz refletida de uma superfície nunca será mais intensa do que era antes de atingir a superfície. Essa regra é aplicada na renderização baseada em física, mas geralmente é omitida dos pipelines de renderização legados usados em videogames e apps para dispositivos móveis.

Se você estiver usando um pipeline de renderização fisicamente baseado com estimativa de luz HDR ambiental, basta garantir que materiais fisicamente baseados sejam usados nos objetos virtuais.

No entanto, se você não estiver usando um pipeline baseado em física, terá algumas opções:

  • A solução mais adequada para isso é migrar para um pipeline baseado em física.

  • Se isso não for possível, uma boa solução alternativa é multiplicar o valor de albedo de um material não baseado na física por um fator de conservação de energia. Isso garante que pelo menos o modelo de sombreamento BRDF possa ser convertido em um modelo fisicamente baseado. Cada BRDF tem um fator diferente. Por exemplo, para uma reflexão difusa, é 1/Pi.