Vật thể ảo chiếu sáng thực tế trong cảnh

Tìm hiểu cách sử dụng tính năng Ước tính ánh sáng trong ứng dụng của bạn.

Điều kiện tiên quyết

Hãy đảm bảo bạn hiểu rõ các khái niệm cơ bản về AR và cách định cấu hình một phiên ARCore trước khi tiếp tục.

Định cấu hình API một lần cho mỗi phiên bằng chế độ phù hợp

Định cấu hình Ước tính ánh sáng một lần cho mỗi phiên cho chế độ bạn muốn sử dụng.

HDR môi trường

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

Cường độ môi trường xung quanh

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

Đã tắt

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

Sử dụng các giá trị thu được từ Giá trị ước tính ánh sáng

Để sử dụng các giá trị thu được từ Ước tính ánh sáng, hãy lấy số liệu ước tính về ánh sáng cho từng khung hình.

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

Sau đó, hãy lấy các thành phần ánh sáng HDR môi trường cho cấu hình hiện tại:

HDR môi trường

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

Cường độ môi trường xung quanh

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

Đảm bảo tiết kiệm năng lượng bằng Environmental HDR API

Bảo toàn năng lượng là nguyên tắc cho thấy ánh sáng phản chiếu từ một bề mặt sẽ không bao giờ cường độ cao hơn trước khi chiếu vào bề mặt đó. Quy tắc này được thực thi trong quá trình kết xuất dựa trên vật lý, nhưng thường bị bỏ qua trong các quy trình kết xuất cũ dùng trong trò chơi điện tử và ứng dụng di động.

Nếu đang sử dụng quy trình kết xuất dựa trên vật lý có ước tính ánh sáng HDR môi trường, bạn chỉ cần đảm bảo vật liệu dựa trên vật lý được sử dụng trong các đối tượng ảo của mình.

Tuy nhiên, nếu không sử dụng quy trình dựa trên vật lý, bạn có một số lựa chọn:

  • Giải pháp lý tưởng nhất cho vấn đề này là chuyển sang hệ thống vận hành dựa trên vật lý.

  • Tuy nhiên, nếu không thể làm vậy, một giải pháp hay là nhân giá trị suất phân chiếu từ một vật liệu không dựa trên vật lý với một hệ số bảo toàn năng lượng. Điều này đảm bảo ít nhất mô hình đổ bóng BRDF có thể được chuyển đổi thành mô hình thực tế. Mỗi BRDF có một hệ số khác nhau – ví dụ: đối với độ phản chiếu khuếch tán, giá trị này là 1/Pi.