-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
345 lines (272 loc) · 7.76 KB
/
main.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// ----------------------------------------------------------------------------
// PBR TYLER
// ----------------------------------------------------------------------------
//
// Takes 2x by 2x PBR texture renders and turns them into 1x by 1x seamless
// textures.
//
// The source map is cut into 4 pieces:
// - base source
// - corners source
// - up/down edge source
// - left/right edge source
//
// Corner and edge pieces are cut through the middle where the picture is
// seamless and put it where the seams are. Having 4x the source might be
// excessive but it guarantees no repetition of the source material.
//
// Required maps:
// - _d - diffuse colour (RGBA) - Alpha currently not tested.
// - _n - normal map (RGB)
// - _hrm - hrm map (RGB) - Channels accordingly: Height, Roughness, Metalness.
//
// No ambient occlusion currently - assuming diffuse colour already has one
// baked in.
//
// Format is PNG.
//
// Usage:
// ./pbrtyler -i <input_path> -o <output_path>
//
// Example:
// ./pbrtyler -i C:\source\tex -o C:\destination\tex
//
// Input dir:
// - tex_d.png
// - tex_n.png
// - tex_hrm.png
//
// Suffixes like _d.png will be added for you.
//
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// TODO
// ----------------------------------------------------------------------------
//
// - add low frequency height to break up the join lines
// - include alpha in the factor compute
//
// ----------------------------------------------------------------------------
#include <string>
#include <exception>
#include "FastNoiseLite.h"
#include "argument_reader.h"
#include "log.h"
#include "loader.h"
#include "maptools.h"
#include "types.h"
using namespace std;
// ----------------------------------------------------------------------------
// GLOBALS
// ----------------------------------------------------------------------------
string input;
string output;
bool blur;
float influence_power = 0.125f;
float height_noise_factor = 0.8f;
float height_epsilon = 0.03f;
MapTools mt;
unsigned int src_w = 0U;
unsigned int src_h = 0U;
unsigned int w;
unsigned int h;
PBRMap src;
PBRMap base;
vector<float> fac_base;
PBRMap sc1;
vector<float> fac_sc1;
PBRMap sc2;
vector<float> fac_sc2;
PBRMap sc3;
vector<float> fac_sc3;
PBRMap dst;
vector<float> fac_dst;
// We can get away with a single map cause no overlap.
PBRMap corners;
vector<float> fac_corners;
// All edges to be blended here.
PBRMap edges;
vector<float> fac_edges;
// No need for ud, copy those straight to edges map.
PBRMap edges_lr;
vector<float> fac_edges_lr;
// ----------------------------------------------------------------------------
// FUNCTIONS
// ----------------------------------------------------------------------------
void get_filenames(int argc, char** argv)
{
OP("- Get arguments.");
input = get_argument_value("-i", argc, argv);
output = get_argument_value("-o", argc, argv);
blur = !get_argument_flag("-noblur", argc, argv);
if (get_argument_flag("-sharpness", argc, argv)) {
influence_power = stof(get_argument_value("-sharpness", argc, argv));
}
if (get_argument_flag("-noise", argc, argv)) {
height_noise_factor = stof(get_argument_value("-noise", argc, argv));
}
if (get_argument_flag("-epsilon", argc, argv)) {
height_epsilon = stof(get_argument_value("-epsilon", argc, argv));
}
}
int read_source_maps()
{
OP("- Read source maps.");
try {
load_pbr(input, src, src_w, src_h);
w = src_w / 2;
h = src_h / 2;
mt.src_w = src_w;
mt.src_h = src_h;
mt.w = w;
mt.h = h;
mt.hnf = height_noise_factor;
mt.he = height_epsilon;
OP("w=[" << w << "] h=[" << h << "]");
} catch (std::exception e) {
OP("Could not load source maps.");
return 1;
}
return 0;
}
void reserve_maps()
{
OP("- Reserve maps.");
reserve_pbr(base, w, h);
reserve_pbr(sc1, w, h);
reserve_pbr(sc2, w, h);
reserve_pbr(sc3, w, h);
reserve_pbr(dst, w, h);
reserve_pbr(corners, w, h);
reserve_pbr(edges, w, h);
reserve_pbr(edges_lr, w, h);
}
void create_influence_maps()
{
OP("- Create influence maps.");
mt.influence_map_base(fac_base, influence_power);
mt.influence_map_corner(fac_sc1, influence_power);
mt.influence_map_edge(fac_sc2, influence_power);
mt.influence_map_edge(fac_sc3, influence_power);
mt.influence_map_empty(fac_corners);
mt.influence_map_empty(fac_edges);
mt.influence_map_empty(fac_edges_lr);
mt.influence_map_empty(fac_dst);
}
void split_sources()
{
OP("- Split into working sources.");
mt.copy_from_wide_map(src, base, 0, 0);
mt.copy_from_wide_map(src, sc1, w, 0);
mt.copy_from_wide_map(src, sc2, 0, h);
mt.copy_from_wide_map(src, sc3, w, h);
mt.copy_from_wide_map(src, dst, w/2, h/2);
free_pbr(src);
}
void copy_corners()
{
OP("- Copy corners to corners temp.");
Rect2 src_ul = Rect2(0, 0, w/2, h/2);
Rect2 src_ur = Rect2(w/2, 0, w/2, h/2);
Rect2 src_dr = Rect2(w/2, h/2, w/2, h/2);
Rect2 src_dl = Rect2(0, h/2, w/2, h/2);
Vec2 dst_ul = Vec2(0, 0);
Vec2 dst_ur = Vec2(w/2, 0);
Vec2 dst_dr = Vec2(w/2, h/2);
Vec2 dst_dl = Vec2(0, h/2);
mt.copy_chunk(sc1, corners, fac_sc1, fac_corners, src_dr, dst_ul);
mt.copy_chunk(sc1, corners, fac_sc1, fac_corners, src_dl, dst_ur);
mt.copy_chunk(sc1, corners, fac_sc1, fac_corners, src_ul, dst_dr);
mt.copy_chunk(sc1, corners, fac_sc1, fac_corners, src_ur, dst_dl);
free_pbr(sc1);
}
void copy_edges()
{
OP("- Copy edges to u,d edge temp.");
Rect2 src_u = Rect2(0, 0, w, h/2);
Rect2 src_d = Rect2(0, h/2, w, h/2);
Rect2 src_l = Rect2(0, 0, w/2, h);
Rect2 src_r = Rect2(w/2, 0, w/2, h);
Vec2 dst_ul = Vec2(0, 0);
Vec2 dst_ur = Vec2(w/2, 0);
Vec2 dst_dr = Vec2(w/2, h/2);
Vec2 dst_dl = Vec2(0, h/2);
mt.copy_chunk(sc2, edges, fac_sc2, fac_edges, src_u, dst_dl);
mt.copy_chunk(sc2, edges, fac_sc2, fac_edges, src_d, dst_ul);
free_pbr(sc2);
OP("- Copy edges to l,r temp.");
mt.copy_chunk(sc3, edges_lr, fac_sc3, fac_edges_lr, src_l, dst_ur);
mt.copy_chunk(sc3, edges_lr, fac_sc3, fac_edges_lr, src_r, dst_ul);
free_pbr(sc3);
}
void apply_height_noise()
{
OP("- Apply height noise.");
FastNoiseLite ns;
ns.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
ns.SetFrequency(1.5f / (float)w);
ns.SetFractalGain(0.5);
ns.SetFractalLacunarity(2.f);
ns.SetFractalOctaves(8
);
std::srand(std::time(0));
ns.SetSeed(std::rand());
mt.apply_fac_noise(fac_base, ns, height_noise_factor);
ns.SetSeed(std::rand());
mt.apply_fac_noise(fac_sc1, ns, height_noise_factor);
ns.SetSeed(std::rand());
mt.apply_fac_noise(fac_sc2, ns, height_noise_factor);
ns.SetSeed(std::rand());
mt.apply_fac_noise(fac_sc3, ns, height_noise_factor);
}
void apply_seams_fix()
{
OP("- Blend edges temp to corner temp.");
mt.blend_map_4_way(dst,
base, edges, edges_lr, corners,
fac_base, fac_edges, fac_edges_lr, fac_corners,
blur
);
}
void save_output()
{
OP("- Save output.");
save_pbr(output, dst, w, h);
}
void clean_up()
{
OP("- Clean up.");
free_pbr(dst);
}
// ----------------------------------------------------------------------------
// MAIN
// ----------------------------------------------------------------------------
int main(int argc, char** argv)
{
OP("\n\n");
OP("--------------------------------------------------------------------");
OP("- PBR TYLER v0.1");
OP("--------------------------------------------------------------------");
// Inputs.
get_filenames(argc, argv);
// Source maps.
int status = read_source_maps();
if (status != 0) return status;
// Setup working memory.
reserve_maps();
create_influence_maps();
split_sources();
apply_height_noise();
// Perform ops.
copy_corners();
copy_edges();
apply_seams_fix();
// Save.
save_output();
// Finish.
clean_up();
OP("- PBR TYLER end.");
OP("--------------------------------------------------------------------");
OP("\n\n");
return 0;
}