Objets virtuels lumineux de manière réaliste dans une scène

Découvrez comment utiliser l'estimation de l'éclairage dans vos propres applications.

Conditions préalables

Assurez-vous de bien comprendre les concepts fondamentaux de la RA et de configurer une session ARCore avant de continuer.

Configurer l'API une fois par session avec le mode approprié

Configurez l'estimation de l'éclairage une fois par session pour le mode que vous souhaitez utiliser.

HDR de l'environnement

// 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é ambiante

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

Désactivé

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

Utiliser les valeurs obtenues à partir de l'estimation de l'éclairage

Pour utiliser les valeurs obtenues à partir de l'estimation de l'éclairage, obtenez l'estimation de la luminosité pour chaque image.

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

Récupérez ensuite les composants d'éclairage HDR de l'environnement pour la configuration actuelle:

HDR de l'environnement

// 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é ambiante

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

Assurer la conservation de l'énergie avec les API Environmental HDR

La conservation de l'énergie est le principe selon lequel la lumière réfléchie par une surface ne sera jamais plus intense qu'avant d'atteindre la surface. Cette règle est appliquée pour le rendu physique, mais est généralement omise des anciens pipelines de rendu utilisés dans les jeux vidéo et les applications mobiles.

Si vous utilisez un pipeline de rendu physique avec une estimation de la luminosité HDR environnementale, assurez-vous simplement que des matériaux physiquement utilisés sont utilisés dans vos objets virtuels.

Toutefois, si vous n'utilisez pas de pipeline basé physiquement, plusieurs options s'offrent à vous:

  • La solution idéale pour cela consiste à migrer vers un pipeline basé physiquement.

  • Si cela n'est pas possible, cependant, une bonne solution consiste à multiplier la valeur de l'albédo à partir d'un matériau non physique par un facteur de conservation de l'énergie. Cela garantit qu'au moins le modèle d'ombrage BRDF peut être converti en modèle d'ombrage physique. Chaque BRDF possède un facteur différent. Par exemple, pour une réflexion diffuse, la valeur est de 1/Pi.