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 riêng bạn.
Điều kiện tiên quyết
Đảm bảo rằng bạn hiểu các khái niệm cơ bản về thực tế tăng cường 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ế độ thích hợp
Định cấu hình tính năng Ước tính ánh sáng một lần cho mỗi phiên đối với 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 ước tính á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ột chế độ 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)
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 cường độ ánh sá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 rằng ánh sáng phản xạ từ một bề mặt sẽ không bao giờ mạnh hơn trước khi chạm 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ũ được dùng trong trò chơi điện tử và ứng dụng di động.
Nếu bạn đang sử dụng quy trình kết xuất dựa trên vật lý với tính năng ước tính ánh sáng HDR môi trường, chỉ cần đảm bảo rằng các vật liệu dựa trên vật lý được dùng trong các đối tượng ảo của bạn.
Tuy nhiên, nếu không sử dụng quy trình dựa trên vật lý, bạn có thể chọn một trong hai cách sau:
Giải pháp lý tưởng nhất cho vấn đề này là di chuyển sang một quy trình dựa trên vật lý.
Tuy nhiên, nếu không thể, một giải pháp thay thế hiệu quả là nhân giá trị albedo của vật liệu không dựa trên vật lý với hệ số bảo toàn năng lượng. Điều này có thể đảm bảo rằng ít nhất mô hình đổ bóng BRDF có thể được chuyển đổi thành mô hình dựa trên vật lý. Mỗi BRDF có một hệ số riêng, ví dụ: đối với phản xạ khuếch tán, hệ số này là 1/Pi.