Virtuelle Objekte in einer Szene realistisch beleuchten

Beleuchtungsschätzung in Ihren eigenen Apps verwenden

Voraussetzungen

Machen Sie sich mit den grundlegenden AR-Konzepten und zur Konfiguration einer ARCore-Sitzung vertraut, bevor Sie fortfahren.

Konfigurieren Sie die API einmal pro Sitzung mit dem entsprechenden Modus.

Konfigurieren Sie die Schätzung der Beleuchtung einmal pro Sitzung für den gewünschten Modus.

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)

ENVIRONMENTAL_HDR-Modus konfigurieren

Zum Konfigurieren des ENVIRONMENTAL_HDR-Modus rufen Sie die Lichtschätzung für jeden Frame ab und rufen dann die HDR-Beleuchtungskomponenten ab, die Sie verwenden möchten.

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

AMBIENT_INTENSITY-Modus konfigurieren

Wenn Sie die Farbkorrekturkomponente im AMBIENT_INTENSITY-Modus verwenden möchten, vermeiden Sie zuerst die Zuweisung der Farbkorrektur für jeden Frame, indem Sie eine gemeinsame Zuweisung wiederverwenden.

Java

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

Kotlin

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

Rufen Sie die Lichtschätzung für jeden Frame ab und ermitteln Sie dann die Komponenten der Umgebungsintensität, die Sie verwenden möchten.

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

Energieeinsparung mit Environmental HDR APIs

Bei der Energieeinsparung handelt es sich um das Prinzip, dass von einer Oberfläche reflektiertes Licht nie stärker sein wird als vor dem Auftreffen auf der Oberfläche. Diese Regel wird beim physikalischen Rendering erzwungen, wird jedoch in der Regel bei Legacy-Rendering-Pipelines, die in Videospielen und mobilen Apps verwendet werden, ausgelassen.

Wenn Sie eine physisch basierte Rendering-Pipeline mit Environmental HDR-Lichtschätzung verwenden, achten Sie darauf, dass in Ihren virtuellen Objekten physikalische Materialien verwendet werden.

Wenn Sie keine physisch basierte Pipeline verwenden, haben Sie jedoch mehrere Möglichkeiten:

  • Die beste Lösung hierfür ist die Migration zu einer physisch basierten Pipeline.

  • Wenn dies jedoch nicht möglich ist, können Sie das Problem umgehen, indem Sie den Albedo-Wert aus einem nicht physikalischen Material mit einem Energieerhaltungsfaktor multiplizieren. Dadurch kann zumindest das BRDF-Schattierungsmodell in physischbasierte konvertiert werden. Jede BRDF hat einen anderen Faktor, zum Beispiel ist er für eine diffuse Reflexion 1/Pi.