#if HAVE_NODECANVAS using System; using UnityEngine; #if HAVE_STANDARDEFFECTS using UnityStandardAssets.ImageEffects; #endif using ParadoxNotion.Design; using NodeCanvas.Framework; namespace Framework.Cameras { /// Adjusts the blur on a camera's image [Category("Camera")] [Description("Adjusts the blur on a camera's image")] public class BlurCameraTask : ActionTask { /// Camera whose blur level will be adjusted [RequiredField] public BBParameter Camera; /// Duration of the action in seconds public float Length; /// Blur size the camera will begin with public float StartBlurLevel; /// Blur size the camera will end with public float TargetBlurLevel; /// Number of iterations the camera will begin with public int StartIterationCount; /// Number of iterations the camera will end with public int TargetIterationCount; /// Summary of what this action does protected override string info { get { if(this.Length == 0.0f) { if(this.TargetBlurLevel == 0.0f) { return "Set Camera Unblurred"; } else if(this.TargetBlurLevel == 1.0f) { return "Set Camera Fully Blurred"; } else { return "Set Camera Blurred " + this.TargetBlurLevel.ToString(); } } else if(this.TargetBlurLevel > this.StartBlurLevel) { return "Camera Slowly Blurrs"; } else if(this.TargetBlurLevel < this.StartBlurLevel) { return "Camera Slowly Unblurrs"; } else { return "Camera Stays Blurred"; } } } /// Called once when the action is executed protected override void OnExecute() { bool enable = (this.StartBlurLevel > 0.0f) || (this.TargetBlurLevel > 0.0f); #if HAVE_STANDARDEFFECTS if(this.optimizedBlur != null) { this.optimizedBlur.enabled = enable; } else if(this.blur != null) { this.blur.enabled = enable; } #endif // HAVE_STANDARDEFFECTS #if HAVE_SUPERBLUR if (this.superBlur != null) { this.superBlur.enabled = enable; } #endif // HAVE_SUPERBLUR } /// Called once before the action is executed for the first time /// An error message or null if no error occurred protected override string OnInit() { string error = base.OnInit(); if(error == null) { #if !HAVE_STANDARDEFFECTS && !HAVE_SUPERBLUR Debug.LogError( "Camera blur tasks will not function because no effects package " + "has been enabled. Set HAVE_STANDARDEFFECTS for standard Unity blur." ); #endif bool haveBlurEffect = false; #if HAVE_STANDARDEFFECTS this.optimizedBlur = this.Camera.value.GetComponent(); if(this.optimizedBlur == null) { this.blur = this.Camera.value.GetComponent(); haveBlurEffect |= (this.blur != null); } else { haveBlurEffect = true; } #endif // HAVE_STANDARDEFFECTS #if HAVE_SUPERBLUR this.superBlur = this.Camera.value.GetComponent(); haveBlurEffect |= (this.superBlur != null); #endif // HAVE_SUPERBLUR if (!haveBlurEffect) { Debug.LogError( "Camera '" + this.Camera.name + "' has none of the supported + enabled " + "blur filters. Camera blurring will not work." ); } } return error; } /// /// Called every frame, if and while the action is running and until it ends /// protected override void OnUpdate() { #if HAVE_STANDARDEFFECTS || HAVE_SUPERBLUR float t = (elapsedTime / this.Length); float blurLevel = Mathf.Lerp(this.StartBlurLevel, this.TargetBlurLevel, t); blurLevel *= blurLevel; int iterationCount = Mathf.RoundToInt( Mathf.Lerp((float)this.StartIterationCount, (float)this.TargetIterationCount, t) ); #endif // HAVE_STANDARDEFFECT || HAVE_SUPERBLUR #if HAVE_STANDARDEFFECTS if(this.optimizedBlur != null) { this.optimizedBlur.blurSize = blurLevel * 10.0f; this.optimizedBlur.blurIterations = iterationCount; } else if(this.blur != null) { this.blur.blurSpread = blurLevel; this.blur.iterations = iterationCount; } #endif // HAVE_STANDARDEFFECTS #if HAVE_SUPERBLUR if(this.superBlur != null) { this.superBlur.iterations = iterationCount; this.superBlur.interpolation = blurLevel; } #endif // HAVE_SUPERBLUR if(elapsedTime >= this.Length) { EndAction(); } } /// Called once when the task ends for any reason protected override void OnStop() { base.OnStop(); #if HAVE_STANDARDEFFECTS if(this.optimizedBlur != null) { this.optimizedBlur.blurSize = this.TargetBlurLevel * 10.0f; this.optimizedBlur.blurIterations = this.TargetIterationCount; if(this.TargetBlurLevel == 0.0f) { this.optimizedBlur.enabled = false; } } else if(this.blur != null) { this.blur.blurSpread = this.TargetBlurLevel; this.blur.iterations = this.TargetIterationCount; if(this.TargetBlurLevel == 0.0f) { this.blur.enabled = false; } } #endif // HAVE_STANDARDEFFECTS #if HAVE_SUPERBLUR if (this.superBlur != null) { this.superBlur.interpolation = this.TargetBlurLevel; this.superBlur.iterations = this.TargetIterationCount; if (this.TargetBlurLevel == 0.0f) { this.superBlur.enabled = false; } } #endif // HAVE_SUPERBLUR } #if HAVE_STANDARDEFFECTS /// Optimized camera blur filter [NonSerialized] private BlurOptimized optimizedBlur; /// Camera blur filter [NonSerialized] private Blur blur; #endif // HAVE_STANDARDEFFECTS #if HAVE_SUPERBLUR /// Free gaussian blur camera effect [NonSerialized] private SuperBlur.SuperBlur superBlur; #endif // HAVE_SUPERBLUR } } // namespace Framework.Cameras #endif // HAVE_NODECANVAS && HAVE_STANDARDEFFECTS