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.

Java

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

Định cấu hình chế độ ENVIRONMENTAL_HDR

Để định cấu hình chế độ ENVIRONMENTAL_HDR, hãy lấy thông tin ước tính về ánh sáng cho từng khung hình, sau đó lấy các thành phần ánh sáng HDR môi trường mà bạn muốn sử dụng.

Java

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

Định cấu hình chế độ AMBIENT_INTENSITY

Nếu bạn dự định sử dụng thành phần chỉnh màu của chế độ AMBIENT_INTENSITY, trước tiên, hãy tránh phân bổ tính năng chỉnh màu trên mọi khung hình bằng cách sử dụng lại mức phân bổ dùng chung.

Java

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

Kotlin

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

Nhận thông tin ước tính về ánh sáng cho từng khung hình, sau đó nhận các thành phần về cường độ môi trường xung quanh mà bạn muốn sử dụng.

Java

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

Đả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.