For my PhD thesis I need to write an image effect that lets me configure a lens on a screen, but honestly, I can’t stop playing Archery Pro. In the calibration phase I need to modify the Image effect values through scripting.
To my surprise; there is a tutorial for doing changes in V1 that you can check here or in a forum post here but for V2 there where no guides.
Luckily enough, the forum post gave helped me out getting started to modify values on the Post Processing stack V2.
So lets say you have an image effect called “PitchTestImgEffect.cs”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using UnityEngine; using UnityEngine.Rendering.PostProcessing; [Serializable] [PostProcess(typeof(PitchTestRenderer), PostProcessEvent.AfterStack, "Hidden/PitchTest", false)] public sealed class PitchTestImgEffect : PostProcessEffectSettings { public FloatParameter pitch = new FloatParameter { value = 2.5f }; [Range(0f, 90f), Tooltip("Lens rotation angle")] public FloatParameter rotationAngle = new FloatParameter { value = 0 }; public TextureParameter pitchTex = new TextureParameter(); } //This class stores the rendering part (All the logic the img effect has). public class PitchTestRenderer : PostProcessEffectRenderer<PitchTestImgEffect> { public override void Render(PostProcessRenderContext context) { PropertySheet sheet = context.propertySheets.Get(Shader.Find("Hidden/PitchTest")); try{ sheet.properties.SetFloat("_Pitch", settings.rotationAngle); sheet.properties.SetFloat("_RotationAngle", settings.rotationAngle); sheet.properties.SetTexture("_PitchTestPattern", settings.pitchTex); context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0); } } } |
Easy enough, my image effect has 2 float values and a texture as you can see here:
What we need to do is to access the PostProcessVolume and get the PostProcessEffectSettings that contains our “PitchTestImgEffect” class, then we can modify its settings :-).
So with this script we can access the value of the pitch and set a value to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.PostProcessing; public class PitchTestController : MonoBehaviour { private PitchTestImgEffect pitchTestSettings; void OnEnable () { PostProcessVolume volume = GetComponent<PostProcessVolume>(); if(volume.profile == null) { enabled = false; Debug.Log("Cant load PostProcess volume"); return; } bool foundEffectSettings = volume.profile.TryGetSettings<PitchTestImgEffect>(out pitchTestSettings); if(!foundEffectSettings) { enabled = false; Debug.Log("Cant load PitchTest settings"); return; } Debug.Log("Got value: " + pitchTestSettings.pitch.GetValue<float>());//get current value Debug.Log("Setting value to 5"); pitchTestSettings.pitch.SetValue(new FloatParameter() { value = 5});//<- modify the value on the img effect } } |
Hopefully this helps you out :). If it helped you, drop me a line and let me know.
Glad it helped you out! 🙂
Thank you thank you thank you! This was driving me a bit crazy. Your explaination is simple and effective. Got it to work in less than 10 minutes after days of headaches!
I’m trying this and its not working and I’m getting mad
PostProcessVolume[] fxVols = FindObjectsOfType();
foreach (PostProcessVolume v in fxVols) {
if (v.isGlobal) {
fxvol = v;
continue;
}
}
BloomRenderer Settings;
bool test = fxvol.profile.TryGetSettings(out Settings);
Settings.settings.threshold.value = 1.31f ;
I haven’t checked that code in a while. But if you dig inside the classes it should be easy to find. That’s how I came up with this post. By checking the code. Hope it helps 🙂
I would like to ask how do you set active depthoffield to on and off when you click on a button?