Advertisement
Staggart

SCPE Built-in RP: Modifying effect parameters at runtime

Feb 6th, 2022 (edited)
2,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering.PostProcessing;
  3.  
  4. // Example for modifying an effect's parameters that has been added to a volume profile
  5. // This is practically identical to modifying settings for any of Unity's effects
  6. [RequireComponent(typeof(PostProcessVolume))]
  7. public class ModifyEffect : MonoBehaviour
  8. {
  9.     public PostProcessVolume volume;
  10.     private SCPE.RadialBlur radialBlurEffect;
  11.  
  12.     public AnimationCurve intensityCurve = new AnimationCurve(new Keyframe[]
  13.     {
  14.         //Bell curve
  15.         new Keyframe(0f, 0f), new Keyframe(0.5f, 1f), new Keyframe(1f, 0f)
  16.     });
  17.    
  18.     public float blendInSpeed = 4f;
  19.    
  20.     [Range(0f, 1f)]
  21.     private float progress;
  22.    
  23.     private void Reset()
  24.     {
  25.         volume = GetComponent<PostProcessVolume>();
  26.     }
  27.  
  28.     private void Start()
  29.     {
  30.         //Note: Accessing "profile" creates a new temporary copy of the profile, it can be modified freely. You can tell by the fact that the name of the profile turns blank in the inspector!
  31.         //This behaviour is identical to accessing "material" on a Renderer component (as opposed to "sharedMaterial")
  32.         //When using "sharedProfile" you'll be modifying the profile asset saved to disk. Changes made to parameters will be persistent, which isn't the intention here.
  33.         volume.profile.TryGetSettings<SCPE.RadialBlur>(out radialBlurEffect);
  34.  
  35.         if (!radialBlurEffect) radialBlurEffect = volume.profile.AddSettings<SCPE.RadialBlur>();
  36.        
  37.         //Another possible method is to create a temporary Volume object at runtime, with a runtime created profile. Then adding effects to it.
  38.  
  39.         //Much like in the inspector, a parameter has to be overriden first if you want to modify it. If it isn't, the value from another volume/profile may be controlling it.
  40.         radialBlurEffect.amount.overrideState = true;
  41.        
  42.         //Initialize a starting value
  43.         radialBlurEffect.amount.value = 0f;
  44.     }
  45.    
  46.     void Update()
  47.     {
  48.         if (!radialBlurEffect) return;
  49.        
  50.         progress += (blendInSpeed * Time.fixedDeltaTime);
  51.  
  52.         //An kind of parameter that controls the visibility of an effect (eg. intensity) is always between 0 and 1
  53.         progress = Mathf.Clamp01(progress);
  54.  
  55.         //Depending on the effect, the "main" parameter may be called different. Most of the time the parameter is called "intensity".
  56.         //You can use IntelliSense to browse the available parameters (CTRL+Space after the period).
  57.         //The .value path points to the actual value of the parameter (it may be a float, color, int, etc...)
  58.         radialBlurEffect.amount.value = intensityCurve.Evaluate(progress);
  59.     }
  60.  
  61.     private void LateUpdate()
  62.     {
  63.         if (Input.GetKeyDown(KeyCode.Space))
  64.         {
  65.             //Reset value. It'll get bumped up again in the Update function
  66.             progress = 0f;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement