Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to change HDR Color Intensity on Custom Material via script

Discussion in 'General Graphics' started by vandel212, Mar 16, 2021.

  1. vandel212

    vandel212

    Joined:
    May 10, 2020
    Posts:
    24
    Hi, I followed this Brackeys tutorial to add glow to one of my game objects, and it works well, but how do I change the intensity of material via script? I want the glow to slowly fade out. I don't see a way to access the intensity value like how the color picker adjusts it.

     
  2. sikth394

    sikth394

    Joined:
    Mar 17, 2020
    Posts:
    1
    Im stuck on the same problem dude, all the Mat.SetColor, Mat.SetFloat answers i come across don't help as i can't seem to reference this field properly :(
     
  3. AFAIK there is no intensity field in reality, it is just an editor field.
    In normal circumstances the components of a color can only be between 0 and 1.
    For HDR color you can set the color components higher. The intensity is the coefficient, So simply new Color( r * intensity, g * intensity, b * intensity).
     
    Zuukr and MaximeChaos like this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,396
    Internally HDR color values are just the equivalent of a RGB Vector3 that’s scaled over 1.0. If you pick a dark color and crank the intensity up high, then close and reopen the picker, it won’t show the same HSV / RGB or intensity values. It’ll be some set of 0.0 - 1.0 HSV / RGB values and intensity that match the previous values results, as the picker is recreating those values from the HDR RGB Vector3 values that already had the intensity baked in.

    https://forum.unity.com/threads/how-to-change-hdr-colors-intensity-via-shader.531861/#post-3501895
     
  5. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    462
    So, how to reference it / modify the intensity via script? I changed the material set color's RGB values to any number (also numbers between 0 and 1) and it doesn't seem to do anything, probably because I can't reference it correctly.
    upload_2022-2-11_2-25-25.png
     
  6. IDK, I don't do visual scripting. Maybe someone else can help you, probably in the visual scripting topic.
     
  7. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    You can access to the the material and changing properties easily in an IEnumerator.
    I used Vector4.Lerp to make a transition between the color properties.
    Here's an example:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gloom : MonoBehaviour
    6. {
    7.     private Material g;
    8.     Vector4 baseColor;
    9.     Vector4 offColor;
    10.     Vector4 currentColor;
    11.  
    12.     // Start is called before the first frame update
    13.  
    14.     private void Awake()
    15.     {
    16.         g = GetComponent<SpriteRenderer>().material;
    17.         baseColor = g.GetColor("_Color");
    18.         offColor = new Vector4(0, 0, 0, 1);
    19.     }
    20.     void Start()
    21.     {
    22.         StartCoroutine(gloom());
    23.  
    24.     }
    25.  
    26.     private IEnumerator gloom()
    27.     {
    28.         float i;
    29.         while (true)
    30.         {
    31.             i = 0;
    32.             while (i < 1)
    33.             {
    34.                
    35.                 currentColor = Vector3.Lerp(baseColor, offColor, i);
    36.                 print(currentColor);
    37.                 g.SetColor("_Color", currentColor);
    38.                 yield return new WaitForSeconds(.05f);
    39.                 i += 0.2f;
    40.                
    41.             }
    42.             i = 1;
    43.             g.SetColor("_Color", offColor);
    44.             yield return new WaitForSeconds(.1f);
    45.             while (i > 0)
    46.             {
    47.                
    48.                 currentColor = Vector3.Lerp(baseColor, offColor, i);
    49.                 print(currentColor);
    50.                 g.SetColor("_Color", currentColor);
    51.                 yield return new WaitForSeconds(.05f);
    52.                 i -= 0.2f;
    53.                
    54.             }
    55.             g.SetColor("_Color", baseColor);
    56.             yield return new WaitForSeconds(.5f);
    57.         }
    58.  
    59.     }
    60. }
     
    GuirieSanchez likes this.
  8. CaptLatinoAmerica

    CaptLatinoAmerica

    Joined:
    Aug 21, 2021
    Posts:
    2
    Damn, what a clean answer.THX!