-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathWoodGrain.osl
More file actions
191 lines (175 loc) · 5.64 KB
/
WoodGrain.osl
File metadata and controls
191 lines (175 loc) · 5.64 KB
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
/*
* WoodGrain.osl by Michel J. Anders (c)2012
* from https://github.com/sambler/osl-shaders
*
* license: cc-by-sa
*
* original script from -
* http://blenderthings.blogspot.com.au/2013/01/an-osl-wood-shader-for-blender-cycles.html
* Modified by Saul Espinosa for Redshift 3D 2020-1-11
*/
// for the original renderman shader, check http://www.larrygritz.com/arman/materials.html
// adapted from larry gritz advanced renderman patterns.h
float smoothpulse (float e0, float e1, float e2, float e3, float x)
{
return smoothstep(e0,e1,x) - smoothstep(e2,e3,x);
}
/* A pulse train of smoothsteps: a signal that repeats with a given
* period, and is 0 when 0 <= mod(x/period,1) < edge, and 1 when
* mod(x/period,1) > edge.
*/
float smoothpulsetrain (float e0, float e1, float e2, float e3, float period, float x)
{
return smoothpulse (e0, e1, e2, e3, mod(x,period));
}
// adapted from larry gritz advanced renderman noises.h
/* fractional Brownian motion
* Inputs:
* p position
* octaves max # of octaves to calculate
* lacunarity frequency spacing between successive octaves
* gain scaling factor between successive octaves
*/
/* A vector-valued antialiased fBm. */
vector vfBm (point p, float octaves, float lacunarity, float gain)
{
float amp = 1;
point pp = p;
vector sum = 0;
float i;
for (i = 0; i < octaves; i += 1) {
vector d = snoise(pp);
sum += amp * d;
amp *= gain;
pp *= lacunarity;
}
return sum;
}
// adapted from larry gritz oak.sl and oak.h
// original comments between /* ... */
// my comments start with //
// note that I dropped the whole filterwidth stuff, partly
// because I don't think it necessary in Blender Cycles, partly
// because the derivatives and area() function doesn't seem to work (yet)
// all specialized snoise defines are replaced by snoise() function calls
float oaktexture (point Pshad,
float dPshad,
float ringfreq,
float ringunevenness,
float grainfreq,
float ringnoise,
float ringnoisefreq,
float trunkwobble,
float trunkwobblefreq,
float angularwobble,
float angularwobblefreq,
float ringy,
float grainy)
{
/* We shade based on Pshad, but we add several layers of warping: */
/* Some general warping of the domain */
vector offset = vfBm(Pshad*ringnoisefreq, 2, 4, 0.5);
point Pring = Pshad + ringnoise*offset;
/* The trunk isn't totally steady xy as you go up in z */
vector d = snoise(Pshad[2]*trunkwobblefreq) ;
Pring += trunkwobble * d * vector(1,1,0);
/* Calculate the radius from the center. */
float r = hypot(Pring[0], Pring[1]) * ringfreq;
/* Add some noise around the trunk */
r += angularwobble * smoothstep(0,5,r)
* snoise (angularwobblefreq*(Pring)*vector(1,1,0.1));
/* Now add some noise so all rings are not equal width */
r += ringunevenness*snoise(r);
float inring = smoothpulsetrain (0.1, 0.55, 0.7, 0.95, 1, r);
point Pgrain = Pshad*grainfreq*vector(1,1,0.05);
float dPgrain = dPshad; //dropped filterwidthp(Pgrain);
float grain = 0;
float i, amp=1;
for (i = 0; i < 2; i += 1) {
float grain1valid = 1-smoothstep(0.2,0.6,dPgrain);
if (grain1valid > 0) {
float g = grain1valid * snoise (Pgrain);
g *= (0.3 + 0.7*inring);
g = pow(clamp(0.8 - (g),0,1),2);
g = grainy * smoothstep (0.5, 1, g);
if (i == 0)
inring *= (1-0.4*grain1valid);
grain = max (grain, g);
}
Pgrain *= 2;
dPgrain *= 2;
amp *= 0.5;
}
return mix (inring*ringy, 1, grain);
}
// larry gritz' original shader was a closure but this shader
// provides different outputs that you can plug into your own
// closures/shaders
surface oak
[[ string help = "Procedural Wood Grain Shader",
string label = "Wood Grain" ]]
(
point Vector = P,
color LightWood = color(0.5, 0.2, 0.067),
color DarkWood = color(0.15, 0.077, 0.028),
float Sharpness = 0.01
[[
float min = 0, float max = 1
]],
float Ringy = 1.0
[[
float min = 0, float max = 5
]],
float RingFreq = 8.0
[[
float min = 0, float max = 50
]],
float RingUnevenness = 0.5
[[
float min = 0, float max = 50
]],
float RingNoise = 0.02
[[
float min = 0, float max = 1
]],
float RingNoiseFreq = 1.0
[[
float min = 0, float max = 1
]],
float Grainy = 0.5
[[
float min = 0, float max = 1
]],
float GrainFreq = 25.0
[[
float min = 0, float max = 50
]],
float TrunkWobble = 0.15
[[
float min = 0, float max = 50
]],
float TrunkWobbleFreq = 0.025
[[
float min = 0, float max = 1
]],
float AngularWobble = 1.0
[[
float min = 0, float max = 1
]],
float AngularWobbleFreq = 1.5
[[
float min = 0, float max = 5
]],
output color Color = 0,
output float Specular = 0.1,
output float Roughness = 0.1,
output float Displacement = 0.0 )
{
float wood = oaktexture (Vector, Sharpness, RingFreq, RingUnevenness, GrainFreq,
RingNoise, RingNoiseFreq, TrunkWobble, TrunkWobbleFreq,
AngularWobble, AngularWobbleFreq, Ringy, Grainy);
Color = mix (LightWood, DarkWood, wood);
Displacement = -wood; // lightwood = 0, darkwood is deeper/lower = -1
Specular = 0.1*(1.0-0.5*wood); // darkwood is less specular
Roughness = 0.1+0.1*wood; // and rougher
}