Global Dimming
The Magic Leap Global Dimmer feature in Unity allows developers to adjust the transparency of the Magic Leap's display. This functionality can be used to make sure your content is visible in a variety of lighting conditions. The Dimmer's value is represented between 0.0 (transparent) and 1.0 (opaque). Values outside of the range are silently clamped.
If the feature is used along with the Segmented Dimmer (Rendering Mode is Alpha Blend
) , the portions of the image affected by the Segmented Dimmer will be scaled to the range after global dimming is applied. For example if the Global Dimmer is set to 50% and a Segmented Dimmer mask of 50% the resulting Mask would be applied at 75%.
This feature requires the Magic Leap 2 Rendering Extensions OpenXR Feature to be enabled in your project's OpenXR Settings (Window > XR Plugin Manager > OpenXR Settings).
Example
Below is a simple example script for Unity that automatically cycles the dimmer value on a Magic Leap device. The script will gradually increase and decrease the dimming value between a minimum and a maximum range. Upon being disabled, it will turn off the global dimmer feature.
Attach this script to any GameObject in your Unity scene and deploy it to the device to see the result.
using UnityEngine;
using UnityEngine.XR.OpenXR;
using MagicLeap.OpenXR.Features;
public class AutoDimmerCycle : MonoBehaviour
{
[SerializeField, Range(0f, 1f), Tooltip("Minimum Global Dimmer value")]
private float minDimmerValue = 0f;
[SerializeField, Range(0f, 1f), Tooltip("Maximum Global Dimmer value")]
private float maxDimmerValue = 1f;
[SerializeField, Tooltip("Duration for a full dimmer cycle (min to max to min) in seconds")]
private float cycleDuration = 5f;
private MagicLeapRenderingExtensionsFeature renderFeature; // Reference to the Magic Leap rendering feature
private float timeSinceStart; // Time elapsed since the start of the cycle
private void Start()
{
renderFeature = OpenXRSettings.Instance.GetFeature<MagicLeapRenderingExtensionsFeature>();
if (renderFeature != null)
{
renderFeature.GlobalDimmerEnabled = true;
}
}
private void Update()
{
if (renderFeature != null)
{
// Update the time elapsed since the start
timeSinceStart += Time.deltaTime;
// Calculate the oscillating dimmer value using Mathf.PingPong
float range = maxDimmerValue - minDimmerValue;
float pingPongValue = Mathf.PingPong(timeSinceStart / cycleDuration * range * 2f, range);
float dimmerValue = minDimmerValue + pingPongValue;
// Apply the updated dimmer value
renderFeature.GlobalDimmerValue = dimmerValue;
}
}
private void OnDisable()
{
if (renderFeature != null)
renderFeature.GlobalDimmerEnabled = false;
}
}
Name | Description |
---|---|
OpenXRSettings.Instance.GetFeature<MagicLeapRenderingExtensionsFeature> | Magic Leap 2 Rendering extensions for OpenXR; provides support for controlling rendering features specific to the Magic Leap 2 such as Global and Dynamic dimming. |
MagicLeapRenderingExtensionsFeature.GlobalDimmerValue | float opacity value 0f (transparent) - 1f (opaque). |
MagicLeapRenderingExtensionsFeature.GlobalDimmerEnabled | bool Global Dimmer enabled (T) or disabled (F) |