-
Notifications
You must be signed in to change notification settings - Fork 0
/
KM_Mix.osl
105 lines (87 loc) · 2.87 KB
/
KM_Mix.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
/* ------------------------------------------------------------------------- *
*
* Copyright (C) 2024 Jake Kurtz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ------------------------------------------------------------------------- */
#include "spectral.h"
void kubelka_munk(float l, float f, float n, float s, color col_a, color col_b, output float R)
{
float k1 = pow2((n - 1.0) / (n + 1.0));
float k2 = clamp(s, 0.0, 1.0 - k1);
float Rm_a = sRGB_to_SPEC(col_a, l);
float Rm_b = sRGB_to_SPEC(col_b, l);
float q = 1.0 - k1 - k2;
float R_a = k1 + (max( Rm_a - k1, 0.0) / (q + k2*Rm_a));
float R_b = k1 + (max( Rm_b - k1, 0.0) / (q + k2*Rm_b));
float K_a = pow2(1.0 - R_a) / max((2.0*R_a), .05);
float K_b = pow2(1.0 - R_b) / max((2.0*R_b), .05);
float K = mix(K_a, K_b, f);
R = 1. + K - sqrt(K*K + 2.0*K);
}
shader Kubelka_Munk_Mix
(
/* -------------------------------------------------------------------------- */
/* Inputs */
/* -------------------------------------------------------------------------- */
float f = .5
[[
string help = "",
string label = "Factor",
float min = 0,
float max = 1
]],
float n = 1.48
[[
string help = "",
string label = "IOR",
float min = 0,
float max = 1
]],
float s = .2
[[
string help = "",
string label = "Internal Reflectance",
float min = 0,
float max = 1
]],
color color_a = .5
[[
string help = "",
string label = "A"
]],
color color_b = .5
[[
string help = "",
string label = "B"
]],
/* -------------------------------------------------------------------------- */
/* Outputs */
/* -------------------------------------------------------------------------- */
output color R = 0
[[
string help = ""
]]
)
{
float l[LAMBDA_SAMPLES];
gen_lambda_samples(P, l);
float R_spec[LAMBDA_SAMPLES];
for (int i = 0; i < LAMBDA_SAMPLES; i++)
{
kubelka_munk(l[i], f, n, s, color_a, color_b, R_spec[i]);
}
R = SPEC_to_sRGB(R_spec, l);
}