نور واقعی اشیاء مجازی در یک صحنه

نحوه استفاده از Lighting Estimation را در برنامه های خود بیاموزید.

پیش نیازها

قبل از ادامه، مطمئن شوید که مفاهیم اساسی AR و نحوه پیکربندی یک جلسه ARCore را درک کرده اید.

API را یک بار در هر جلسه با حالت مناسب پیکربندی کنید

تخمین روشنایی را یک بار در هر جلسه برای حالتی که می خواهید استفاده کنید پیکربندی کنید.

HDR محیطی

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

از مقادیر بدست آمده از Lighting Estimation استفاده کنید

برای استفاده از مقادیر بدست آمده از Lighting Estimation، تخمین نور را برای هر فریم بدست آورید.

// 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 محیطی را برای پیکربندی فعلی دریافت کنید:

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

اطمینان از صرفه جویی در انرژی با API های محیطی HDR

حفظ انرژی این اصل است که نور منعکس شده از یک سطح هرگز شدیدتر از قبل از برخورد با سطح نخواهد بود. این قانون در رندرهای مبتنی بر فیزیکی اعمال می‌شود، اما معمولاً از خطوط لوله رندر قدیمی مورد استفاده در بازی‌های ویدیویی و برنامه‌های تلفن همراه حذف می‌شود.

اگر از یک خط لوله رندر مبتنی بر فیزیکی با برآورد نور محیطی HDR استفاده می کنید، به سادگی اطمینان حاصل کنید که از مواد مبتنی بر فیزیکی در اشیاء مجازی شما استفاده شده است.

با این حال، اگر از یک خط لوله مبتنی بر فیزیکی استفاده نمی کنید، چند گزینه دارید:

  • ایده آل ترین راه حل برای این کار مهاجرت به یک خط لوله مبتنی بر فیزیکی است.

  • با این حال، اگر این امکان پذیر نیست، یک راه حل خوب این است که مقدار albedo از یک ماده غیر فیزیکی را در یک عامل حفظ انرژی ضرب کنید. این می تواند مطمئن شود که حداقل مدل سایه زنی BRDF می تواند به مبتنی بر فیزیکی تبدیل شود. هر BRDF فاکتور متفاوتی دارد - برای مثال، برای بازتاب پراکنده 1/Pi است.