عناصر افتراضية إضاءة واقعية في المشهد

تعرَّف على كيفية استخدام تقدير الإضاءة في تطبيقاتك.

المتطلبات الأساسية

قبل المتابعة، تأكَّد من فهم مفاهيم الواقع المعزّز الأساسية وكيفية ضبط جلسة ARCore.

ضبط واجهة برمجة التطبيقات مرة واحدة لكل جلسة باستخدام الوضع المناسب

اضبط ميزة "تقدير الإضاءة" مرة واحدة لكل جلسة للوضع الذي تريد استخدامه.

جافا

// 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

لضبط وضع ENVIRONMENTAL_HDR، احصل على تقدير حالة الضوء لكل إطار، ثم احصل على مكوّنات إضاءة النطاق العالي الديناميكية (HDR) للبيئة المحيطة التي تريد استخدامها.

جافا

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

إذا كنت تخطّط لاستخدام مكوّن تصحيح الألوان في AMBIENT_INTENSITY وضع، تجنَّب أولاً تخصيص تصحيح الألوان في كل إطار من خلال إعادة استخدام تخصيص مشترك.

جافا

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

Kotlin

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

احصل على تقدير الإضاءة لكل إطار، ثم احصل على مكوّنات شدة الإضاءة المحيطة التي تريد استخدامها.

جافا

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

ضمان الحفاظ على الطاقة باستخدام واجهات برمجة التطبيقات Environmental HDR

حفظ الطاقة هو مبدأ ينص على أنّ الضوء المنعكس من سطح ما لن يكون أبدًا أكثر سطوعًا من الضوء قبل أن يصطدم بالسطح. يتم تطبيق هذه القاعدة في العرض المستند إلى الفيزياء، ولكن يتم عادةً حذفها من مسارات العرض القديمة المستخدَمة في ألعاب الفيديو وتطبيقات الأجهزة الجوّالة.

إذا كنت تستخدم مسار عرض مستندًا إلى الفيزياء مع تقدير الإضاءة في نطاق عالي الديناميكية البيئي، ما عليك سوى التأكّد من استخدام مواد مستندة إلى الفيزياء في العناصر الافتراضية.

إذا كنت لا تستخدم مسار عرض يستند إلى الفيزياء، لديك خياران:

  • الحلّ الأنسب لهذه المشكلة هو الانتقال إلى مسار معالجة يستند إلى الفيزياء.

  • إذا لم يكن ذلك ممكنًا، يمكنك بدلاً من ذلك ضرب قيمة اللون الأساسي من مادة غير مستندة إلى الفيزياء في عامل الحفاظ على الطاقة. يمكن أن يضمن ذلك إمكانية تحويل نموذج التظليل BRDF على الأقل إلى نموذج يستند إلى الفيزياء. لكل دالة BRDF عامل مختلف، على سبيل المثال، بالنسبة إلى الانعكاس المنتشر، يكون العامل 1/Pi.