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

How do I change the HDRP Lit Material opacity at runtime?

Discussion in 'Graphics Experimental Previews' started by Ehredt, Jul 30, 2018.

  1. Ehredt

    Ehredt

    Joined:
    Jun 20, 2018
    Posts:
    79
    With standard materials, I was able to use a UI slider to change the material's alpha property. However, with the HDRP/Lit material, I can't do that using Color. I found that with an animation, I can change the material's opacity using the alpha channel on "Base Color" instead of "Color," but how do I access Base Color via a script? I can't find it in the documentation or anything. It feels like I'm missing something very simple.

    Thanks in advance!
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    https://docs.unity3d.com/ScriptReference/Material.SetColor.html
    https://docs.unity3d.com/ScriptReference/Material.SetVector.html

    Use with _BaseColor

    Right click material -> Edit to see the variable names, eg.
    Code (CSharp):
    1. Shader "HDRenderPipeline/Lit"
    2. {
    3.     Properties
    4.     {
    5.         // Versioning of material to help for upgrading
    6.         [HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
    7.  
    8.         // Following set of parameters represent the parameters node inside the MaterialGraph.
    9.         // They are use to fill a SurfaceData. With a MaterialGraph this should not exist.
    10.  
    11.         // Reminder. Color here are in linear but the UI (color picker) do the conversion sRGB to linear
    12.         _BaseColor("BaseColor", Color) = (1,1,1,1)
    13.         _BaseColorMap("BaseColorMap", 2D) = "white" {}
    14.  
    15.         _Metallic("_Metallic", Range(0.0, 1.0)) = 0
    16.         _Smoothness("Smoothness", Range(0.0, 1.0)) = 1.0
    17.         _MaskMap("MaskMap", 2D) = "white" {}
    18.         _SmoothnessRemapMin("SmoothnessRemapMin", Float) = 0.0
    19.         _SmoothnessRemapMax("SmoothnessRemapMax", Float) = 1.0
    20.         _AORemapMin("AORemapMin", Float) = 0.0
    21.         _AORemapMax("AORemapMax", Float) = 1.0
    22.  
    23.         _NormalMap("NormalMap", 2D) = "bump" {}     // Tangent space normal map
    24.         _NormalMapOS("NormalMapOS", 2D) = "white" {} // Object space normal map - no good default value
    25.         _NormalScale("_NormalScale", Range(0.0, 2.0)) = 1
    26.  
    27.         _BentNormalMap("_BentNormalMap", 2D) = "bump" {}
    28.         _BentNormalMapOS("_BentNormalMapOS", 2D) = "white" {}
    29.  
    30.         _HeightMap("HeightMap", 2D) = "black" {}
    31.         // Caution: Default value of _HeightAmplitude must be (_HeightMax - _HeightMin) * 0.01
    32.         // Those two properties are computed from the ones exposed in the UI and depends on the displaement mode so they are separate because we don't want to lose information upon displacement mode change.
    33.         [HideInInspector] _HeightAmplitude("Height Amplitude", Float) = 0.02 // In world units. This will be computed in the UI.
    34.         [HideInInspector] _HeightCenter("Height Center", Range(0.0, 1.0)) = 0.5 // In texture space
    35.  
    36.         [Enum(MinMax, 0, Amplitude, 1)] _HeightMapParametrization("Heightmap Parametrization", Int) = 0
    37.         // These parameters are for vertex displacement/Tessellation
    38.         _HeightOffset("Height Offset", Float) = 0
    39.         // MinMax mode
    40.         _HeightMin("Heightmap Min", Float) = -1
    41.         _HeightMax("Heightmap Max", Float) = 1
    42.         // Amplitude mode
    43.         _HeightTessAmplitude("Amplitude", Float) = 2.0 // in Centimeters
    44.         _HeightTessCenter("Height Center", Range(0.0, 1.0)) = 0.5 // In texture space
    45.  
    46.         // These parameters are for pixel displacement
    47.         _HeightPoMAmplitude("Height Amplitude", Float) = 2.0 // In centimeters
    48.  
    49.         _DetailMap("DetailMap", 2D) = "black" {}
    50.         _DetailAlbedoScale("_DetailAlbedoScale", Range(0.0, 2.0)) = 1
    51.         _DetailNormalScale("_DetailNormalScale", Range(0.0, 2.0)) = 1
    52.         _DetailSmoothnessScale("_DetailSmoothnessScale", Range(0.0, 2.0)) = 1
    53.  
    54.         _TangentMap("TangentMap", 2D) = "bump" {}
    55.         _TangentMapOS("TangentMapOS", 2D) = "white" {}
    56.         _Anisotropy("Anisotropy", Range(-1.0, 1.0)) = 0
    57.         _AnisotropyMap("AnisotropyMap", 2D) = "white" {}
    58.  
    59.         _DiffusionProfile("Diffusion Profile", Int) = 0
    60.         _SubsurfaceMask("Subsurface Radius", Range(0.0, 1.0)) = 1.0
    61.         _SubsurfaceMaskMap("Subsurface Radius Map", 2D) = "white" {}
    62.         _Thickness("Thickness", Range(0.0, 1.0)) = 1.0
    63.         _ThicknessMap("Thickness Map", 2D) = "white" {}
    64.         _ThicknessRemap("Thickness Remap", Vector) = (0, 1, 0, 0)
    65.  
    66.         _IridescenceThickness("Iridescence Thickness", Range(0.0, 1.0)) = 1.0
    67.         _IridescenceThicknessMap("Iridescence Thickness Map", 2D) = "white" {}
    68.         _IridescenceThicknessRemap("Iridescence Thickness Remap", Vector) = (0, 1, 0, 0)
    69.         _IridescenceMask("Iridescence Mask", Range(0.0, 1.0)) = 1.0
    70.         _IridescenceMaskMap("Iridescence Mask Map", 2D) = "white" {}
    71.  
    72.         _CoatMask("Coat Mask", Range(0.0, 1.0)) = 0.0
    73.         _CoatMaskMap("CoatMaskMap", 2D) = "white" {}
    74.  
    75.         [ToggleUI] _EnergyConservingSpecularColor("_EnergyConservingSpecularColor", Float) = 1.0
    76.         _SpecularColor("SpecularColor", Color) = (1, 1, 1, 1)
    77.         _SpecularColorMap("SpecularColorMap", 2D) = "white" {}
    Note: accessing .material at runtime will cause Unity to replace the given material with a copy for that runtime session. Use .sharedMaterial at runtime to change the original material on a temporary basis.

    Don't modify them with script at editor time unless you want to make changes permanent.
     
  3. Ehredt

    Ehredt

    Joined:
    Jun 20, 2018
    Posts:
    79
    Thanks! That was exactly what I needed.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Or add PerRendererData attribute in "com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader" and use MaterialPropertyBlock's for editing colors of many objects without creating new instances. But i'm not tested MPB in HDRP.
     
  5. AviGarud

    AviGarud

    Joined:
    Feb 14, 2019
    Posts:
    2
    can you please explain how did you do it. I am trying to change the real time material using HDRP. However I dont know how you solved it. I am new in unity therefore please explain in detains or with picture if you can.
     
  6. AviGarud

    AviGarud

    Joined:
    Feb 14, 2019
    Posts:
    2
    I solved it. this is how you change the material color in HDRP


    //Set the main Color of the Material to green
    rend.material.shader = Shader.Find("_BaseColor");
    rend.material.SetColor("_BaseColor", Color.green);
     
  7. OfficialHermie

    OfficialHermie

    Joined:
    Oct 12, 2012
    Posts:
    585
    @AviGarud Your code doesn't work for me, resulting in an ShaderError and pink material. Can you post a full / proper code, please?
     
  8. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    613
    Frankly, this code is confusing, even though the docs show this in their example.
    https://docs.unity3d.com/ScriptReference/Material.SetColor.html
    @OfficialHermie , Shader.Find() is the same as GameObject .Find() in that you insert a string and it returns the specified shader. If it can't find one, it returns an Internal Shader Error (which, of course, turns your material pink, indicating the material cannot be rendered.) (Possibly incorrect wording of the error but you get the idea.)

    Shader.Find("Specular") makes more since, since there is indeed a material called "Specular", but it changes your entire shader and, to my best knowledge, resets it to default.

    So just remove the first line and keep it as "rend.material.SetColor("_BaseColor", Color.green);"



    Oh, and uuuh; if you are not using HDRP, then use "_Color" instead. As the moderator said, Right click shader -> Edit shader to see the variable names and find the name of the variable you want to change there (my respects to that glorious trick, kind sir.)
     
  9. jwheeler_unity

    jwheeler_unity

    Unity Technologies

    Joined:
    Nov 2, 2018
    Posts:
    7
    I'm not sure how this worked for you. Shader.Find is meant to find a shader, not a parameter of a shader.

    If you use Shader.Find("HDRP/Lit") then that should find the shader you want and assign it to the material. You can then use SetColor to set the _BaseColor property.
     
  10. FutureWang

    FutureWang

    Joined:
    May 24, 2018
    Posts:
    22
    Is there any way to get the HDRP/Lit's Surface Option by code? I want to find out the materials that "material type " is "SubsurfaceScattering" and "_DiffusionProfile" is null.
    upload_2021-7-23_17-18-16.png
     

    Attached Files:

  11. Cor1979

    Cor1979

    Joined:
    Oct 4, 2009
    Posts:
    65
    Did you figure this out? I'm looking for the same thing
     
  12. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    Does anyone know how to really make this work? I have a technique that works in Unity Built-in pipeline, but when used in HDRP, the material's alpha property will not change under normal circumstances. For example, I am lerping the alpha value from 0 to 1 so that it will fade in. But what I get as a result is that it just shows the opaque object. However, if I pause the editor and click on the material component in the inspector then it instantly updates to the correct alpha value. Why won't it update on its own? And is there some way to force it to refresh when I set the alpha?
     
  13. ican0

    ican0

    Joined:
    Jun 28, 2021
    Posts:
    1
    Did you ever find a fix for this ?
     
  14. magique

    magique

    Joined:
    May 2, 2014
    Posts:
    4,030
    I believe it is working for me and has been for a while. However, I don't recall what I did to make it work.