How to properly declare a TextureParameter in a Custom PostProcessing Effect?
Hello guys , I'm having a bit of troubke when trying to make cuatom postbprocessing effects that require extra texgures . Here is my code :
[Serializable]
[PostProcess(typeof(TextureOverlayRenderer), PostProcessEvent.BeforeStack, "Custom/TextureOverlay")]
public sealed class PostProcessingEffect_TextureOverlay : PostProcessEffectSettings
{
[Tooltip("Texture To Apply")]
public TextureParameter overlayTexture = new TextureParameter();
[Range(1.0f, 20.0f), Tooltip("Texture Repeat Factor")]
public FloatParameter textureRepeatFactor = new FloatParameter { value = 1.0f };
[Range(0f, 1f), Tooltip("Effect Opacity")]
public FloatParameter opacity = new FloatParameter { value = 0.5f };
public override bool IsEnabledAndSupported(PostProcessRenderContext context)
{
return enabled.value && (overlayTexture != null) && (opacity>0);
}
}
public sealed class TextureOverlayRenderer : PostProcessEffectRenderer<PostProcessingEffect_TextureOverlay>
{
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/Custom/TextureOverlay"));
sheet.properties.SetTexture("_OverlayTexture", settings.overlayTexture);
sheet.properties.SetFloat("_TextureRepeatFactor", settings.textureRepeatFactor);
sheet.properties.SetFloat("_EffectOpacity", settings.opacity);
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}
}
As you can see , I´m declaring the texture and setting it to new TextureParameter(). However in editor I'm always getting erros concerning that texture. The most common one is "Argument Null Exception , Argument cannot be null. Parameter Name:Value"
I really don't know what to do , when I move something in the effect editor or add the effect to the post processing cinemachine component those errors show up . But whem I'm in play mode they seem to go away.
public override bool IsEnabledAndSupported(PostProcessRenderContext context)//<--
{
return enabled.value && (overlayTexture != null) && (opacity>0);
}
[<--] what is this doing? It obviously is required for an override so you need to do something with it i think that that is causing your null ref error.
public override bool IsEnabledAndSupported(PostProcessRenderContext context)
{
TextureOverlayRenderer.Render(context);// not sure if this is the problem..
return enabled.value && (overlayTexture != null) && (opacity>0);
}
well actually, i don't think that wouldwork because its a sealed class
PS. Thanks didn't know you could do that. Sure cleans stuff up Big time!!
[Range(0f, 1f), Tooltip("Effect Opacity")]
over:
[Range(0f, 1f)]
[Tooltip("Effect Opacity")]
Answer by InkaTorque · Feb 19, 2019 at 06:14 AM
Anyone?? No one??? Because of those null references the setup of the post process in every virtual camera is getting erased randomly
Answer by watsonsong · Feb 22, 2020 at 05:37 AM
@InkaTorque, Have you resolve this, I think I met the same problem.
Answer by sharkwithlasers · Mar 24, 2020 at 06:33 AM
EDIT: Found a solution to this. The sequence of steps I used is as follows:
Add the TextureParameter to the *Settings class
Add this post processing effect as an effect in your post processing profile.
Set the texture in your post processing profile
Add the sheet.properties.SetTexture(...) statement in your *Renderer class.
This is a hacky fix, but it worked for me. I could definitely see this being super tedious when using this effect across many different profiles/projects (you'll need to comment out step 4, set the texture as in step 4, then uncomment out your comment).
I'd love to know if there is a better solution to this, however.
Original Post: I'm getting the same issue as well. Even when I have a null check in the Render function.
No clue what's going on.
Answer by yl_tys · Jul 22, 2021 at 09:49 AM
// Reference (UnityPackage: Post Processing Texture Overlay)
public sealed class PostMask : PostProcessEffectSettings
{
public TextureParameter maskTex = new TextureParameter { value = null };
}
public sealed class PostMaskRenderer : PostProcessEffectRenderer
{
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/tys/PostMask"));
var imageTexture = settings.maskTex.value == null? RuntimeUtilities.whiteTexture: settings.maskTex.value;
sheet.properties.SetTexture("_MaskTex", imageTexture);
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0);
}
}
Your answer
Follow this Question
Related Questions
Why am I getting error when using postprocessing v2 on profile.TryGetSettings ? 0 Answers
why the effects of the Post Processing stack do not activate through an if-statement 0 Answers
VR Post processing not woking in Lightweight RP 0 Answers
Bloom on iPod touch makes everything white and black 0 Answers
Setting Lift-Gamma-Gain values from C# script? (URP, Post-Processing v2) 1 Answer