- Home /
Changing Post process V2 with C#
There are a lot of tutorials on how to use the new Post Process v2 but very little to no information on how to effect the stack with a script.
I am trying to manipulate a post process with C# and the documentation from unity seems to be for version 1. While the answerer HERE has me confused. but may be of use to more experienced programmers.
Following the Unity documentation I tried:
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class SteadyPostHealthDrain : MonoBehaviour
{
//When done this script should increase post process effects by a steady rate while active
//It could be cool to make a variable for another script to pass a multiplier so health drains faster without protective gear if the script is active.
PostProcessVolume m_Volume; //Using Apps Hungarian member m_ just because the documentation does.
Vignette m_Vignette;
ChromaticAberration m_ChromaticAberration;
DepthOfField m_DepthOfFeild;
Bloom m_Bloom;
bool passedOut; //Or dead
//All of this could be created with a post process volume that is turned off and on instead of instantiating.
void Start()
{
passedOut = false;
m_Vignette = ScriptableObject.CreateInstance<Vignette>();
m_Vignette.enabled.Override(true);
m_Vignette.intensity.Override(1f);
m_Vignette.smoothness.Override(100f);
m_ChromaticAberration = ScriptableObject.CreateInstance<ChromaticAberration>();
m_ChromaticAberration.enabled.Override(true);
m_ChromaticAberration.intensity.Override(1f);
m_Bloom = ScriptableObject.CreateInstance<Bloom>();
m_Bloom.enabled.Override(true);
m_Bloom.intensity.Override(1f);
m_Bloom.threshold.Override(1f);
m_DepthOfFeild = ScriptableObject.CreateInstance<DepthOfField>();
m_DepthOfFeild.enabled.Override(true);
m_DepthOfFeild.focusDistance.Override(1f);
//m_DepthOfFeild.kernelSize.GetValue<Small>; //How can I set the "Max Blur Size" to small?
m_Volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, m_Vignette);
m_Volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, m_ChromaticAberration);
m_Volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, m_Bloom);
}
//increase effects over time
void Update()
{
if (!passedOut) // post not fully faded in
{
//Bloom intensity from 0 to 5 Bloom threshold from 1.5 to .25
//Chromatic Aberration intensity from 0 to 1
//Depth of Field Focus Distance from 3f to .6f
//Vignette Intensity from 0 to 1 color #841717
//This effect could have a delay to start fading in a few seconds after the other effects.
}
if (passedOut) //already faded-in, destroy volume
{
//Remove the post process
Destroy(m_Volume, 0f);
// Display a failure message
//...instantiate a canvas?
}
// Better yet if a post process volume is used just turn off the effect and reset its values for next use.
}
}
However I am unsure of how to turn my pseudo-comments into a working script. and it seems the new v2 post process stack should be written differently. so I wrote this:
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class SteadyPostHealthDrain : MonoBehaviour
{
Vignette vignetteLayer; // why is this set to null in jPhilpp's example? Does that zero out the properties?
ChromaticAberration chromaticAberrationLayer;
DepthOfField DOFLayer;
Bloom bloomLayer;
private bool passedOut; //Or dead
private bool updateTimer = false;
private float effectTimer = 0.0f;
void Start()
{
passedOut = false;
//This seems to be one volume with multiple effects instead of the old mess seen in the last script.
PostProcessVolume activeVolume = gameObject.GetComponent<PostProcessVolume>();
activeVolume.profile.TryGetSettings(out vignetteLayer);
activeVolume.profile.TryGetSettings(out chromaticAberrationLayer);
activeVolume.profile.TryGetSettings(out DOFLayer);
activeVolume.profile.TryGetSettings(out bloomLayer);
updateTimer = true;
effectTimer = 0.0f;
}
//increase effects over time
void Update()
{
if (updateTimer == true)
{
effectTimer += Time.deltaTime * 1; // I can slow this down once I know it works
}
if (!passedOut) // post not fully faded in
{
//Bloom intensity from 0 to 5 Bloom threshold from 1.5 to .25
bloomLayer.enabled.value = true;
bloomLayer.threshold.value = effectTimer; // can this be clamped?
//Chromatic Aberration intensity from 0 to 1
chromaticAberrationLayer.enabled.value = true;
chromaticAberrationLayer.intensity.value = effectTimer;
//Depth of Field Focus Distance from 3f to .6f
DOFLayer.enabled.value = true;
DOFLayer.focusDistance.value = (3.0f - effectTimer);
//DOFLayer.MaxBlurSize = small ... how is this set by code? Do I even need to if I'm just turning off and on a volume?
//Vignette Intensity from 0 to 1 color #841717
//This effect could have a delay to start fading in a few seconds after the other effects.
if (effectTimer >= 15)
{
vignetteLayer.enabled.value = true;
//vignetteLayer.color.value = #841717; How?
vignetteLayer.intensity.value = (effectTimer - 15.0f);
}
}
if (passedOut) //already faded-in, destroy volume
{
//Remove the post process
bloomLayer.enabled.value = false;
chromaticAberrationLayer.enabled.value = false;
DOFLayer.enabled.value = false;
vignetteLayer.enabled.value = false;
//set the values back to a default ... is this needed?
//Adding a color grading effect to make the screen black may effect a failure message
// Display a failure message
//...instantiate a canvas?
}
}
}
I put the script on a post processing layer and tried to use a collision to activate it. Some of it worked its not turning off and on but the vision gets very blurry and colorful very fast. I tried putting the script on a collider that I could touch to activate it but it didn't work at all?
So.... If anyone knows of better documentation or a good tutorial on scripting these effects please let me know.
Your answer
Follow this Question
Related Questions
How to switch OFF motion vectors for SkinnedMeshRenderer ? 0 Answers
How to capture screenshot with post process effect? 1 Answer
Disabling / Destroying a camera component cause a black blinking / flickering on Android 0 Answers
Running an OnRenderImage effect before the post processing stack (v2) 1 Answer
Skybox and color correction with steamVR 0 Answers