Informationen zur Verwendung der Beleuchtungsschätzung in Ihren eigenen Apps.
Vorbereitung
Sie müssen die grundlegenden AR-Konzepte und die Konfiguration einer ARCore-Sitzung verstehen, bevor Sie fortfahren.
API einmal pro Sitzung mit dem entsprechenden Modus konfigurieren
Konfigurieren Sie die Beleuchtungsschä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)
Modus ENVIRONMENTAL_HDR konfigurieren
Um den Modus ENVIRONMENTAL_HDR zu konfigurieren, rufen Sie die Lichtschätzung für jeden Frame ab und rufen Sie dann die HDR-Beleuchtungskomponenten für die Umgebung 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.
}
}
Modus AMBIENT_INTENSITY konfigurieren
Wenn Sie die Farbkorrekturkomponente des Modus AMBIENT_INTENSITY verwenden möchten, vermeiden Sie zuerst die Zuweisung der Farbkorrektur für jeden Frame, indem Sie eine freigegebene 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 rufen Sie dann die Komponenten für die Umgebungsintensität 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 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 sicherstellen
Die Energieeinsparung ist das Prinzip, dass das von einer Oberfläche reflektierte Licht niemals intensiver sein wird als vor dem Auftreffen auf die Oberfläche. Diese Regel wird beim physikalisch basierten Rendering erzwungen, wird aber in der Regel in älteren Rendering-Pipelines, die in Videospielen und mobilen Apps verwendet werden, weggelassen.
Wenn Sie eine physikalisch basierte Rendering-Pipeline mit Environmental HDR-Lichtschätzung verwenden, müssen Sie lediglich darauf achten, dass in Ihren virtuellen Objekten physikalisch basierte Materialien verwendet werden.
Wenn Sie jedoch keine physikalisch basierte Pipeline verwenden, haben Sie mehrere Möglichkeiten:
Die beste Lösung ist, zu einer physikalisch basierten Pipeline zu migrieren.
Wenn das nicht möglich ist, können Sie den Albedo-Wert eines nicht physikalisch basierten Materials mit einem Energieeinsparungsfaktor multiplizieren. So kann zumindest das BRDF-Shading-Modell in ein physikalisch basiertes Modell umgewandelt werden. Jede BRDF hat einen anderen Faktor. Bei einer diffusen Reflexion ist er beispielsweise 1/Pi.