Control passthrough dimming
Read and request the passthrough dimming level from your app, and respond when the system changes it.
Request a dimming level to control how much light from the physical environment the device blocks. For example, you can use dimming to make virtual content easier to read. Dimming is only available on devices whose runtime supports the XR_ANDROID_global_passthrough_dimming OpenXR extension.
To learn how Android XR applies dimming by default, and how dimming differs between devices, refer to Introduction to passthrough dimming.
Prerequisites
Before you control passthrough dimming, enable the Android XR: Camera (Passthrough) feature. If you disable this feature, the Android XR package doesn't enable the dimming extension, and the dimming APIs return an error status. For instructions, refer to Configure project settings.
Request and read dimming levels
To request a dimming level:
- Call
ARCameraFeature.TryGetSupportedDimmingLevelsto get the dimming levels that the runtime supports. If the call returns an error status, the API didn't return the supported levels. For example, this happens if the runtime doesn't support dimming. - Call
ARCameraFeature.TryRequestGlobalDimmingLevelwith a value from0to1. A value of0is no light blocking (least dimming), and1is maximum light blocking (most dimming). The API clamps values outside this range to0or1.
The runtime doesn't apply a requested dimming level immediately. When the runtime processes your request, it uses the requested level instead of the default dimming level for the current blend mode. If the requested value doesn't map exactly to a supported dimming level, the runtime rounds it to the closest supported dimming level.
To read the current dimming level, call ARCameraFeature.TryGetGlobalDimmingLevel. Don't read the level immediately after you request it, because the runtime hasn't processed your request yet.
The following code sample shows how to use the public dimming APIs to request and read dimming levels in your app:
[SerializeField, Range(0f, 1f)]
float targetDimming = 0.5f;
void ApplyDimming()
{
var supportedResult =
ARCameraFeature.TryGetSupportedDimmingLevels(Allocator.Temp);
if (supportedResult.status.IsError())
{
Debug.LogWarning("Failed to get the supported dimming levels.");
return;
}
// Dimming level values are [0,1] (least to most) returned in
// ascending order.
var supportedLevels = supportedResult.value;
if (supportedLevels.Length == 0)
{
Debug.LogWarning("The runtime returned no supported dimming levels.");
return;
}
// Request a dimming value in [0,1] (least to most).
// The API clamps the value to [0,1], and the runtime rounds
// it to the closest supported level.
var setStatus =
ARCameraFeature.TryRequestGlobalDimmingLevel(targetDimming);
if (setStatus.IsError())
{
Debug.LogWarning("Failed to request dimming.");
return;
}
}
void GetDimming()
{
var currentResult = ARCameraFeature.TryGetGlobalDimmingLevel();
if (currentResult.status.IsError())
{
Debug.LogWarning("Failed to read the current dimming level.");
return;
}
Debug.Log($"Requested dimming: {targetDimming}");
Debug.Log($"Current dimming: {currentResult.value}");
}
Respond to dimming changes
The system, hardware controls, and other runtime-driven behavior can change the dimming level outside your own dimming requests. Respond to these changes if your app needs to update UI or other behavior when the dimming level changes.
To respond to dimming changes:
- When your component is enabled, subscribe to the
ARCameraFeature.dimmingLevelChangedevent. - When your component is disabled, unsubscribe from the event.
- In your event handler, call
ARCameraFeature.TryGetGlobalDimmingLevelto read the current dimming level.
The following code sample reads and logs the current dimming level when the runtime reports a dimming level change:
void OnEnable()
{
ARCameraFeature.dimmingLevelChanged += OnDimmingLevelChanged;
}
void OnDisable()
{
ARCameraFeature.dimmingLevelChanged -= OnDimmingLevelChanged;
}
void OnDimmingLevelChanged()
{
var currentResult = ARCameraFeature.TryGetGlobalDimmingLevel();
if (currentResult.status.IsError())
{
Debug.LogWarning("Failed to read the current dimming level.");
return;
}
Debug.Log($"Dimming changed: {currentResult.value}");
}
Your event handler now reads the current dimming level whenever the runtime reports a dimming level change.
Additional resources
- Introduction to passthrough dimming
- Set up passthrough
- Camera (AR Foundation)