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

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

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

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

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

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

نطاق عالي الديناميكية بيئي

// Configure the session's lighting estimation mode for
// AR_LIGHT_ESTIMATION_MODE_ENVIRONMENTAL_HDR.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_ENVIRONMENTAL_HDR);
ArSession_configure(session, config);
ArConfig_destroy(config);

كثافة المحيط

// Configure the session's lighting estimation mode for
// AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY);
ArSession_configure(session, config);
ArConfig_destroy(config);

غير مفعَّل

// Disable the session's lighting estimation mode.
ArConfig* config = NULL;
ArConfig_create(session, &config);
ArSession_getConfig(session, config);
ArConfig_setLightEstimationMode(session, config,
                                AR_LIGHT_ESTIMATION_MODE_DISABLED);
ArSession_configure(session, config);
ArConfig_destroy(config);

استخدام القيم التي تم الحصول عليها من "تقدير الإضاءة"

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

// Get the current frame.
ArFrame* ar_frame = NULL;
if (ArSession_update(session, ar_frame) != AR_SUCCESS) {
  LOGE("ArSession_update error");
  return;
}

// Get the light estimate for the current frame.
ArLightEstimate* ar_light_estimate = NULL;
ArLightEstimate_create(session, &ar_light_estimate);
ArFrame_getLightEstimate(session, ar_frame, ar_light_estimate);

ArLightEstimateState ar_light_estimate_state;
ArLightEstimate_getState(session, ar_light_estimate,
                         &ar_light_estimate_state);

// Check that the light estimate is valid before proceeding.
if (ar_light_estimate_state != AR_LIGHT_ESTIMATE_STATE_VALID) {
  LOGE("ArLightEstimateState is not valid.");
  ArLightEstimate_destroy(ar_light_estimate);
  return;
}

بعد ذلك، عليك الحصول على مكوّنات إضاءة النطاق العالي الديناميكية (HDR) البيئية للإعدادات الحالية:

نطاق عالي الديناميكية بيئي

// Get intensity and direction of the main directional light from the current
// light estimate.
float direction[3];
ArLightEstimate_getEnvironmentalHdrMainLightDirection(
    session, ar_light_estimate, direction);

float intensity[3];
ArLightEstimate_getEnvironmentalHdrMainLightIntensity(
    session, ar_light_estimate, intensity);

// Get ambient lighting as spherical harmonics coefficients.
float ambient_spherical_harmonics[27];
ArLightEstimate_getEnvironmentalHdrAmbientSphericalHarmonics(
    session, ar_light_estimate, ambient_spherical_harmonics);

// Get HDR environmental lighting as a cubemap in linear color space.
ArImageCubemap cubemap_textures;
ArLightEstimate_acquireEnvironmentalHdrCubemap(session, ar_light_estimate,
                                               cubemap_textures);
int width = -1;
int height = -1;
int32_t format = -1;
for (int i = 0; i < 6; ++i) {
  ArImage* image_ptr = cubemap_textures[i];
  // We can access the cubemap texture data through ArImage APIs.
  ArImage_getWidth(session, image_ptr, &width);
  ArImage_getHeight(session, image_ptr, &height);
  ArImage_getFormat(session, image_ptr, &format);
  // Acquired image must be released with ArImage_release once it is no
  // longer needed.
  ArImage_release(image_ptr);
}
ArLightEstimate_destroy(ar_light_estimate);

كثافة المحيط

// Get the pixel intensity of AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY mode.
float pixel_intensity;
ArLightEstimate_getPixelIntensity(session, ar_light_estimate,
                                  &pixel_intensity);

// Get the pixel color correction of
// AR_LIGHT_ESTIMATION_MODE_AMBIENT_INTENSITY mode.
float color_correction[4];
ArLightEstimate_getColorCorrection(session, ar_light_estimate,
                                   color_correction);
ArLightEstimate_destroy(ar_light_estimate);

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

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

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

ولكن إذا كنت لا تستخدم مسارًا قائمًا ماديًا، فلديك خياران:

  • والحل الأمثل لذلك هو التحوّل إلى خط أنابيب قائم على وجود مادي.

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