-
Notifications
You must be signed in to change notification settings - Fork 1
/
labpbr.osl
executable file
·117 lines (102 loc) · 4.56 KB
/
labpbr.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#define EPSILON (1e-6)
float ior(float f0)
{
float sqrt_f0 = sqrt(max(f0, 0.02));
return (1.0 + sqrt_f0) / max(1.0 - sqrt_f0, EPSILON);
}
color f0(color n, color k)
{
color k2 = max(k * k, color(1e-6));
color r = ((n - 1) * (n - 1) + k2) / ((n + 1) * (n + 1) + k2);
return sqrt(r);
}
color reconstruct_normal(float x, float y)
{
float f = 0.0;
vector t = vector(x, y, 0.0);
// Bring to [-1, 1] range
t -= 0.5;
t *= 2;
t.z = 0;
f = sqrt(1.0 - dot(t, t));
return vector(x, 1 - y, (f / 2) + 0.5);
}
shader labpbr_to_principled_bsdf(
color inAlbedo = color(0.0) [[ string label = "Albedo Texture" ]],
float inAlbedo_a = 0.0 [[ string label = "Albedo Texture Alpha" ]],
color inNormal = color(0.0) [[ string label = "Normal Texture" ]],
float inNormal_a = 0.0 [[ string label = "Normal Texture Alpha"]],
color inSpecular = color(0.0) [[ string label = "Specular Texture" ]],
float inSpecular_a = 0.0 [[ string label = "Specular Texture Alpha" ]],
float inMetalMixFactor = 0.5 [[ string label = "HCM Mix Factor", string help = "Controls the Mix factor for Hardcoded Metals between F0 and Albedo", string widget = "slider", float min = 0.0, float max = 1.0 ]],
output color outAlbedo = 0.0 [[ string label = "Base Color" ]],
output float outSSS = 0.0 [[ string label = "Subsurface" ]],
output color outSSC = 0.0 [[ string label = "Subsurface Color" ]],
output float outMetallic = 0.0 [[ string label = "Metallic" ]],
output float outSpecular = 0.0 [[ string label = "Specular" ]],
output float outRoughness = 0.0 [[ string label = "Roughness" ]],
output float outIoR = 1.450 [[ string label = "IOR"]],
output float outTransmission = 0.0 [[ string label = "Transmission" ]],
output color outEmission = 0.0 [[ string label = "Emission" ]],
output float outEmissionStrength = 0.0 [[ string label = "Emission Strength"]],
output float outAlpha = 1.0 [[ string label = "Alpha" ]],
output color outNormal = 0.0 [[ string label = "Normal" ]],
output float outHeightmap = 0.0 [[ string label = "Heightmap" ]],
)
{
color albedo = color(0.0);
color F0 = color(0.0);
float metallic = 1.0;
int is_metal = (int)(255 * inSpecular.g);
// is_metal = 254;
if (is_metal < 230) {
float sss_offset = 65.0 / 255.0;
outSSS = inSpecular.b < sss_offset ? 0.0 : (inSpecular.b - sss_offset) / (1 - sss_offset);
outSSC = inAlbedo;
// Bake Ambient Occclusion
albedo = inAlbedo * inNormal.b;
// Reflectivity
outSpecular = inSpecular.g;
outIoR = ior(inSpecular.g);
metallic = 0.0;
} else if (is_metal > 254) {
// Albedo is F0
albedo = inAlbedo;
metallic = 1.0;
} else {
// Signal out of bounds indexed metals, or place your custom N, K values here & uncomment is_metal = 254 above.
color n = color(1.0, 0.0, 0.0);
color k = color(2.0, 1.0, 0.5);
if (is_metal == 230) { // Iron
n = color(2.9114, 2.9497, 2.5845); k = color(3.0893, 2.9318, 2.7670); }
else if (is_metal == 231) { // Gold
n = color(0.18299, 0.42108, 1.3734); k = color(3.4242, 2.3459, 1.7704); }
else if (is_metal == 232) { // Aluminium
n = color(1.3456, 0.96521, 0.61722); k = color(7.4746, 6.3995, 5.3031); }
else if (is_metal == 233) { // Chrome
n = color(3.1071, 3.1812, 2.3230); k = color(3.3314, 3.3291, 3.1350); }
else if (is_metal == 234) { // Copper
n = color(0.27105, 0.67693, 1.3164); k = color(3.6092, 2.6248, 2.2921); }
else if (is_metal == 235) { // Lead
n = color(1.9100, 1.8300, 1.4400); k = color(3.5100, 3.4000, 3.1800); }
else if (is_metal == 236) { // Platinum
n = color(2.3757, 2.0847, 1.8453); k = color(4.2655, 3.7153, 3.1365); }
else if (is_metal == 237) { // Silver
n = color(0.15943, 0.14512, 0.13547); k = color(3.9291, 3.1900, 2.3808); }
F0 = f0(n, k);
albedo = mix(inAlbedo, F0, inMetalMixFactor);
metallic = 1.0;
}
outAlbedo = albedo;
outMetallic = metallic;
// Roughness (convert from perceptual smoothness)
outRoughness = pow(1.0 - inSpecular.r, 2.0);
if (inAlbedo_a < 1 && inAlbedo_a > 0)
outTransmission = 1.0;
else
outAlpha = inAlbedo_a;
outEmission = inAlbedo;
outEmissionStrength = inSpecular_a < 1.0 ? (inSpecular_a / (254.0 / 255.0)) : 0.0;
outNormal = reconstruct_normal(inNormal.r, inNormal.g);
outHeightmap = mix(0.75, 1.0, inNormal_a);
}