// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if GT_USE_URP
using UnityEngine;
using UnityEngine.UI;
namespace Microsoft.MixedReality.GraphicsTools
{
///
/// Automatically blurs an image and passes the image and rect size/location into materials which will overlay the rect.
///
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("Scripts/GraphicsTools/AcrylicBackgroundRectProvider")]
public class AcrylicBackgroundRectProvider : BaseMeshEffect
{
[Experimental]
[Tooltip("List of materials to apply the _BlurBackgroundRect and _blurTexture to.")]
[SerializeField]
private Material[] materials = null;
///
/// "List of materials to apply the _BlurBackgroundRect and _blurTexture to."
///
public Material[] Materials
{
get => materials;
set
{
materials = value;
UpdateMaterialsProperties();
}
}
[Tooltip("List of Graphic components to apply the _BlurBackgroundRect and _blurTexture to. Use this if your target has a material which changes or is instanced at runtime.")]
[SerializeField]
private Graphic[] graphics = null;
///
/// "List of Graphic components to apply the _BlurBackgroundRect and _blurTexture to. Use this if your target has a material which changes or is instanced at runtime."
///
public Graphic[] Graphics
{
get => graphics;
set
{
graphics = value;
InstanceGraphicComponents();
UpdateMaterialsProperties();
}
}
///
/// Should Graphics components create unique materials?
///
public bool UseInstanceMaterials
{
get { return instanceMaterials; }
set
{
if (instanceMaterials != value)
{
instanceMaterials = value;
InstanceGraphicComponents();
UpdateMaterialsProperties();
}
}
}
[Tooltip("Should Graphics components create unique materials?")]
[SerializeField]
private bool instanceMaterials = false;
[Tooltip("The index of the layer in the AcrylicLayerManager to copy settings from.")]
[SerializeField]
private int layerIndex = 0;
[Tooltip("The material to use when copying the source texture to a render texture for blurring. (If none is specified, uses the default Unity blit material.)")]
[SerializeField]
private Material blitMaterial = null;
[Tooltip("The destination color to copy the source texture onto when using a custom blit material.")]
[SerializeField]
private Color blitColor = Color.black;
///
/// Access to the pre-blurred texture.
///
public Texture SourceTexture
{
get
{
Texture output = null;
Image image = GetComponent();
if (image != null)
{
output = (image.sprite != null) ? image.sprite.texture : null;
}
else
{
RawImage rawImage = GetComponent();
if (rawImage != null)
{
output = rawImage.texture;
}
}
return output;
}
}
///
/// Access to the result of BlurImageTexture.
///
public Texture BlurredTexture
{
get => source;
}
private Canvas canvas = null;
private RenderTexture source = null;
private RenderTexture destination = null;
private int rectNameID = 0;
private int textureID = 0;
private bool hasBlurred = false;
///
/// Blurs the image at startup.
///
protected override void Start()
{
base.Start();
// Avoid blurring twice if another script called BlurImageTexture before start.
if (!hasBlurred)
{
BlurImageTexture();
}
}
///
/// Updates the material properties each frame.
///
protected void Update()
{
UpdateMaterialsProperties();
}
///
/// Cleans up all rendering resources.
///
protected override void OnDestroy()
{
base.OnDestroy();
if (source != null)
{
source.Release();
source = null;
}
if (destination != null)
{
destination.Release();
destination = null;
}
// Restore the blit material to it's initial state in case it was dirtied during BlurImageTexture.
MaterialRestorer.Restore(blitMaterial);
}
///
/// Update materials whenever the mesh is modified.
///
public override void ModifyMesh(VertexHelper vh)
{
UpdateMaterialsProperties();
}
///
/// Iterates over all materials and graphics and applies the latest blur properties.
///
public void UpdateMaterialsProperties()
{
if (!Application.isPlaying)
{
return;
}
if (canvas == null)
{
canvas = GetComponentInParent