Search Unity

How to set color filter through script

Discussion in 'Universal Render Pipeline' started by ohnocall911, Oct 21, 2021.

  1. ohnocall911

    ohnocall911

    Joined:
    Apr 18, 2019
    Posts:
    1
    Hey guys, I've had some issues getting a day/night cycle going on.

    I have a main time script below that checks the hours and is supposed to apply a color filter to the PostProcessingVolume. Found a couple similar threads for adjusting values through floats in the forums but none related to setting the color.

    Here's what I have. Tried a couple different variations with no success:

    Code (CSharp):
    1. public class TimeSystem : MonoBehaviour
    2. {
    3.     public Volume volume;
    4.     private ColorAdjustments colorAdjustments;
    5.  
    6. /*
    7. Commented out since this didn't work either
    8.  
    9.     Color earlyMorningColor = new Color(253, 127, 127);
    10.     Color morningColor = new Color(255, 255, 255);
    11.     Color afternoonColor = new Color(236, 236, 236);
    12.     Color eveningColor = new Color(176, 197, 197);
    13.     Color midnightColor = new Color(121, 125, 166);
    14.  
    15.     public Gradient dayLight;
    16.  
    17.     public Color earlyMorningShade;
    18.     public Color morningShade;
    19.     public Color afternoonShade;
    20.     public Color eveningShade;
    21.     public Color midnightShade;
    22.     */
    23.  
    24. ...
    25.  
    26. void Start()
    27.     {
    28.         volume = GetComponent<Volume>();
    29.         volume.profile.TryGet(out ColorAdjustments colorAdjustments);
    30.     }
    31.  
    32.  
    33. void CalculateTimePeriods()
    34.     {
    35.        
    36.  
    37.         if (hour == 3 || hour == 4 || hour == 5 || hour == 6)
    38.         {
    39.  
    40.             //volume.profile.TryGetSettings(out colorEarlyMorning);
    41.             //colorEarlyMorning.colorFilter.value = earlyMorningColor;
    42.             colorAdjustments.colorFilter.value = Color.HSVToRGB(253, 127, 127);
    43.         }
    44. }
    45.  
     
  2. TheBossaaaa

    TheBossaaaa

    Joined:
    Nov 19, 2022
    Posts:
    9
    No one has answer to this?
     
  3. oliverschnabel

    oliverschnabel

    Unity Technologies

    Joined:
    Mar 13, 2018
    Posts:
    41
    In the Color class in Unity, each color component is a floating point value with a range from 0 to 1. If you use the values provided above, you get way too high intensity on the HDR Color of the ColorFilter.

    So you need to devide the numbers by 255 to get them into the correct range. E.g. the following script assigns the correct earlyMorningColor when you press T in Play Mode.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.Universal;
    6.  
    7. public class ColorFilterAdjustment : MonoBehaviour
    8. {
    9.  
    10.     public Volume volume;
    11.     private ColorAdjustments colorAdjustments;
    12.  
    13.     private Color earlyMorningColor = new Color(253.0f / 255.0f, 127.0f / 255.0f, 127.0f / 255.0f);
    14.  
    15.     void Update()
    16.     {
    17.         if(volume == null)
    18.             return;
    19.  
    20.         if(Input.GetKeyUp(KeyCode.T)) {
    21.             volume.profile.TryGet(out ColorAdjustments colorAdjustments);
    22.  
    23.             if(colorAdjustments != null) {
    24.                 colorAdjustments.colorFilter.value = earlyMorningColor;
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    I just tested it in the new URP Sample and this is the result that I got in the Terminal Scene:

    upload_2024-1-9_9-6-32.png

    The Volume that is assigned needs to have the ColorFilter already activated in this example:

    upload_2024-1-9_9-10-12.png