using System; using UnityEngine; namespace Cinemachine.Examples { /// /// An example add-on module for Cinemachine Virtual Camera for controlling /// the FadeOut shader included in our example package. /// [AddComponentMenu("")] // Hide in menu [ExecuteAlways] public class CinemachineFadeOutNearbyObjects : CinemachineExtension { /// /// Radius of the look at target. /// [Tooltip("Radius of the look at target.")] public float m_LookAtTargetRadius = 1; /// /// Minimum distance to have fading out effect in front of the camera. /// [Tooltip("Minimum distance to have fading out effect in front of the camera.")] public float m_MinDistance = 0; /// /// Maximum distance to have fading out effect in front of the camera. /// [Tooltip("Maximum distance to have fading out effect in front of the camera.")] public float m_MaxDistance = 8; /// /// If true, m_MaxDistance will be set to /// distance between this virtual camera and LookAt target minus m_LookAtTargetRadius. /// [Tooltip("If true, MaxDistance will be set to " + "distance between this virtual camera and LookAt target minus LookAtTargetRadius.")] public bool m_SetToCameraToLookAtDistance = false; /// /// Material using the FadeOut shader. /// [Tooltip("Material using the FadeOut shader.")] public Material m_FadeOutMaterial; static readonly int k_MaxDistanceID = Shader.PropertyToID("_MaxDistance"); static readonly int k_MinDistanceID = Shader.PropertyToID("_MinDistance"); /// /// Updates FadeOut shader on the specified FadeOutMaterial. /// /// The virtual camera being processed /// The current pipeline stage /// The current virtual camera state /// The current applicable deltaTime protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime) { if (stage == CinemachineCore.Stage.Finalize) { if (m_FadeOutMaterial == null || !m_FadeOutMaterial.HasProperty(k_MaxDistanceID) || !m_FadeOutMaterial.HasProperty(k_MinDistanceID)) return; if (m_SetToCameraToLookAtDistance && vcam.LookAt != null) { m_MaxDistance = Vector3.Distance(vcam.transform.position, vcam.LookAt.position) - m_LookAtTargetRadius; } m_FadeOutMaterial.SetFloat(k_MaxDistanceID, m_MaxDistance); m_FadeOutMaterial.SetFloat(k_MinDistanceID, m_MinDistance); } } void OnValidate() { m_LookAtTargetRadius = Math.Max(0, m_LookAtTargetRadius); m_MinDistance = Math.Max(0, m_MinDistance); m_MaxDistance = Math.Max(0, m_MaxDistance); } } }