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.

Prérequis

Avant de continuer, assurez-vous de bien comprendre les concepts fondamentaux de la RA et de savoir configurer une session ARCore.

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

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)

Configurer le mode ENVIRONMENTAL_HDR

Pour configurer le mode ENVIRONMENTAL_HDR, obtenez une estimation de la luminosité pour chaque image, puis obtenez les composants d'éclairage HDR d'environnement que vous souhaitez utiliser.

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

Configurer le mode AMBIENT_INTENSITY

Si vous prévoyez d'utiliser le composant de correction des couleurs de AMBIENT_INTENSITY , commencez par éviter d'appliquer la correction des couleurs à chaque image en réutilisant une allocation partagée.

Java

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

Kotlin

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

Obtenez une estimation de la luminosité pour chaque image, puis les composants de l'intensité ambiante que vous souhaitez utiliser.

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

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 jamais plus intense qu'avant qu'elle n'arrive à la surface. Cette règle est appliquée dans l'affichage physique, mais elle est généralement omise les pipelines de rendu utilisés dans les jeux vidéo et les applications mobiles.

Si vous utilisez un pipeline de rendu basé sur physiquement avec la technologie HDR environnemental pour une estimation de la luminosité ambiante, il vous suffit de vous assurer que des matériaux physiques sont utilisés des objets virtuels.

Si vous n'utilisez pas de pipeline basé sur le physique, vous disposez options:

  • La solution la plus idéale consiste à migrer vers un pipeline basé sur la physique.

  • Si cela n'est pas possible, vous pouvez toutefois contourner ce problème en multipliant valeur d'albédo à partir d'un matériau non physique par une fonction d'économie d'énergie un facteur. Cela permet de garantir au moins que le modèle d'ombrage BRDF peuvent être convertis en modèles Chaque BRDF a un facteur différent : par exemple, pour une réflexion diffuse, elle est de 1/Pi.