Search Unity

How to change anti-aliasing and Shadow Resolution through code

Discussion in 'High Definition Render Pipeline' started by itsnottme, Sep 4, 2020.

  1. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    Hey, how do I change the anti-aliasing on the camera through code? I tried to access it but I can't find the right variable to change.

    Also, how do I change the shadow resolution through code in HDRP? It used to be QualitySettings.shadowResolution in standard shader, but now it's on the lights and HDRP settings.
    Accessing the shadow distance and cascades through the volume isn't a problem.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Here's what I use for Anti Aliasing. You want to set the antialiasing of the HDAdditionalCameraData object on the same gameobject as the camera.

    Code (CSharp):
    1. if (hdCameraData != null && shouldAdjustHDCameraOverrides)
    2. {
    3.     if (hdCameraData.antialiasing != GraphicsPlayerOptions.Antialiasing)
    4.     {
    5.         //Debug.Log($"Setting Anti-aliasing to to {GraphicsSettings.Antialiasing}");
    6.         hdCameraData.antialiasing = GraphicsPlayerOptions.Antialiasing;
    7.  
    8.         // If using SMAA, make sure quality is set to High.
    9.         if (GraphicsPlayerOptions.Antialiasing == HDAdditionalCameraData.AntialiasingMode.SubpixelMorphologicalAntiAliasing)
    10.         {
    11.             hdCameraData.SMAAQuality = HDAdditionalCameraData.SMAAQualityLevel.High;
    12.         }
    13.     }
    14. }
    I'm not sure about the shadow resolution, and whether you just need to iterate over all lights in the scene, or if there's a better way.
     
    itsnottme likes this.
  3. itsnottme

    itsnottme

    Joined:
    Mar 8, 2017
    Posts:
    129
    That works, thank you.
     
  4. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    How about the other types of Anti-Aliasing? Like, MSAA to be specific.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    The code I posted is for HDRP 7, so I don't know if things have changed in later versions, but in HDRP 7 it's just a matter of choosing the correct HDAdditionalCameraData.AntialiasingMode value. No idea if that's still the case in later versions.
     
  6. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    I'll have to check this at work tomorrow. As far as I know, when doing this in the Editor for HDRP 7.5, we need to go into the HDRP quality settings, set HDRP to Forward rendering, check the MSAA checkbox, then go into the Frame Settings for the Camera, and set that to MSAA. At least, that's what we do if we want MSAA. I'll have to look though my copy of the HDRP 7.5 source code to see how setting the values onto the HDAdditionalCameraData trickles back to the project settings.
     
  7. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,279
    MSAA being a completely separate setup to the other AA settings is just... odd. I mean I guess because it is a forward-rendering hardware thing, not a post-effect (?) but I see no way to change MSAA at runtime either except by changing quality settings with different setups.

    Shadow resolution does not seem changeable on a light at runtime (HDAdditionalLightData for a light just has gets like so many HDRP classes) but you can change quality settings to one with different shadow resolution settings (via a different hdrp asset assigned to it)

    Quality settings let you change a load of hdrp settings at runtime by applying a different hdrp asset but no good for 'toggling' individual features