Bir sahnedeki sanal nesneleri gerçekçi bir şekilde ışıklandırın

Işık Tahmini'ni kendi uygulamalarınızda nasıl kullanacağınızı öğrenin.

Ön koşullar

Devam etmeden önce temel AR kavramlarını ve ARCore oturumunu nasıl yapılandıracağınızı anladığınızdan emin olun.

API'yi oturum başına bir kez uygun modda yapılandırın.

Kullanmak istediğiniz mod için Işıklandırma Tahmini'ni oturum başına bir kez yapılandırın.

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 modunu yapılandırma

ENVIRONMENTAL_HDR modunu yapılandırmak için her karenin ışık tahminini alın, ardından kullanmak istediğiniz çevresel HDR ışık bileşenlerini alın.

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 modunu yapılandırma

AMBIENT_INTENSITY modunun renk düzeltme bileşenini kullanmayı planlıyorsanız önce paylaşılan bir tahsisi yeniden kullanarak her karede renk düzeltme tahsisinden kaçının.

Java

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

Kotlin

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

Her kare için ışık tahminini alın, ardından kullanmak istediğiniz ortam yoğunluğu bileşenlerini alın.

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

Çevresel HDR API'leriyle enerji tasarrufu sağlama

Enerji korunumu, bir yüzeyden yansıyan ışığın, yüzeye çarpmadan önceki halinden asla daha yoğun olmayacağı ilkesidir. Bu kural, fiziksel tabanlı oluşturmada zorunlu kılınır ancak genellikle video oyunlarında ve mobil uygulamalarda kullanılan eski oluşturma işlem hatlarında atlanır.

Çevresel HDR ışık tahmini ile fiziksel tabanlı bir oluşturma ardışık düzeni kullanıyorsanız sanal nesnelerinizde fiziksel tabanlı malzemeler kullanıldığından emin olmanız yeterlidir.

Ancak fiziksel temelli bir ardışık düzen kullanmıyorsanız birkaç seçeneğiniz vardır:

  • Bunun için en ideal çözüm, fiziksel tabanlı bir ardışık düzene geçmektir.

  • Ancak bu mümkün değilse fiziksel temelli olmayan bir malzemenin albedo değerini enerji koruma faktörüyle çarpmak iyi bir geçici çözümdür. Bu, en azından BRDF gölgelendirme modelinin fiziksel tabanlı hale dönüştürülmesini sağlayabilir. Her BRDF'nin farklı bir faktörü vardır. Örneğin, dağınık yansıma için bu faktör 1/Pi'dir.