Virtuelle Objekte in einer Szene realistisch beleuchten

Weitere Informationen zur Verwendung der Lichtschätzung in Ihren eigenen Apps

Vorbereitung

Machen Sie sich mit den grundlegenden AR-Konzepten vertraut. und Konfigurieren einer ARCore-Sitzung beschrieben, bevor du fortfährst.

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

Konfigurieren Sie die Belichtungsschätzung 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

Wenn Sie den ENVIRONMENTAL_HDR-Modus konfigurieren möchten, rufen Sie die Lichtschätzung für jeden Frame ab und holen Sie sich dann die gewünschten HDR-Umgebungsbeleuchtungskomponenten.

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 du die Farbkorrekturkomponente von AMBIENT_INTENSITY verwenden möchtest Vermeiden Sie zunächst die Zuweisung einer Farbkorrektur auf jedem 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)

Du kannst die geschätzte Lichtquelle für jeden Frame und dann die Komponenten der Umgebungsintensität abrufen 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)
}

Mit Environmental HDR APIs für Energieeinsparung sorgen

Energieerhaltung ist das Prinzip, dass von einer Oberfläche reflektiertes Licht intensiver sein als vor dem Auftritt. Diese Regel ist wird im physikalischen Rendering erzwungen, wird jedoch bei Legacy-Versionen in der Regel weggelassen. Rendering-Pipelines, die in Videospielen und mobilen Apps verwendet werden.

Wenn Sie eine physisch basierte Rendering-Pipeline mit Umgebungs-HDR verwenden sollten Sie lediglich darauf achten, dass Sie physikalisch basierte Materialien verwenden, virtuelle Objekte.

Wenn Sie keine physikalische Pipeline verwenden, Optionen:

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

  • Sollte dies jedoch nicht möglich sein, können Sie das Problem umgehen, indem Sie Albedo-Wert eines nicht physisch basierenden Materials durch Energieeinsparung Faktor. Dadurch wird sichergestellt, dass zumindest das BRDF-Schattierungsmodell in physikalische Umgebungen umgewandelt werden kann. Jeder BRDF hat einen anderen Faktor: Bei einer diffusen Reflexion ist es beispielsweise 1/Pi.