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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
Shave and a Haircut emulation shader (c) 2019 Epic Games
by Joe Alter
Illuminance fixes by Bernard Edlington, updated by David Lin, AOVs and secondary spec by John Helms
Last updated: 03/08/2012
*/
normal pxslUtilShadingNormal(normal n;) {
normal Ns = normalize(n);
extern vector I;
uniform float sides = 2;
uniform float raydepth;
attribute("Sides", sides);
rayinfo("depth", raydepth);
if (sides == 2 || raydepth > 0) Ns = faceforward(Ns, I, Ns);
return Ns;
}
surface Shave (
float Ka = 0,
SHAVEambdiff = 0.6,
SHAVEspec = 0.35,
SHAVEgloss = 0.07,
SHAVEopacity = 1,
SHAVEselfshad=1,
coneangle = 30; /* note to Joe: this parameter should be converted from the SHAVEgloss primvar */
color SHAVEspec_color=1,
SHAVEspec_color2 = 1,
rootcolor=1,
tipcolor=1;
/* AOVs commented ones not yet supported */
output varying
color Ambient=0,
// Backscattering=0,
DiffuseColor=0,
DiffuseDirect=0,
DiffuseDirectShadow=0,
DiffuseEnvironment=0,
DiffuseIndirect=0,
// Incandescence=0,
OcclusionDirect=0,
OcclusionIndirect=0,
// Refraction=0,
// Rim=0,
SpecularColor=0,
SpecularDirect=0,
SpecularDirectShadow=0;
// SpecularEnvironment=0,
// SpecularIndirect=0,
// Subsurface=0,
// Translucence=0;
)
{
normal Ns = pxslUtilShadingNormal(N);
normal Nn = normalize(N);
vector In = normalize(I);
// secondary spec variables
float bendTangentAmt = 0.35; /* use values between 0 and 1, less than 0 is off */
float secondarySHAVEglossMult = 0.7; /* multiply SHAVEgloss for secondary spec gloss value, less than 1.0 is more broad */
vector T = normalize (dPdv), /* tangent along length of hair */
// Tu = normalize (mix(dPdv,-vector(-Nn),bendTangentAmt)), /* bent tangent up along length of hair */
V = -normalize(I), /* V is the view vector */
Tu = normalize (mix(dPdv,-vector(V),bendTangentAmt)); /* bent tangent up along length of hair */
float diffK = 0,
VdotT = V . T,
VdotTSq = VdotT * VdotT,
sq2 = 1;
float VdotTu = V . Tu,
VdotTSqU = VdotTu * VdotTu,
sq3 = 1;
color Clocal = 1,
inshadow,
Cl_noshd,
Cl_diff;
color SpecularDirectD = 0,
SpecularDirectU = 0,
SpecularDirectShadowD = 0,
SpecularDirectShadowU = 0;
if(VdotTSq < 1)
sq2 = sqrt(1 - VdotTSq);
if(VdotTSqU < 1)
sq3 = sqrt(1 - VdotTSqU);
// illumination from direct lights
// here we:
// - add to our total diffuse contribution
// - tally direct occlusion (shadow)
// - tally (unoccluded) direct illumination
illuminance("-environment", P, "lightcache", "reuse")
{
vector Ln = normalize(L);
float nondiff = 0, rawDiff, rawDiffu;
color Cselfshad;
Cselfshad = Cl * SHAVEselfshad + (1 - SHAVEselfshad);
lightsource("__nondiffuse", nondiff);
if (nondiff < 1)
{
rawDiff = (1-nondiff) * sqrt(1 - pow((T . Ln), 2));
rawDiffu = (1-nondiff) * sqrt(1 - pow((Tu . Ln), 2));
diffK = mix(1, rawDiff, SHAVEambdiff);
if( 0 != lightsource("_shadow", inshadow) )
OcclusionDirect += inshadow;
if( 0 == lightsource("_cl_noshadow", Cl_noshd) )
Cl_noshd = Cselfshad;
Cl_diff = Cl_noshd - Cselfshad;
DiffuseDirect += diffK * Cl_noshd;
DiffuseDirectShadow += diffK * Cl_diff;
}
float nonspec = 0;
color inshadow = 0, Cl_noshd = 0, Cl_diff;
lightsource("__nonspecular", nonspec);
if (nonspec < 1)
{
float specK = (1-nonspec) * rawDiff * sq2 - (Ln . T ) * VdotT;
specK = pow (specK, 1 / ( 1 * ( 0.101 - SHAVEgloss ) ) );// * 0.5;
float specK2 = (1-nonspec) * rawDiffu * sq3 - (Ln . Tu ) * VdotTu;
specK2 = pow (specK2, 1 / ( 1 * ( 0.101 - (SHAVEgloss*secondarySHAVEglossMult) ) ) );// * 0.5;
if( 0 != lightsource("_shadow", inshadow) )
OcclusionDirect += inshadow;
if( 0 == lightsource("_cl_noshadow", Cl_noshd) )
Cl_noshd = Cselfshad;
Cl_diff = Cl_noshd - Cselfshad;
SpecularDirectD += specK * Cl_noshd;
SpecularDirectShadowD += specK * Cl_diff;
SpecularDirectU += specK2 * Cl_noshd;
SpecularDirectShadowU += specK2 * Cl_diff;
}
}
// illumination from environments
// this will include both environment and indirect illum
float occl = 0;
color indiff = color(0);
color envcolor = color(0);
color speccolor = color(0);
// get all envlights
shader envlights[] = getlights("category", "environment");
uniform float i, n = arraylength(envlights);
// for specular calculations
vector envdir = reflect(In, Nn);
float angle = radians(coneangle);
for(i=0;i<n;i+=1) // Loop through each envlight
{
//Diffuse contributions from EnvLight
envlights[i]->diffuseIllum(P, Ns, Nn, indiff, envcolor, envdir, occl);
occl=SHAVEselfshad*occl+(1.0-SHAVEselfshad);
color env = envcolor * (1-occl);
DiffuseIndirect += indiff - env;
DiffuseEnvironment += env;
//Specular contributions from EnvLight
//envlights[i]->specularIllum(P, envdir, angle, speccolor);
//SpecularEnvironment += speccolor;
}
Clocal = mix( rootcolor, tipcolor, v );
Oi = Os*SHAVEopacity;
//color radiosity = Oi * Clocal * (DiffuseDirect - DiffuseDirectShadow + DiffuseEnvironment + DiffuseIndirect);
color radiosity = Clocal * (DiffuseDirect - DiffuseDirectShadow + DiffuseEnvironment + DiffuseIndirect);
// find out if it's a bake pass
string CurrentPassClass = "";
option("user:pass_class", CurrentPassClass);
// calculate area for occlusion baking
float a = area(P, "dicing");
// If is a bake pass
if (CurrentPassClass == "RenderRadiosity" && a > 0)
{
for(i=0;i<n;i+=1) // Loop through each envlight
{
envlights[i]->bake(P, Nn, a, radiosity, Clocal);
}
}
Clocal *= Cs;
// AOVs
if (bendTangentAmt >= 0)
{
SpecularDirect = max((SpecularDirectD * SHAVEspec_color * SHAVEspec),
(SpecularDirectU * SHAVEspec_color2 * SHAVEspec));
SpecularDirectShadow = max((SpecularDirectShadowD * SHAVEspec_color * SHAVEspec),
(SpecularDirectShadowU * SHAVEspec_color2 * SHAVEspec));
}
else
{
SpecularDirect = SpecularDirectD * SHAVEspec_color * SHAVEspec;
SpecularDirectShadow = SpecularDirectShadowD * SHAVEspec_color * SHAVEspec;
}
Ambient = Clocal * Ka * ambient();
OcclusionIndirect += occl;
DiffuseColor = Clocal;
DiffuseDirect *= Clocal;
DiffuseDirectShadow *= Clocal;
DiffuseEnvironment *= Clocal;
DiffuseIndirect *= Clocal;
// Note Oi does not affect specular. - no no, it should (joe)
if(Oi != color(1)) {
Ambient *= Oi;
SpecularDirect*=Oi;
SpecularDirectShadow*=Oi;
OcclusionIndirect *= Oi;
DiffuseDirect *= Oi;
DiffuseDirectShadow *= Oi;
DiffuseEnvironment *= Oi;
DiffuseIndirect *= Oi;
}
Ci = Ambient +
DiffuseDirect - DiffuseDirectShadow +
SpecularDirect - SpecularDirectShadow +
DiffuseEnvironment + DiffuseIndirect;
}
|