Menerangi objek virtual secara realistis dalam adegan

Pelajari cara menggunakan Estimasi Pencahayaan di aplikasi Anda sendiri.

Prasyarat

Pastikan Anda memahami konsep dasar AR dan cara mengonfigurasi sesi ARCore sebelum melanjutkan.

Mengonfigurasi API sekali per sesi dengan mode yang sesuai

Konfigurasikan Estimasi Pencahayaan sekali per sesi untuk mode yang ingin Anda gunakan.

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)

Konfigurasi mode ENVIRONMENTAL_HDR

Untuk mengonfigurasi mode ENVIRONMENTAL_HDR, dapatkan perkiraan cahaya untuk setiap bingkai, lalu dapatkan komponen pencahayaan HDR lingkungan yang ingin Anda gunakan.

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

Konfigurasi mode AMBIENT_INTENSITY

Jika Anda berencana menggunakan komponen koreksi warna AMBIENT_INTENSITY , pertama-tama hindari alokasi koreksi warna pada setiap bingkai dengan menggunakan kembali alokasi bersama.

Java

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

Kotlin

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

Dapatkan perkiraan cahaya untuk setiap bingkai, lalu dapatkan komponen intensitas sekitar yang ingin Anda gunakan.

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

Memastikan konservasi energi dengan Environmental HDR API

Konservasi energi adalah prinsip bahwa cahaya yang dipantulkan dari permukaan akan tidak pernah lebih intens dari sebelum mencapai permukaan. Aturan ini diterapkan dalam rendering berbasis fisik, tetapi biasanya dihilangkan dari versi lama pipeline rendering yang digunakan dalam video {i>game<i} dan aplikasi seluler.

Jika Anda menggunakan pipeline rendering berbasis fisik dengan Environmental HDR perkiraan cahaya, cukup pastikan bahan berbasis fisik digunakan dalam objek virtual.

Namun, jika tidak menggunakan pipeline berbasis fisik, Anda memiliki beberapa opsi:

  • Solusi yang paling ideal untuk hal ini adalah bermigrasi ke pipeline berbasis fisik.

  • Namun, jika tidak memungkinkan, solusi yang baik adalah mengalikan nilai albedo dari material non-fisik dengan faktor penghematan energi. Hal ini dapat memastikan setidaknya model shading BRDF dapat dikonversi menjadi berbasis fisik. Setiap BRDF memiliki faktor yang berbeda -- misalnya, untuk refleksi difusi, nilainya adalah 1/Pi.