Découvrez comment utiliser Lighting Estimation dans vos propres applications.
Prérequis
Avant de continuer, assurez-vous de bien comprendre les concepts fondamentaux de la RA et de savoir comment configurer une session ARCore.
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 frame, puis obtenez les composants d'éclairage HDR de l'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 l'allocation de la correction des couleurs sur chaque frame 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 l'estimation de la luminosité pour chaque frame, puis obtenez 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 l'économie d'énergie avec les API HDR environnementales
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 dans le rendu basé sur la physique, mais elle 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 basé sur la physique avec l'estimation de la lumière HDR environnementale, assurez-vous simplement que des matériaux basés sur la physique sont utilisés dans vos objets virtuels.
Si vous n'utilisez pas de pipeline basé sur la physique, vous avez deux options :
La solution idéale consiste à migrer vers un pipeline basé sur la physique.
Si ce n'est pas possible, une bonne solution de contournement consiste à multiplier la valeur d'albédo d'un matériau non basé sur la physique par un facteur de conservation de l'énergie. Cela permet de s'assurer qu'au moins le modèle d'ombrage BRDF peut être converti en modèle basé sur la physique. Chaque BRDF a un facteur différent (par exemple, pour une réflexion diffuse, il s'agit de 1/Pi).