- Home /
Assigning HDR color to material property via script
I'm trying to assign an HDR emission color (with components with values greater than 1.0) to a material at runtime via script, but I'm not sure how to do this. I'm using a Color, but all my values get clamped to the usual 0-1 range. I can set a >1 value using the material inspector, but I don't seem to be able to accomplish the same effect via script at runtime. To be clear, I'm able to assign a color to the material, just not an HDR one with components outside the usual 0-1 range.
Any ideas?
I believe the Color type is always going to clamp the values - not sure there's anything you can do about that. Have you tried using a Vector4 for the property ins$$anonymous$$d of Color?
I tried that, but no dice. DynamicGI.SetEmissive() takes a Color as its second argument, so any Vector4 value is probably getting converted, and thus clamped.
Answer by pappu33 · Sep 04, 2019 at 05:58 AM
[ColorUsage(true, true)]
public Color myColor;
This lets you pick hdr color in inspector
Answer by robertseegrist · Sep 14, 2017 at 07:49 PM
I too had this issue.
I found the easiest way is:
public Material laserMaterial;
private Color laserRed = new Vector4 (1.988f, 0.438f, 0.438f, 1.0f);
void Start() {
laserMaterial.SetColor("_TintColor", laserRed);
.....
You can create as many Vector4 colors as you want and simply assign them using yourMaterial.SetColor("_TintColor", yourColor); anywhere in your script.
Answer by Raunchard · Nov 27, 2015 at 10:07 AM
To get the HDR color picker in the Inspector use ColorUsageAttribute. http://docs.unity3d.com/ScriptReference/ColorUsageAttribute.html
example: [ColorUsageAttribute(true,true,0f,8f,0.125f,3f)] public Color colour;
Answer by crazyrems · Jan 22, 2021 at 11:05 AM
In case you want to HDR colors in MaterialPropertyBlocks
, use Vector4
functions instead of Color
functions:
[ColorUsage(true, true)]
public Color hdr_col;
private MaterialPropertyBlock _propBlock;
hdr_col = (Color)_propBlock.GetVector("activeColor");
_propBlock.SetVector("activeColor", (Vector4)hdr_col);
It looks like SetColor
clamps the values between [0,1].
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
How to FadeIn 3dObject using alpha color? 1 Answer
Simple question about materials 1 Answer
Multiple Cars not working 1 Answer