#if HAVE_SLATE using System; using UnityEngine; using Slate; using Slate.ActionClips; using Framework.Support; namespace Framework.Cinematics { /// Fades a standard shader material using its alpha channel [Category("Material")] [Description("Fades a material in and out using its color's alpha channel")] public class FadeMaterialAlphaAction : ActorActionClip { /// For how long the text will be displayed (including fading time) public float Length = 2; /// Time over which the text will fade in public float BlendIn = 0.25f; /// Time over which the text will fade out public float BlendOut = 0.25f; /// Fading method to use for the alpha channel public EaseType interpolation = EaseType.QuadraticInOut; /// Summary of what this action does public override string info { get { return "Fade Material"; } } /// Called before the event is played back for the first time protected override void OnEnter() { this.materials = MaterialHelper.GetMeshMaterials(actor); int materialCount = this.materials.Length; this.previousAlpha = new float[materialCount]; for(int index = 0; index < materialCount; ++index) { previousAlpha[index] = this.materials[index].color.a; } } /// Called when the action's effects should be reversed /// /// Called either after the event has been scrubbed/played in reverse /// or when the stop button is hit after the event was played. /// protected override void OnReverse() { int materialCount = this.materials.Length; for(int index = 0; index < materialCount; ++index) { Color color = this.materials[index].color; color.a = this.previousAlpha[index]; this.materials[index].color = color; } } /// /// Called every frame, if and while the action is running and until it ends /// protected override void OnUpdate(float deltaTime) { int materialCount = this.materials.Length; float weight = GetClipWeight(deltaTime); float opacity = Easing.Ease(this.interpolation, 0.0f, 1.0f, weight); for(int index = 0; index < materialCount; ++index) { Color color = this.materials[index].color; //color.a = Easing.Ease(this.interpolation, 0.0f, this.previousAlpha[index], weight); color.a = opacity; this.materials[index].color = color; } } /// Duration of the action in seconds public override float length { get { return this.Length; } set { this.Length = value; } } /// How long the fade-in should take in seconds public override float blendIn { get { return this.BlendIn; } set { this.BlendIn = value; } } /// How long the fade-out should take in seconds public override float blendOut { get { return this.BlendOut; } set { this.BlendOut = value; } } /// All materials in whom the opacity level will be changed [NonSerialized] private Material[] materials; /// Opacity levels the materials had before the action [NonSerialized] private float[] previousAlpha; } } // namespace Framework.Cinematics #endif // HAVE_SLATE