forked from Maxon-Computer/Redshift-OSL-Shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleTiles.osl
241 lines (222 loc) · 7.59 KB
/
SimpleTiles.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
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
241
// Tiles, Bricks and other Patterns
// SimpleTiles.osl, by Zap Andersson
// Modified: 2019-11-25
// Copyright 2019 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
// https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
#define PARAM lookup,fluvw+point(xx,0,0),GapWidth*0.05,Tiling,Edge*0.1,Bump,HitIdx
// Do a single rectangle
int doRect(float loX, float hiX, float loY, float hiY, int Tile, int InIdx, float Radius,
// Layout of these match the PARAM macro above
point uvw, point baseuvw, float GapWidth, vector Tiling, float Edge, output float Bump, output int Index
)
{
float x = uvw[0] / Tiling[0], y = uvw[1] / Tiling[1];
float lox = loX / Tiling[0];
float hix = hiX / Tiling[0];
float loy = loY / Tiling[1];
float hiy = hiY / Tiling[1];
// First quick rejection when being outside the rectangle
if (x < lox + GapWidth ||
x > hix - GapWidth ||
y < loy + GapWidth ||
y > hiy - GapWidth)
return 0;
// Turn coordinate into circle
float gwr = GapWidth + Radius;
float lx = x - (lox + gwr);
float ly = y - (loy + gwr);
float hx = x - (hix - gwr);
float hy = y - (hiy - gwr);
// Now compute special-circle-coordinate
// which is really the edges around the rectangle
vector dp = vector(lx<0?lx:(hx>0?hx:0.0), ly<0?ly:(hy>0.0?hy:0.0), 0.0);
float d = length(dp);
// Outside the radius - we missed
if (d > Radius) return 0;
// Inside? Compute the bump as a Smoothstep
if (Radius > Edge)
Bump = 1.0 - smoothstep(Radius-Edge, Radius+Edge, d);
else
{
// If there is an Edge but the radius is smaller, we
// need to compute the bump differently...
float lx = x - (lox + GapWidth);
float ly = y - (loy + GapWidth);
float hx = x - (hix - GapWidth);
float hy = y - (hiy - GapWidth);
Bump = smoothstep(-Edge, Edge, lx);
Bump *= smoothstep(-Edge, Edge, -hx);
Bump *= smoothstep(-Edge, Edge, ly);
Bump *= smoothstep(-Edge, Edge, -hy);
}
// Set index to inIdx
Index = InIdx;
// Return the tile ID we are in
return Tile;
}
shader SimpleTiles
[[ string help = "<font size=+1><b>Simple Tiles</b></font><br>"
"Allows various kinds of Tiling. The Tiling Offset<br>"
"modifies the base parameter of the tiling layout,<br>"
"and can generate different effects. ",
string category = "Textures",
string label = "Simple Tiles" ]]
(
// Inputs
point UVW = point(u,v,0),
float Scale = 1.0
[[ float min=0.000001, float max=1000000.0 ]],
int TileMode = 0
[[ string widget = "mapper",
string label = "Tiling Mode",
string options =
"Checker Tiles:1"
"|Running/Stack Bond:0"
"|English Bond:2"
"|Fine Running/Stack Bond:3"
"|Twist Box:4",
string help="Choose the kind of tile pattern to use. "
"The 'TilingOffset' parameter adjusts the "
"appearance of the tiles."
]],
float U_Count = 2.0
[[ string label = "Tiles in U",
float min=0.000001, float max=1000000.0 ]],
float V_Count = 2.0
[[ string label = "Tiles in V",
float min=0.000001, float max=1000000.0 ]],
float TilingOffset = 0.5
[[ string label = "Tiling Offset",
float min=0.0, float max=1.0,
string help = "An adjustable parameter which changes "
"the appearance of the tiling. Does different things "
"for different Tiling Modes" ]],
float GapWidth = 0.1
[[ string label = "Gap Width",
string help = "The width of the gap between tiles",
float min=0.0, float max=10.0 ]],
float Radius = 0.2
[[ string label = "Corner Roundness",
string help = "Adds fillet radius to the corner of the tiles",
float min=0.0, float max=10.0 ]],
float Edge = 0.1
[[ string label = "Edge Width (for Bump)",
string help = "The width of a (slightly beveled) fake 'Edge' "
"to give the tile a more realistic appearance",
float min=0.0, float max=10.0 ]],
float EdgeBump = 1.0
[[ float min=-10.0, float max=10.0,
string help = "The amount of Edge bump effect" ]],
float ColorBump = 0.0
[[ float min=-10.0, float max=10.0,
string help = "Adds some contribution of the color "
"settings to the Bump output" ]],
color GapColor = 0.2
[[ string label = "Gap Color",
string help = "The color of the gap between tiles" ]],
color TileColor1 = 0.4
[[ string label = "Tile Color 1",
string help = "First Tile Color"
]],
color TileColor2 = 0.5
[[ string label = "Tile Color 2",
string help = "Second Tile Color"
]],
color TileColor3 = 0.6
[[ string label = "Tile Color 3",
string help = "Third Tile Color. Not all Tiling Modes use this."
]],
// Outputs
output color Col = 0.0,
output float Bump = 0.0,
output int Tile = 0,
output int TileIdx = 0,
output int Index = 0
)
{
int Overlap = 1;
vector Tiling = vector(U_Count, V_Count, 0.0);
Col = GapColor;
point uvw = UVW * Tiling / Scale;
float to = TilingOffset;
float to2 = to*0.5;
float Roundness = Radius / 10.0;
// Floor of UVW
point fluvw = floor(uvw);
// Fractional UVW
point fruvw = uvw - fluvw;
// Loop over neighbours
for (int xx = -Overlap; xx <= 0; xx++)
{
point lookup = uvw - fluvw - point(xx,0,0);
int HitIdx = 0;
// Running Bond
if (TileMode == 0)
{
if (Tile == 0)
Tile = doRect(0.0 ,1.0, 0.0, 0.5, 1, 1, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(to, to+1.0, 0.5, 1.0, 2, 2, Roundness, PARAM);
}
// Stack Bond
if (TileMode == 1)
{
if (Tile == 0)
Tile = doRect(0.0 ,0.5, 0.0, 0.5, 1, 1, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.0, 0.5, 0.5, 1.0, 2, 2, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.5 ,1.0, 0.0, 0.5, 2, 3, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.5, 1.0, 0.5, 1.0, 1, 4, Roundness, PARAM);
}
// English Bond
if (TileMode == 2)
{
if (Tile == 0)
Tile = doRect(0.0 ,1.0, 0.0, 0.5, 1, 1, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.0+to2, 0.5+to2, 0.5, 1.0, 2, 2, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.5+to2, 1.0+to2, 0.5, 1.0, 2, 3, Roundness, PARAM);
}
// Fine Running Bond
if (TileMode == 3)
{
if (Tile == 0)
Tile = doRect(0.0 ,1.0, 0.0, 0.5, 1, 1, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.0+to, 0.5+to, 0.5, 0.75, 2, 2, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.5+to, 1.0+to, 0.5, 0.75, 2, 3, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.0+to2, 0.5+to2, 0.75, 1.0, 3, 4, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.5+to2, 1.0+to2, 0.75, 1.0, 3, 5, Roundness, PARAM);
}
// Twist Box
if (TileMode == 4)
{
if (Tile == 0)
Tile = doRect(0.0, 1.0-to2, 0.0, to2, 1, 1, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(to2, 1.0, 1.0-to2, 1.0, 1, 2, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(0.0, to2, to2, 1.0, 2, 3, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(1.0-to2, 1.0, 0.0, 1.0-to2, 2, 4, Roundness, PARAM);
if (Tile == 0)
Tile = doRect(to2, 1.0-to2, to2, 1.0-to2, 3, 5, Roundness, PARAM);
}
if (HitIdx > 0)
{
TileIdx = HitIdx;
Index = HitIdx + int((float)cellnoise(fluvw+point(xx,0,0)) * 1024.0);
}
}
if (Tile == 1) Col = TileColor1;
if (Tile == 2) Col = TileColor2;
if (Tile == 3) Col = TileColor3;
Bump *= EdgeBump;
Bump += dot(0.3333, Col) * ColorBump;
}