Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

HDRP Decal Fade Factor not working from C# script

Discussion in 'Graphics Experimental Previews' started by Just_Lukas, Jun 7, 2019.

  1. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    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:
    1. Use projector's material to set color alpha channel
    2. 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.
     
  2. remi_unity163

    remi_unity163

    Unity Technologies

    Joined:
    Jun 19, 2018
    Posts:
    16
    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.
     
  3. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Yea calling projector.OnValidate() at the end of update method did update the transparency, thx a lot man.
     
  4. remi_unity163

    remi_unity163

    Unity Technologies

    Joined:
    Jun 19, 2018
    Posts:
    16
    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
     
  5. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,117
    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.
     
    Wolfos likes this.
  6. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    958
    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:

    Code (CSharp):
    1. [ExecuteInEditMode]
    2.  
    3. using UnityEngine;
    4. using UnityEngine.Rendering.HighDefinition;
    5. [ExecuteInEditMode]
    6. public class AnimatedDecal : MonoBehaviour
    7. {
    8.     private DecalProjector _decalProjector;
    9.    
    10.     void Awake()
    11.     {
    12.         _decalProjector = GetComponent<DecalProjector>();
    13.     }
    14.    
    15.     void LateUpdate()
    16.     {
    17.         _decalProjector.fadeFactor = _decalProjector.fadeFactor;
    18.     }
    19. }
    20.  
     
    masakki, Gillissie and IntoTheSoul like this.
  7. IntoTheSoul

    IntoTheSoul

    Joined:
    Oct 31, 2018
    Posts:
    30
    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

     
    Wolfos likes this.
  8. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    308
    Has anyone reported this bug to Unity?
     
  9. punk

    punk

    Joined:
    Jun 28, 2013
    Posts:
    408
    still seems to be an issue, doesn't work in timeline
     
    Gosciu likes this.