Illumina realisticamente gli oggetti virtuali in una scena

Scopri come usare Lighting Estimation nelle tue app.

Prerequisiti

Assicurati di aver compreso i concetti fondamentali di AR e di configurare una sessione ARCore prima di procedere.

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

Configura Lighting Estimation una volta per sessione per la modalità che vuoi utilizzare.

HDR ambientale

// Configure the session's lighting estimation mode for
// AR_LIGHT_ESTIMATION_MODE_ENVIRONMENTAL_HDR.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_ENVIRONMENTAL_HDR);
ArSession_configure(session, config);
ArConfig_destroy(config);

Intensità ambiente

// Configure the session's lighting estimation mode for
// AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY);
ArSession_configure(session, config);
ArConfig_destroy(config);

Disabilitata

// Disable the session's lighting estimation mode.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_DISABLED);
ArSession_configure(session, config);
ArConfig_destroy(config);

Utilizza i valori ottenuti da Lighting Estimation

Per utilizzare i valori ottenuti da Lighting Estimation, ottieni la stima della luce per ciascun fotogramma.

// Get the current frame.
ArFrame* ar_frame = NULL;
if (ArSession_update(session, ar_frame) != AR_SUCCESS) {
  LOGE("ArSession_update error");
  return;
}

// Get the light estimate for the current frame.
ArLightEstimate* ar_light_estimate = NULL;
ArLightEstimate_create(session, &ar_light_estimate);
ArFrame_getLightEstimate(session, ar_frame, ar_light_estimate);

ArLightEstimateState ar_light_estimate_state;
ArLightEstimate_getState(session, ar_light_estimate,
                         &ar_light_estimate_state);

// Check that the light estimate is valid before proceeding.
if (ar_light_estimate_state != AR_LIGHT_ESTIMATE_STATE_VALID) {
  LOGE("ArLightEstimateState is not valid.");
  ArLightEstimate_destroy(ar_light_estimate);
  return;
}

Quindi ottieni i componenti dell'illuminazione HDR ambientale per la configurazione corrente:

HDR ambientale

// Get intensity and direction of the main directional light from the current
// light estimate.
float direction[3];
ArLightEstimate_getEnvironmentalHdrMainLightDirection(
    session, ar_light_estimate, direction);

float intensity[3];
ArLightEstimate_getEnvironmentalHdrMainLightIntensity(
    session, ar_light_estimate, intensity);

// Get ambient lighting as spherical harmonics coefficients.
float ambient_spherical_harmonics[27];
ArLightEstimate_getEnvironmentalHdrAmbientSphericalHarmonics(
    session, ar_light_estimate, ambient_spherical_harmonics);

// Get HDR environmental lighting as a cubemap in linear color space.
ArImageCubemap cubemap_textures;
ArLightEstimate_acquireEnvironmentalHdrCubemap(session, ar_light_estimate,
                                               cubemap_textures);
int width = -1;
int height = -1;
int32_t format = -1;
for (int i = 0; i < 6; ++i) {
  ArImage* image_ptr = cubemap_textures[i];
  // We can access the cubemap texture data through ArImage APIs.
  ArImage_getWidth(session, image_ptr, &width);
  ArImage_getHeight(session, image_ptr, &height);
  ArImage_getFormat(session, image_ptr, &format);
  // Acquired image must be released with ArImage_release once it is no
  // longer needed.
  ArImage_release(image_ptr);
}
ArLightEstimate_destroy(ar_light_estimate);

Intensità ambiente

// Get the pixel intensity of AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY mode.
float pixel_intensity;
ArLightEstimate_getPixelIntensity(session, ar_light_estimate,
                                  &pixel_intensity);

// Get the pixel color correction of
// AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY mode.
float color_correction[4];
ArLightEstimate_getColorCorrection(session, ar_light_estimate,
                                   color_correction);
ArLightEstimate_destroy(ar_light_estimate);

Garantire il conservazione dell'energia con le API Environmental HDR

La conservazione dell'energia è il principio secondo cui la luce riflessa da una superficie non sarà mai più intensa di prima che colpisse la superficie. Questa regola viene applicata nel rendering basato sul fisico, ma di solito viene omessa dalle pipeline di rendering legacy utilizzate nei videogiochi e nelle app per dispositivi mobili.

Se utilizzi una pipeline di rendering basata fisicamente con la stima della luce HDR ambientale, assicurati semplicemente che negli oggetti virtuali vengano utilizzati materiali basati sul fisico.

Se non utilizzi una pipeline basata fisicamente, tuttavia, hai due opzioni:

  • La soluzione più ideale è la migrazione a una pipeline basata fisicamente.

  • Se ciò non è possibile, tuttavia, una buona soluzione alternativa consiste nel moltiplicare il valore di albedo da un materiale non fisico per un fattore di conservazione dell'energia. In questo modo è possibile convertire almeno il modello di ombreggiatura BRDF in formato fisico. Ogni BRDF ha un fattore diverso: ad esempio, per un riflesso diffuso è 1/Pi.