ARPlaneMeshVisualizerFader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine.XR.Interaction.Toolkit.Utilities.Tweenables.Primitives;
  2. namespace UnityEngine.XR.Templates.AR
  3. {
  4. /// <summary>
  5. /// Performs additional visual operations on the ARPlane mesh, such as animated alpha fading.
  6. /// </summary>
  7. [RequireComponent(typeof(MeshRenderer))]
  8. public class ARPlaneMeshVisualizerFader : MonoBehaviour
  9. {
  10. [Tooltip("Renderer component on the ARPlaneMeshVisualizer prefab. Used to fetch the material to fade in/out.")]
  11. [SerializeField]
  12. Renderer m_PlaneRenderer;
  13. /// <summary>
  14. /// The Renderer component on the ARPlaneMeshVisualizer prefab. Used to fetch the material to fade in/out.
  15. /// </summary>
  16. public Renderer planeRenderer
  17. {
  18. get => m_PlaneRenderer;
  19. set => m_PlaneRenderer = value;
  20. }
  21. [Tooltip("Fade in/out speed multiplier applied during the alpha tweening. The lower the value, the slower it works. A value of 1 is full speed (1 second).")]
  22. [Range(0.1f, 1.0f)]
  23. [SerializeField]
  24. float m_FadeSpeed = 1f;
  25. /// <summary>
  26. /// Fade in/out speed multiplier applied during the alpha tweening.
  27. /// The lower the value, the slower it works. A value of 1 is full speed (1 second).
  28. /// </summary>
  29. public float fadeSpeed
  30. {
  31. get => m_FadeSpeed;
  32. set => m_FadeSpeed = value;
  33. }
  34. int m_ShaderAlphaPropertyID;
  35. float m_SurfaceVisualAlpha = 1f;
  36. float m_TweenProgress;
  37. Material m_PlaneMaterial;
  38. #pragma warning disable CS0618 // Type or member is obsolete -- affordance system to be replaced in a future XRI version
  39. readonly FloatTweenableVariable m_AlphaTweenableVariable = new FloatTweenableVariable();
  40. #pragma warning restore CS0618
  41. /// <summary>
  42. /// See <see cref="MonoBehaviour"/>.
  43. /// </summary>
  44. void Awake()
  45. {
  46. m_ShaderAlphaPropertyID = Shader.PropertyToID("_PlaneAlpha");
  47. m_PlaneMaterial = m_PlaneRenderer.material;
  48. visualizeSurfaces = true;
  49. }
  50. /// <summary>
  51. /// See <see cref="MonoBehaviour"/>.
  52. /// </summary>
  53. void OnDestroy()
  54. {
  55. m_AlphaTweenableVariable.Dispose();
  56. }
  57. /// <summary>
  58. /// See <see cref="MonoBehaviour"/>.
  59. /// </summary>
  60. void Update()
  61. {
  62. m_AlphaTweenableVariable.HandleTween(m_TweenProgress);
  63. m_TweenProgress += Time.unscaledDeltaTime * m_FadeSpeed;
  64. m_SurfaceVisualAlpha = m_AlphaTweenableVariable.Value;
  65. m_PlaneMaterial.SetFloat(m_ShaderAlphaPropertyID, m_SurfaceVisualAlpha);
  66. }
  67. /// <summary>
  68. /// Show plane surfaces if true, hide plane surfaces if false
  69. /// </summary>
  70. public bool visualizeSurfaces
  71. {
  72. set
  73. {
  74. m_TweenProgress = 0f;
  75. m_AlphaTweenableVariable.target = value ? 1f : 0f;
  76. m_AlphaTweenableVariable.HandleTween(0f);
  77. }
  78. }
  79. public void SetVisualsImmediate(float alpha)
  80. {
  81. m_AlphaTweenableVariable.target = alpha;
  82. m_AlphaTweenableVariable.HandleTween(1f);
  83. }
  84. }
  85. }