Illumina realisticamente gli oggetti virtuali in una scena

Scopri come utilizzare la stima dell'illuminazione nelle tue app.

Prerequisiti

Assicurati di comprendere i concetti fondamentali della realtà aumentata e su come configurare una sessione ARCore prima di procedere.

Configura l'API una volta per sessione con la modalità appropriata

Configura la stima dell'illuminazione una volta per sessione per la modalità che vuoi utilizzare.

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)

Configura la modalità ENVIRONMENTAL_HDR

Per configurare la modalità ENVIRONMENTAL_HDR, ottieni la stima della luminosità per ogni frame: quindi scegli i componenti di illuminazione HDR ambientali che vuoi utilizzare.

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

Configurare la modalità AMBIENT_INTENSITY

Se prevedi di utilizzare il componente di correzione del colore della modalità AMBIENT_INTENSITY , evita innanzitutto l'allocazione della correzione del colore su ogni frame riutilizzando un'allocazione condivisa.

Java

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

Kotlin

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

Ottieni una stima della luce per ogni fotogramma, poi controlla i componenti dell'intensità ambientale che vuoi utilizzare.

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

Garantire il risparmio energetico con le API HDR ambientale

La conservazione dell'energia è il principio per cui la luce riflessa da una superficie non sarà mai più intensa di quanto non fosse prima di raggiungere la superficie. Questa regola è applicata al rendering su base fisica, ma di solito viene omessa dalla versione pipeline di rendering usate in videogiochi e app mobile.

Se utilizzi una pipeline di rendering su base fisica con Environmental HDR la stima della luce, assicurati semplicemente che vengano utilizzati materiali fisicamente presenti nella tua di oggetti virtuali.

Se, però, non utilizzi una pipeline basata fisicamente, hai un paio di opzioni:

  • La soluzione ideale è eseguire la migrazione a una pipeline fisica.

  • Se ciò non fosse possibile, una buona soluzione alternativa consiste nel moltiplicare valore di albedo proveniente da un materiale non basato fisicamente su un piano di conservazione dell'energia fattore. In questo modo, si garantisce che almeno il modello di ombreggiatura BRDF può essere convertito in modello fisico. Ogni BRDF ha un fattore diverso: Ad esempio, per una riflessione diffusa è 1/Pi.