Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved [SOLVED] Changing dof via script does not work.

Discussion in 'Universal Render Pipeline' started by Demencja, Apr 22, 2021.

  1. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    Any advice?

    Doesn't work in editor, playmode, and build.

    ezgif.com-gif-maker.gif


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using UnityEngine.Rendering.Universal;
    4.  
    5. [ExecuteInEditMode]
    6. public class AnimatedDof : MonoBehaviour
    7. {
    8.     [Header("Mode")]
    9.     public DepthOfFieldMode mode = DepthOfFieldMode.Off;
    10.     public bool overrideMode;
    11.  
    12.     [Header("Gaussian")]
    13.     public float start = 0f;
    14.     public bool overrideStart;
    15.     [Space]
    16.     public float end = 0f;
    17.     public bool overrideEnd;
    18.     [Space]
    19.     [Range(0.5f, 1.5f)]
    20.     public float maxRadius = 0.5f;
    21.     public bool overrideMaxRadius;
    22.     [Space]
    23.     public bool highQualitySampling;
    24.     public bool overrideHighQualitySampling;
    25.  
    26.     [Header("Bokeh")]
    27.     [Min(0.1f)]
    28.     public float focusDistance = 0.1f;
    29.     public bool overrideFocusDistance;
    30.     [Space]
    31.     [Range(1f, 300f)]
    32.     public float focalLength = 1f;
    33.     public bool overrideFocalLength;
    34.     [Space]
    35.     [Range(1f, 32f)]
    36.     public float aperture = 1f;
    37.     public bool overrideAperture;
    38.     [Space]
    39.     [Range(3, 9)]
    40.     public int bladeCount = 3;
    41.     public bool overrideBladeCount;
    42.     [Space]
    43.     [Range(0f, 1f)]
    44.     public float bladeCurvature = 0f;
    45.     public bool overrideBladeCurvature;
    46.     [Space]
    47.     [Range(-180f, 180f)]
    48.     public float bladeRotation = -180f;
    49.     public bool overrideBladeRotation;
    50.  
    51.     private Volume volume;
    52.     private DepthOfField testDoF;
    53.  
    54.     private void Awake()
    55.     {
    56.         volume = gameObject.GetComponent<Volume>();
    57.         volume.profile.TryGet(out testDoF);
    58.     }
    59.  
    60.     private void UpdateDof()
    61.     {
    62.         if (testDoF != null && overrideMode)
    63.         {
    64.             testDoF.mode = new DepthOfFieldModeParameter(mode, overrideMode);
    65.  
    66.             if (mode == DepthOfFieldMode.Gaussian)
    67.             {
    68.                 testDoF.gaussianStart = new MinFloatParameter(start, 0f, overrideStart);
    69.                 testDoF.gaussianEnd = new MinFloatParameter(end, 0f, overrideEnd);
    70.                 testDoF.gaussianMaxRadius = new ClampedFloatParameter(maxRadius, 0.5f, 1.5f, overrideMaxRadius);
    71.                 testDoF.highQualitySampling = new BoolParameter(highQualitySampling, overrideHighQualitySampling);
    72.             }
    73.             else if (mode == DepthOfFieldMode.Bokeh)
    74.             {
    75.                 testDoF.focusDistance = new MinFloatParameter(focusDistance, 0f, overrideFocusDistance);
    76.                 testDoF.focalLength = new ClampedFloatParameter(focalLength, 1f, 300f, overrideFocalLength);
    77.                 testDoF.aperture = new ClampedFloatParameter(aperture, 1f, 32f, overrideAperture);
    78.                 testDoF.bladeCount = new ClampedIntParameter(bladeCount, 3, 9, overrideBladeCount);
    79.                 testDoF.bladeCurvature = new ClampedFloatParameter(bladeCurvature, 0f, 1f, overrideBladeCurvature);
    80.                 testDoF.bladeRotation = new ClampedFloatParameter(bladeRotation, -180f, 180f, overrideBladeRotation);
    81.             }
    82.         }
    83.     }
    84.  
    85.     // Update is called once per frame
    86.     void Update()
    87.     {
    88.         UpdateDof();
    89.     }
    90. }
    91.  
    92.  
     
  2. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,791
    Before each property use overrideState= true
    testDoF.gaussianStar.overrideState = true
     
  3. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    this don't work too.

    Code (CSharp):
    1.                 testDoF.focusDistance.overrideState = true;
    2.                 testDoF.focusDistance = new MinFloatParameter(focusDistance, 0f, overrideFocusDistance);
     
  4. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    This work!

    testDoF.focusDistance.overrideState = true;
    testDoF.focusDistance.value = focusDistance;
     
  5. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    Final script if some one want.

    Code (CSharp):
    1. public class AnimatedDof : MonoBehaviour
    2. {
    3.     [Header("Mode")]
    4.     public DepthOfFieldMode mode = DepthOfFieldMode.Off;
    5.     public bool overrideMode;
    6.  
    7.     [Header("Gaussian")]
    8.     public float start = 0f;
    9.     public bool overrideStart;
    10.     [Space]
    11.     public float end = 0f;
    12.     public bool overrideEnd;
    13.     [Space]
    14.     [Range(0.5f, 1.5f)]
    15.     public float maxRadius = 0.5f;
    16.     public bool overrideMaxRadius;
    17.     [Space]
    18.     public bool highQualitySampling;
    19.     public bool overrideHighQualitySampling;
    20.  
    21.     [Header("Bokeh")]
    22.     [Min(0.1f)]
    23.     public float focusDistance = 0.1f;
    24.     public bool overrideFocusDistance;
    25.     [Space]
    26.     [Range(1f, 300f)]
    27.     public float focalLength = 1f;
    28.     public bool overrideFocalLength;
    29.     [Space]
    30.     [Range(1f, 32f)]
    31.     public float aperture = 1f;
    32.     public bool overrideAperture;
    33.     [Space]
    34.     [Range(3, 9)]
    35.     public int bladeCount = 3;
    36.     public bool overrideBladeCount;
    37.     [Space]
    38.     [Range(0f, 1f)]
    39.     public float bladeCurvature = 0f;
    40.     public bool overrideBladeCurvature;
    41.     [Space]
    42.     [Range(-180f, 180f)]
    43.     public float bladeRotation = -180f;
    44.     public bool overrideBladeRotation;
    45.  
    46.     private Volume volume;
    47.     private DepthOfField testDoF;
    48.  
    49.     private void UpdateDof()
    50.     {
    51.         if (volume == null)
    52.         {
    53.             volume = gameObject.GetComponent<Volume>();
    54.         }
    55.         if (volume != null && testDoF == null)
    56.         {
    57.             volume.profile.TryGet(out testDoF);
    58.         }
    59.  
    60.         if (testDoF != null && overrideMode)
    61.         {
    62.             testDoF.mode.overrideState = overrideMode;
    63.             testDoF.mode.value = this.mode;
    64.  
    65.             if (mode == DepthOfFieldMode.Gaussian)
    66.             {
    67.                 testDoF.gaussianStart.overrideState = overrideStart;
    68.                 testDoF.gaussianStart.value = start;
    69.  
    70.                 testDoF.gaussianEnd.overrideState = overrideEnd;
    71.                 testDoF.gaussianEnd.value = end;
    72.  
    73.                 testDoF.gaussianMaxRadius.overrideState = overrideMaxRadius;
    74.                 testDoF.gaussianMaxRadius.value = maxRadius;
    75.  
    76.                 testDoF.highQualitySampling.overrideState = overrideHighQualitySampling;
    77.                 testDoF.highQualitySampling.value = highQualitySampling;
    78.             }
    79.             else if (mode == DepthOfFieldMode.Bokeh)
    80.             {
    81.                 testDoF.focusDistance.overrideState = overrideFocusDistance;
    82.                 testDoF.focusDistance.value = focusDistance;
    83.  
    84.                 testDoF.focalLength.overrideState = overrideFocalLength;
    85.                 testDoF.focalLength.value = focalLength;
    86.  
    87.                 testDoF.aperture.overrideState = overrideAperture;
    88.                 testDoF.aperture.value = aperture;
    89.  
    90.                 testDoF.bladeCount.overrideState = overrideBladeCount;
    91.                 testDoF.bladeCount.value = bladeCount;
    92.  
    93.                 testDoF.bladeCurvature.overrideState = overrideBladeCurvature;
    94.                 testDoF.bladeCurvature.value = bladeCurvature;
    95.  
    96.                 testDoF.bladeRotation.overrideState = overrideBladeRotation;
    97.                 testDoF.bladeRotation.value = bladeRotation;
    98.             }
    99.         }
    100.     }
    101.  
    102.     // Update is called once per frame
    103.     void Update()
    104.     {
    105.         UpdateDof();
    106.     }
    107. }
     
    Last edited: Apr 22, 2021