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.

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 l'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 du mode AMBIENT_INTENSITY, évitez d'abord d'allouer 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 d'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 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.