Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Access Renderer Feature settings at runtime

Discussion in 'Shader Graph' started by tasticad, Nov 2, 2019.

  1. tasticad

    tasticad

    Joined:
    Dec 4, 2017
    Posts:
    10
    Hello everyone,

    Please can someone help me with that? How can I access the renderer setting of a Custom Renderer in Unity 2019.2?

    I would like for instance to switch the Comp value from Equal to Always at runtime:

    Unity_renderer_feature_settings.png
     
  2. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    693
    Some thing like this
    but works only in LWRP in URP not work anymore :(


    foreach ( var renderObjSetting in _scriptableRendererData.rendererFeatures.OfType<RenderObjects>( ) )
    {
    renderObjSetting.settings.cameraSettings.cameraFieldOfView = _currentFPSFov;
    renderObjSetting.settings.cameraSettings.offset = _currentFPSOffset;
    }

    Link to _scriptableRendererData store in [SerializedField]
     
  3. echuang98

    echuang98

    Joined:
    Oct 21, 2015
    Posts:
    21
    I was also wondering how I can access and modify renderer feature settings at runtime.
    I am trying to achieve that with the below code in 2019.3.3f1.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.Universal;
    5. using UnityEngine.Experimental.Rendering.Universal;
    6.  
    7. public class TestChangeRPValues : MonoBehaviour
    8. {
    9.     // Reference to the ForwardRendererData asset
    10.     public ForwardRendererData forwardRendererData;
    11.  
    12.     List<ScriptableRendererFeature> features;
    13.  
    14.     Pixelate pixelate;
    15.     [Range(1, 128)]
    16.     public int testPixelSize = 10;
    17.  
    18.     RenderObjects renderObjects;
    19.     public LayerMask testLayerMask;
    20.  
    21.     void Start()
    22.     {
    23.         // Reference to the render features on the ForwardRendererData
    24.         features = forwardRendererData.rendererFeatures;
    25.  
    26.         pixelate = (Pixelate)features[0];
    27.         renderObjects = (RenderObjects)features[1];
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         pixelate.settings.pixelSize = testPixelSize;
    33.         renderObjects.settings.filterSettings.LayerMask = testLayerMask;
    34.     }
    35. }
    36.  
    1st_Moment.jpg

    I want to modify "Pixel Size" and "Layer Mask" in the right inspector with my script. (Above image)
    The parameters seem to change only when my mouse hover on to the right inspector.
    Although the parameters in the right inspector did change, those settings were not applied to game view and scene view. (Below Video)

    If I directly modify the right inspector, those settings are applied successfully. (Below Video) However I need to modify them at runtime with scripts.


    What is the correct way to access and modify renderer feature settings at runtime?
    It would be so great if someone could shed some light on this!
     
    qsleonard likes this.
  4. Jes28

    Jes28

    Joined:
    Sep 3, 2012
    Posts:
    693
    Currently in out project:

    Code (CSharp):
    1. private        void    ExtractScriptableRendererData()
    2. {
    3.     var pipeline = ((UniversalRenderPipelineAsset)GraphicsSettings.renderPipelineAsset);
    4.     FieldInfo propertyInfo = pipeline.GetType(  ).GetField( "m_RendererDataList", BindingFlags.Instance | BindingFlags.NonPublic );
    5.     _scriptableRendererData = ((ScriptableRendererData[]) propertyInfo?.GetValue( pipeline ))?[0];
    6. }

    Code (CSharp):
    1. foreach ( var renderObjSetting in _scriptableRendererData.rendererFeatures.OfType<UnityEngine.Experimental.Rendering.Universal.RenderObjects>( ) )
    2. {
    3.     renderObjSetting.settings.cameraSettings.cameraFieldOfView    = _currentFPSFov;
    4.     renderObjSetting.settings.cameraSettings.offset                = _currentFPSOffset;
    5. }
    Without this it dont work in builds at all.

    May be Last URP have some more direct way to access RenderObjects I dont know about.
    For now this code make us happy :)
     
  5. echuang98

    echuang98

    Joined:
    Oct 21, 2015
    Posts:
    21
    Thanks a lot! :)
     
  6. taynaTinoco

    taynaTinoco

    Joined:
    Jul 31, 2018
    Posts:
    6
    I'm doing the same thing, the values on my RenderObjects change, but it doesn't update my view.
    Here is my code:

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         ExtractScriptableRendererData();
    4.     }
    5.  
    6.     private void LateUpdate()
    7.     {
    8.         if (Input.GetKeyDown(KeyCode.Space))
    9.             ChangeStencil();
    10.     }
    11.  
    12.     private void ExtractScriptableRendererData()
    13.     {
    14.         var pipeline = ((UniversalRenderPipelineAsset)GraphicsSettings.renderPipelineAsset);
    15.         FieldInfo propertyInfo = pipeline.GetType().GetField("m_RendererDataList", BindingFlags.Instance | BindingFlags.NonPublic);
    16.         _scriptableRendererData = ((ScriptableRendererData[])propertyInfo?.GetValue(pipeline))?[0];
    17.         renderObjects = (RenderObjects)_scriptableRendererData.rendererFeatures[0];
    18.         renderObjects.settings.stencilSettings.stencilCompareFunction = CompareFunction.Equal;
    19.        
    20.  
    21.     }
    22.  
    23.     private void ChangeStencil()
    24.     {
    25.          renderObjects.settings.stencilSettings.stencilCompareFunction = CompareFunction.NotEqual;      
    26.      
    27.     }
     
  7. IggyZuk

    IggyZuk

    Joined:
    Jan 17, 2015
    Posts:
    34
    When you modify a value inside of a ScriptableRendererFeature you have to call ScriptableRendererData.SetDirty(); to indicate that something was changed.

    Example:
    Code (CSharp):
    1. [SerializeField] ForwardRendererData rendererData;
    2.  
    3. var blurFeature = rendererData.rendererFeatures.OfType<MobileBlurUrp>().FirstOrDefault();
    4.  
    5. if (blurFeature == null) return;
    6.      
    7. blurFeature.settings.BlurAmount = value;
    8.  
    9. rendererData.SetDirty();
     
    tnhnbyr, ThinhHB and Benfont like this.
  8. marcosroque

    marcosroque

    Joined:
    Oct 31, 2016
    Posts:
    2
    Short and precise, worked perfectly. Thank you!
     
unityunity