Advertisement
Staggart

SCPE URP: Modifying effect parameters at runtime

Feb 6th, 2022 (edited)
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  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(Volume))]
  7. public class ModifyEffect : MonoBehaviour
  8. {
  9.     public Volume 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<Volume>();
  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.TryGet(typeof(SCPE.RadialBlur), out radialBlurEffect);
  34.        
  35.         //Another possible method is to create a temporary Volume object at runtime, with a runtime created profile. Then adding effects to it.
  36.  
  37.         //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.
  38.         radialBlurEffect.amount.overrideState = true;
  39.        
  40.         //Initialize a starting value
  41.         radialBlurEffect.amount.value = 0f;
  42.     }
  43.    
  44.     void Update()
  45.     {
  46.         if (!radialBlurEffect) return;
  47.        
  48.         progress += (blendInSpeed * Time.fixedDeltaTime);
  49.  
  50.         //An kind of parameter that controls the visibility of an effect (eg. intensity) is always between 0 and 1
  51.         progress = Mathf.Clamp01(progress);
  52.  
  53.         //Depending on the effect, the "main" parameter may be called different. Most of the time the parameter is called "intensity".
  54.         //You can use IntelliSense to browse the available parameters (CTRL+Space after the period).
  55.         //The .value path points to the actual value of the parameter (it may be a float, color, int, etc...)
  56.         radialBlurEffect.amount.value = intensityCurve.Evaluate(progress);
  57.     }
  58.  
  59.     private void LateUpdate()
  60.     {
  61.         if (Input.GetKeyDown(KeyCode.Space))
  62.         {
  63.             //Reset value. It'll get bumped up again in the Update function
  64.             progress = 0f;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement