Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

HDRP - Change Custom Frame Settings through Scripts

Discussion in 'Graphics Experimental Previews' started by senordaneeh, Sep 23, 2019.

  1. senordaneeh

    senordaneeh

    Joined:
    Feb 2, 2019
    Posts:
    9
    How can I change the Frame Settings of a Camera through Scripting?
    I'm trying to have a custom user quality settings, and im looking for ways to toggle the Frame Setting mask through scripting, switching things like SSR or SSAO through a script.
    How can I do it? I can't find detailed enough info through the documentation
     
  2. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
  3. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Also how to make field with custom frame settings like in camera? And why I can't add more default frame settings? If I have several cameras with custom frame settings I need to manually go over each one and tick or untick a lot of checkboxes.
     
  4. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    766
    I made a small CS script to enable some frame settings in runtime if you'd like.

    You can find the whole list of fields here.


    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.Rendering.HighDefinition;
    3. using System.Collections.Generic;
    4. using System.Diagnostics;
    5. using System.Reflection;
    6. using System.Linq;
    7. using UnityEngine;
    8. using Utilities;
    9. using System;
    10.  
    11. public class CustomFrameSetting : MonoBehaviour
    12. {
    13.    
    14.     private     HDAdditionalCameraData             _cameraData;
    15.     private     FrameSettings                     _frameSettings;
    16.     private     FrameSettingsOverrideMask         _frameSettingsOverrideMask;
    17.    
    18.     void Start()
    19.     {
    20.         //Getting components
    21.         _cameraData = this.GetComponent<HDAdditionalCameraData>();
    22.         _frameSettings = _cameraData.renderingPathCustomFrameSettings;
    23.         _frameSettingsOverrideMask = _cameraData.renderingPathCustomFrameSettingsOverrideMask;
    24.        
    25.         //Make sure Custom Frame Settings are enabled in the camera
    26.         _cameraData.customRenderingSettings = true;
    27.        
    28.         //Enabling Lit Shader Mode
    29.         _frameSettingsOverrideMask.mask[(uint)FrameSettingsField.LitShaderMode] = true;
    30.         //...
    31.        
    32.         //Applying the frame setting mask back to the camera
    33.         _cameraData.renderingPathCustomFrameSettingsOverrideMask = _frameSettingsOverrideMask;
    34.        
    35.        
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.        
    42.         if(Input.GetKeyDown(KeyCode.F))
    43.         {
    44.             _frameSettings.litShaderMode = LitShaderMode.Forward;
    45.             SetFrameSettings(_frameSettings);
    46.         }
    47.        
    48.         if(Input.GetKeyDown(KeyCode.D))
    49.         {
    50.             _frameSettings.litShaderMode = LitShaderMode.Deferred;
    51.             SetFrameSettings(_frameSettings);
    52.         }
    53.  
    54.     }
    55.    
    56.     private void SetFrameSettings(FrameSettings frameSettings){
    57.         _cameraData.renderingPathCustomFrameSettings  = frameSettings;
    58.     }
    59. }
    60.  
     
    ebaender, _geo__, JerryKies and 2 others like this.
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Thanks!
     
  6. Benjamin_V

    Benjamin_V

    Joined:
    Apr 1, 2019
    Posts:
    11
    Hi, is it possible to directly modify the HDRP default settings instead of doing it on each camera ? I don't find any acces to it through script.
     
    JerryKies likes this.
  7. JerryKies

    JerryKies

    Joined:
    Apr 1, 2019
    Posts:
    2
    Any news on this? Couldn't find anything and it's kind of annoying to change every camera and volume per script. Would be much easier to be able to disable certain features globally. It is really hard to provide a half way decent settings menu for the players. Or did I miss anything and there is a way to achieve it in the meantime?
     
  8. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    766
    HDRP Global Settings are set manually and globally (as the name suggests) for the whole project you are working on. For performance (or artistic reasons), those can be overridden per camera as you already understood.

    For the concept of menu settings (like graphics quality) for users, the proper and current way to do this, is by having multiple HDRP assets with different features enabled per asset. That way a user could select a quality setting corresponding to an HDRP asset (low / medium / high for exemple). The HDRP template has this already setup if you want an example.

    upload_2022-9-27_16-41-54.png
     
  9. manbearpigman

    manbearpigman

    Joined:
    Feb 29, 2020
    Posts:
    37
    Finally some sweet sweet prog.
    So it took me forever to figure this out, because there's no f-----ing documentation explaining this stuff.
    But if you want something like custom LOD biases you need to also change the LOD bias mode to overwrite
    It may sound straight forward, but with no one explaining this it was confusing as heck.
    So for example if we want the LOD bias to = 2.2f

    It will look something like the attached file:
     

    Attached Files:

  10. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,340
    You can control some (many) things with volumes.

    One approach that worked for me was to create a global volume with very high priority (like 99). I then add my overrides to that dynamically. Since it's global and high priority it automatically overrides any other volumes.

    Example (generic base class):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3.  
    4. public abstract class SettingsVolume<T> : MonoBehaviour where T : SettingsVolume<T>
    5. {
    6.     private static T _instance;
    7.     public static T Instance
    8.     {
    9.         get
    10.         {
    11.             if (!_instance)
    12.             {
    13.                 _instance = new GameObject().AddComponent<T>();
    14.                 _instance.name = _instance.GetType().ToString();
    15.                 _instance.createVolume();
    16.                 _instance.init();
    17.  
    18.                 DontDestroyOnLoad(_instance.gameObject);
    19.             }
    20.             return _instance;
    21.         }
    22.     }
    23.  
    24.     protected Volume volume;
    25.  
    26.     protected virtual int getPriority()
    27.     {
    28.         return 99;
    29.     }
    30.  
    31.     protected virtual void createVolume()
    32.     {
    33.         volume = gameObject.AddComponent<Volume>();
    34.         volume.priority = getPriority();
    35.         volume.profile = ScriptableObject.CreateInstance<VolumeProfile>();
    36.     }
    37.  
    38.     protected abstract void init();
    39. }
    40.  

    Example of a concrete override volume that disables or enables shadows.
    Code (CSharp):
    1. using UnityEngine.Rendering.HighDefinition;
    2.  
    3. public class SettingsVolumeShadowsHDRP : SettingsVolume<SettingsVolumeShadowsHDRP>
    4. {
    5.     protected HDShadowSettings shadowSettings;
    6.     protected ContactShadows contactShadows;
    7.  
    8.     public bool Shadows
    9.     {
    10.         get
    11.         {
    12.             return !shadowSettings.maxShadowDistance.overrideState;
    13.         }
    14.  
    15.         set
    16.         {
    17.             shadowSettings.maxShadowDistance.overrideState = !value;
    18.             contactShadows.enable.overrideState = !value;
    19.         }
    20.     }
    21.  
    22.     override protected void init()
    23.     {
    24.  
    25.         // Setup to disable shadows via max distance = 0.
    26.         shadowSettings = volume.profile.Add<HDShadowSettings>();
    27.         shadowSettings.Override(shadowSettings, 1f);
    28.         shadowSettings.maxShadowDistance.value = 0f;
    29.         shadowSettings.maxShadowDistance.overrideState = false;
    30.  
    31.         // Setup to disable contact shadows
    32.         contactShadows = volume.profile.Add<ContactShadows>();
    33.         contactShadows.enable.value = false;
    34.         contactShadows.enable.overrideState = false;
    35.     }
    36. }

    Usage:
    SettingsVolumeShadowsHDRP.Instance.Shadows = false;


    You may end up with quite some GOs in the scene using this but if that's a problem then you can always combine them into one. Not sure if that's the way to go but it worked for me and I need not worry about which volumes or cameras are active at the moment. Of course this only work for things controllable by volumes.
     
  11. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    Is there really no way to globally toggle quality features at runtime? Without the hacky use of global volumes and per camera scripts.

    This can't possibly be the "proper" way.

    What If I want the user to control different bloom settings globally, and also different ambient occlusion settings? (like exists in every game)

    Global low/medium/high settings are not granular enough, and if anything just get in the way of/add complication to achieving the desired granular settings.

    Thanks :)
     
  12. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    In order to get granular quality settings, we are using a combination of a high priority global profile, which applies the quality indices, and per camera frame settings, to actually enable/disable the features.

    This seems to work in theory, but we are seeing some strange issues when attempting to set the quality index on various effects.

    For example, on a fresh volume, I add a ScreenSpaceAmbientOcclusion, and set the quality override to 1, desiring "Medium" Settings.

    I am doing it like this
    Code (CSharp):
    1. screenSpaceAmbientOcclusion.quality.Override(1);
    I would expect the volume to look like this, with only the quality level overridden.
    upload_2023-3-24_14-20-20.png

    Instead I am seeing this, where the quality level is set, but various sub settings are incorrectly set. This incorrectly applies the quality level, and it sometimes has the incorrect values here, according to the quality level set on the HDRP asset.

    upload_2023-3-24_14-21-48.png

    Is there something silly I am doing?

    Thanks,
    Chris
     
  13. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    Strangely, I have noticed that _sometimes_ it works as expected, with no sub setting overrides.
     
  14. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    Another thing I am not to sure on is how to globally set shadow quality. You cant seem to set this with a volume, and you cant set this with frame settings. What is the recommended way to change between low/medium/high resolution and filtering settings?

    Surely we don't need to add a script to every single light?
     
  15. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    766
    For shadow quality, you are supposed to set on your light the quality you desire and then you can manage this quality (resolution) in each HDRP settings like in the screenshots. Depending on which quality settings is set, a different HDRP asset is assigned to it and Medium will correspond to a specific resolution.

     
  16. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    Sorry I should have clarified in my post that I want to change these settings at runtime, for user exposed quality settings.

    The per camera script workaround is still the only working method, since the renderpipeline asset settings cannot be changed at runtime (for now!)
    https://forum.unity.com/threads/how...tering-quality-at-runtime-fix-coming.1425288/
     
    chap-unity likes this.