HDRP Decal Fade Factor not working from C# script

Yo, I’ve created simple C# script which is using Decal Projector Component’s fade factor to fade out instantiated decal. Unfortunatelly this fading effect only works when I move the slider in the inspector but not when I modify projector.m_FadeFactor variable from script. Despite the fact that editor Fade Factor slider properly updates based on C# script, the opacity of decal itself will stay unchanged, even if the fade value is set to 0.

I also tried the following to dynamically chagne decal’s transparency but with no success:

  • Use projector’s material to set color alpha channel
  • Use projector’s material “Global Opacity” slider

Can somebody help me with this simple fading-out instantiated decals please?
Any help will be much appreciated.

Side Notes:

  • Projector is not instantiating its own material during runtime but rather uses sharedMaterial for some reason so coloring one decal will color the rest as well.
  • The m_FadeFactor variable is in range from 0 to 1 while inspector slider value goes from 0 to 100.

Hello,

Indeed this is an API issue.
When something change in the Inspector, the OnValidate() method is called. It is a public function on DecalProjectorComponent that you need to call manually after a change to update the decal when scripting at the moment.

I’ll enter an issue on this. Hopefully this workarround will work for you.

Yea calling projector.OnValidate() at the end of update method did update the transparency, thx a lot man.

There is also a DecalSystem that should help a lot while scripting decals. You can check the class for more info.

Note: the API documentation is still in construction

The issue with this is that the decal cannot be animated via mecanim. This issue is still a problem in 2021 October. Please fix this.

1 Like

For the next person who comes here, it’s fixed now when setting it through code but not when setting it through an animation. So add the following wonderfully confusing code to your object, which will fix it:

[ExecuteInEditMode]

using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
[ExecuteInEditMode]
public class AnimatedDecal : MonoBehaviour
{
    private DecalProjector _decalProjector;
   
    void Awake()
    {
        _decalProjector = GetComponent<DecalProjector>();
    }
    
    void LateUpdate()
    {
        _decalProjector.fadeFactor = _decalProjector.fadeFactor;
    }
}
3 Likes

Thanks for this. In addition I think instead of setting it as [ExecuteInEditMode], it should be [ExecuteAlways] based on what the documentation states.

https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html

1 Like

Has anyone reported this bug to Unity?

still seems to be an issue, doesn’t work in timeline

1 Like

Same problem. Will try to use provided hack. It’s too bad they dont provide the decal projector with an update EveryFrame setting etc like they do with other components