ให้แสงวัตถุเสมือนจริงในฉากอย่างสมจริง

เรียนรู้วิธีใช้การประมาณแสงในแอปของคุณเอง

ข้อกำหนดเบื้องต้น

ตรวจสอบว่าคุณเข้าใจแนวคิด AR พื้นฐาน และวิธีกำหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ

กำหนดค่า API 1 ครั้งต่อเซสชันด้วยโหมดที่เหมาะสม

กำหนดค่าการประมาณการให้แสง 1 ครั้งต่อเซสชันสำหรับโหมดที่ต้องการใช้

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

ใช้ค่าที่ได้รับจากการประมาณการให้แสงสว่าง

หากต้องการใช้ค่าที่ได้จากการประมาณแสง ให้ดูค่าประมาณแสงสำหรับแต่ละเฟรม

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

รับประกันการอนุรักษ์พลังงานด้วย Environmental HDR API

การอนุรักษ์พลังงานคือหลักการที่ว่าแสงที่สะท้อนจากพื้นผิวจะไม่รุนแรงมากไปกว่าตอนก่อนที่จะกระทบพื้นผิว กฎนี้บังคับใช้ในการแสดงผลที่อิงตามกายภาพ แต่ปกติแล้วจะไม่รวมอยู่ในไปป์ไลน์การแสดงผลแบบเดิมที่ใช้ในวิดีโอเกมและแอปบนอุปกรณ์เคลื่อนที่

หากคุณใช้ไปป์ไลน์การแสดงภาพตามตัวจริงที่มีการประมาณแสง HDR สภาพแวดล้อม ให้ตรวจดูว่ามีการใช้วัสดุที่อิงทางกายภาพในวัตถุเสมือนของคุณ

อย่างไรก็ตาม หากไม่ได้ใช้ไปป์ไลน์ที่อิงตามตัวจริง คุณมี 2 ตัวเลือกดังนี้

  • วิธีแก้ปัญหาที่ดีที่สุดสําหรับกรณีนี้คือการย้ายข้อมูลไปยังไปป์ไลน์แบบอิงข้อมูลจริง

  • แต่ถ้าทำไม่ได้ วิธีแก้ปัญหาเบื้องต้นที่ดีคือการคูณค่า Albedo จากวัสดุที่ไม่ได้อาศัยกายภาพด้วยปัจจัยการอนุรักษ์พลังงาน ซึ่งช่วยให้มั่นใจได้ว่าอย่างน้อยโมเดลการแรเงา BRDF สามารถแปลงเป็นแบบจริงได้ แต่ละ BRDF มีปัจจัยที่แตกต่างกัน เช่น ในกรณีของการสะท้อนแบบกระจายจะเท่ากับ 1/Pi