Using the standard shader, how to get the current emission brigthness in code?
I know that I can create a HDR color by multiplying a base color by the brightness factor like that:
float brightnessFactor = 3f;
Color hdrColor = Color.red * brightnessFactor;
And, then, set this HDR color as emission in the standard shader like that:
material.SetColor("_EmissionColor", hdrColor);
But, if I get the emission color from the shader, how can I know the brightness value that was used to create the HDR color?
var hdrEmissionColor = material.GetColor("_EmissionColor");
var baseEmissionColor = ???
What I really want to know is how to get, by code, the brightness set on the HDR color dialog box in the editor.
Answer by JonathanCzeck · Dec 06, 2015 at 06:00 AM
There isn't an extra brightness value in the color, it's just UI sugar.
The brightness will be the highest value between R, G, and B. To get the base color, divide each color component by that highest value.
Hey, Jonathan!
Thanks for the insight. I created a small test script to test your explanation but I found it to be almost right. The problem with your solution is that by picking the highest component and dividing all of them by that one we normalize the HDR color back to a non-HDR state but the normalized color can differ from the original color if the original color brightness is less than 1.
Hard to explain in words, here it is an example:
An example that work:
Original Color: (0.0, 0.5, 1.0) // Original color brightness is 1 (one of the components must be equal to one)
Brightness: 6.35 // Any value greater that 1 to generate an HDR color.
Resulting HDR Color: (0.0, 3.175, 6.350) // Original Color * Brightness
Biggest Component: 6.350
Restored Color: (0.0, 0.5, 0.1) // Resulting HDR Color / Biggest Component
An example that doesn't work:
Original Color: (1.161, 0.422, 0.684) // Original color brightness is 0.684
Brightness: 6.35 // Any value greater that 1 to generate an HDR color.
Resulting HDR Color: (1.022, 2.682, 4.342) // Original Color * Brightness
Biggest Component: 4.342
Restored Color: (0.235, 0.618, 1) // Resulting HDR Color / Biggest Component
Here, take the script that I created to test this. The problem became easy to see when you set the brightness value to something greater that 1 and play with the original color:
[ExecuteInEdit$$anonymous$$ode]
public class ColorTest : $$anonymous$$onoBehaviour
{
[ColorUsage(true, true, 0, 8f, 0.125f, 3f)]
public Color originalColor = new Color(0.1f, 0.2f, 0.3f, 1f);
public float brightness = 3;
[Header("Calculated (don't change manually):")]
[ColorUsage(true, true, 0, 8f, 0.125f, 3f)]
public Color multipliedColor;
public float biggestValue;
[ColorUsage(true, true, 0, 8f, 0.125f, 3f)]
public Color restoredColor;
void Update()
{
multipliedColor = originalColor * brightness;
biggestValue = multipliedColor.r;
if (multipliedColor.g > biggestValue) biggestValue = multipliedColor.g;
if (multipliedColor.b > biggestValue) biggestValue = multipliedColor.b;
if (biggestValue > 1f)
{
restoredColor =
new Color(
multipliedColor.r / biggestValue,
multipliedColor.g / biggestValue,
multipliedColor.b / biggestValue
);
}
else
{
restoredColor = multipliedColor;
}
Debug.Log("---");
Debug.LogFormat("originalColor: {0}", originalColor);
Debug.LogFormat("hdrColor: {0}", multipliedColor);
Debug.LogFormat("biggestValue: {0}", biggestValue);
Debug.LogFormat("restoredColor: {0}", restoredColor);
}
}
Answer by Kayos · Jun 02, 2016 at 04:44 PM
"Color finalValue=Color.White*someValue; // someValue adjust the scale of emission"
I just tested this, simply multiplying the colour by someValue will increase the emission value.
Source: http://forum.unity3d.com/threads/standard-shader-emission-value.280153/
Your answer
Follow this Question
Related Questions
A public list of color in shader. 0 Answers
Critical issue with glitching standard shaders 1 Answer
Line Renderer displays black while using the Standard Shader on PC 0 Answers
Sprites/Cutout Shader ? 0 Answers
unity standard shader broken android 0 Answers