forked from Maxon-Computer/Redshift-OSL-Shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuma_Shader.osl
78 lines (76 loc) · 2.46 KB
/
Luma_Shader.osl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Adrian Cruceru
// Redshift Rendering Technologies 2020
// This file is licensed under Apache 2.0 license
// luma
shader Luma
[[ string help = "computes the relative luminance = luma of an input in various formats",
string label = "Luma/Relative Luminance" ]]
(
// inputs
color inColor = 0
[[string label = "In Color"]],
// various modes to compute the relative Luminance
int mode = 0
[[ string label = "Mode",
string widget = "mapper",
string options = "Rec. 709 (HD):0|Rec. 2020 (4k):1|Ccir 601 (SD):2|Average:3|Red:4|Green:5|Blue:6"]],
float gamma = 2.2
[[ string label = "Gamma",
float min = 0, float max = 3 ]],
int preGamma = 1
[[ string widget = "checkBox",
string label = "Pre Gamma" ]],
int postGamma = 1
[[ string widget = "checkBox" ,
string label = "Post Gamma" ]],
// outputs
output color outColor = 1
[[ string label = "Out Color" ]]
)
{
// define various luma standards
vector rec709 = {0.2126, 0.7152, 0.0722};
vector rec2020 = {0.2627, 0.678, 0.0593};
vector ccir601 = {0.299, 0.587, 0.114};
float R = preGamma*pow(inColor[0],1/gamma)+(1-preGamma)*inColor[0];
float G = preGamma*pow(inColor[1],1/gamma)+(1-preGamma)*inColor[1];
float B = preGamma*pow(inColor[2],1/gamma)+(1-preGamma)*inColor[2];
if (mode == 0)
{
// internally the luminance function is using REC709
// outColor = luminance(inColor);
color C = rec709[0]*R +rec709[1]*G+rec709[2]*B;
color powC = pow(C,gamma);
outColor = postGamma*powC+(1-postGamma)*C;
}
else if (mode == 1)
{
color C = rec2020[0]*R+rec2020[1]*G+rec2020[2]*B;
color powC = pow(C,gamma);
outColor = postGamma*powC+(1-postGamma)*C;
}
else if (mode == 2)
{
color C = ccir601[0]*R+ccir601[1]*G+ccir601[2]*B;
color powC = pow(C,gamma);
outColor = postGamma*powC+(1-postGamma)*C;
}
else if (mode == 3)
{
color C = (R+G+B)/3;
color powC = pow(C,gamma);
outColor = postGamma*powC+(1-postGamma)*C;
}
else if (mode == 4)
{
outColor = postGamma*pow(R,gamma)+(1-postGamma)*R;
}
else if (mode == 5)
{
outColor = postGamma*pow(G,gamma)+(1-postGamma)*G;
}
else if (mode == 6)
{
outColor = postGamma*pow(B,gamma)+(1-postGamma)*B;
}
}