26float smoothstep(
float edge0,
float edge1,
float x) {
27 if (edge0 == edge1)
return x < edge0 ? 0.f : 1.f;
28 const float t = std::clamp((
x - edge0) / (edge1 - edge0), 0.f, 1.f);
29 return t *
t * (3.f - 2.f *
t);
33float voronoiDist(
const NoiseField &
n,
float x,
float y) {
return n.worleyF1(
x,
y); }
35std::unique_ptr<image::ImageData> makeFromHeightFn(
const Params &
params, std::string &
error,
36 const TextureRecipeDef &def) {
38 if (ctx.width > 4096 || ctx.height > 4096) {
39 error =
"texture size too large (max 4096)";
42 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
50TextureRecipeDef makeDef(std::string
id, ColorRamp ramp,
51 std::function<
float(
float,
float,
const NoiseField &)>
height,
54 def.id = std::move(
id);
55 def.albedo = std::move(ramp);
56 def.height = std::move(
height);
61std::vector<TextureRecipeDef> buildDefs() {
62 std::vector<TextureRecipeDef> defs;
67 ramp.add(0.00f, 58, 38, 22);
68 ramp.add(0.35f, 92, 62, 34);
69 ramp.add(0.65f, 120, 84, 48);
70 ramp.add(1.00f, 148, 110, 68);
72 pbr.roughnessLow = 0.75f;
73 pbr.roughnessHigh = 1.f;
74 defs.push_back(makeDef(
75 "tex.soil", std::move(ramp),
76 [](
float u,
float v,
const NoiseField &
n) {
77 float h =
n.fbm(
u,
v, 4);
78 h =
h * 0.75f + 0.25f *
n.valueNoise(
u * 3.1f + 2.f,
v * 3.1f);
87 ramp.add(0.00f, 52, 54, 58);
88 ramp.add(0.40f, 88, 90, 96);
89 ramp.add(0.70f, 130, 132, 138);
90 ramp.add(1.00f, 176, 178, 184);
92 pbr.roughnessLow = 0.6f;
93 pbr.roughnessHigh = 0.9f;
94 pbr.aoStrength = 1.4f;
95 defs.push_back(makeDef(
96 "tex.stone", std::move(ramp),
97 [](
float u,
float v,
const NoiseField &
n) {
98 float h =
n.ridged(
u,
v, 4);
99 const float crack = std::fabs(
n.valueNoise(
u * 2.7f,
v * 2.7f) - 0.5f) * 2.f;
100 h =
h * 0.7f + (1.f -
crack) * 0.3f;
109 ramp.add(0.00f, 28, 24, 20);
110 ramp.add(0.28f, 58, 52, 44);
111 ramp.add(0.55f, 92, 84, 70);
112 ramp.add(0.80f, 122, 112, 96);
113 ramp.add(1.00f, 148, 138, 120);
115 pbr.roughnessLow = 0.62f;
116 pbr.roughnessHigh = 0.98f;
117 pbr.aoStrength = 1.7f;
118 pbr.normalStrength = 3.0f;
119 defs.push_back(makeDef(
120 "tex.rock", std::move(ramp),
121 [](
float u,
float v,
const NoiseField &
n) {
122 float h =
n.ridged(
u * 0.9f,
v * 0.9f, 5);
124 std::pow(std::fabs(
n.valueNoise(
u * 2.2f,
v * 2.2f) - 0.5f) * 2.f, 1.6f);
125 h =
h * 0.62f + 0.22f *
n.fbm(
u * 2.8f,
v * 2.8f, 3) + (1.f -
crack) * 0.16f;
134 ramp.add(0.00f, 230, 230, 235);
135 ramp.add(0.45f, 200, 200, 210);
136 ramp.add(0.55f, 120, 120, 135);
137 ramp.add(0.70f, 190, 190, 200);
138 ramp.add(1.00f, 245, 245, 248);
140 pbr.roughnessLow = 0.08f;
141 pbr.roughnessHigh = 0.35f;
142 defs.push_back(makeDef(
143 "tex.marble", std::move(ramp),
144 [](
float u,
float v,
const NoiseField &
n) {
145 const float w =
n.warp(
u,
v, 2.5f, std::max(2, 3));
146 float vein = std::sin((
u +
w * 4.f) * 3.14159265f * 2.f) * 0.5f + 0.5f;
147 vein = std::pow(vein, 1.6f);
148 return vein * 0.65f +
w * 0.35f;
156 ramp.add(0.00f, 18, 60, 110);
157 ramp.add(0.40f, 28, 96, 150);
158 ramp.add(0.70f, 48, 140, 180);
159 ramp.add(1.00f, 120, 200, 210);
161 pbr.roughnessLow = 0.05f;
162 pbr.roughnessHigh = 0.3f;
163 pbr.metallic = 0.05f;
164 defs.push_back(makeDef(
165 "tex.water", std::move(ramp),
166 [](
float u,
float v,
const NoiseField &
n) {
167 float wave = 0.5f + 0.5f * std::sin((
u * 2.2f +
v * 0.4f) * 6.28318f +
168 n.valueNoise(
u * 1.3f,
v * 1.3f) * 2.f);
169 float detail =
n.fbm(
u * 1.5f,
v * 1.5f, 4);
170 return wave * 0.55f +
detail * 0.45f;
178 ramp.add(0.00f, 20, 60, 120);
179 ramp.add(0.40f, 40, 120, 190);
180 ramp.add(0.70f, 90, 170, 220);
181 ramp.add(1.00f, 190, 230, 245);
183 pbr.roughnessLow = 0.04f;
184 pbr.roughnessHigh = 0.5f;
185 defs.push_back(makeDef(
186 "tex.ripple", std::move(ramp),
187 [](
float u,
float v,
const NoiseField &
n) {
188 const float rings = 6.f;
189 float d = voronoiDist(
n,
u,
v);
190 float ring = 0.5f + 0.5f * std::sin(
d * 6.28318f * rings);
193 return ring * 0.7f +
n.fbm(
u * 2.f,
v * 2.f, 3) * 0.3f;
201 ramp.add(0.00f, 70, 130, 200);
202 ramp.add(0.45f, 110, 170, 220);
203 ramp.add(0.70f, 180, 210, 235);
204 ramp.add(1.00f, 245, 248, 252);
205 defs.push_back(makeDef(
206 "tex.sky_cloud", std::move(ramp),
207 [](
float u,
float v,
const NoiseField &
n) {
208 const float sky = std::clamp(1.f -
v / std::max(0.01f, 4.f), 0.f, 1.f);
209 float clouds =
n.fbm(
u * 0.9f + 3.f,
v * 0.9f, 4);
211 return sky * 0.45f +
clouds * 0.55f;
218 ramp.add(0.00f, 150, 106, 58);
219 ramp.add(0.30f, 178, 128, 72);
220 ramp.add(0.55f, 130, 88, 48);
221 ramp.add(0.75f, 96, 62, 36);
222 ramp.add(1.00f, 70, 44, 28);
224 pbr.roughnessLow = 0.35f;
225 pbr.roughnessHigh = 0.7f;
226 defs.push_back(makeDef(
227 "tex.wood", std::move(ramp),
228 [](
float u,
float v,
const NoiseField &
n) {
229 const float w =
n.warp(
u * 0.8f,
v * 0.8f, 2.5f, 3);
230 float grain = 0.5f + 0.5f * std::sin((
u * 4.f +
w * 6.f) * 6.28318f);
232 const float detail =
n.fbm(
u * 3.f,
v * 3.f, 3);
241 ramp.add(0.00f, 30, 44, 34);
242 ramp.add(0.40f, 52, 76, 52);
243 ramp.add(0.70f, 74, 104, 68);
244 ramp.add(1.00f, 108, 140, 92);
246 pbr.roughnessLow = 0.8f;
247 pbr.roughnessHigh = 1.f;
248 pbr.normalStrength = 2.f;
249 defs.push_back(makeDef(
250 "tex.cloth", std::move(ramp),
251 [](
float u,
float v,
const NoiseField &
n) {
252 const float threads = 9.f;
253 const float fx =
u * threads;
254 const float fy =
v * threads;
255 const float dx = std::fabs(std::fmod(fx, 1.f) - 0.5f);
256 const float dy = std::fabs(std::fmod(fy, 1.f) - 0.5f);
257 const float c = std::min(
dx,
dy);
258 float thread = 1.f -
c * 2.f;
259 const int ix = int(std::floor(fx));
260 const int iy = int(std::floor(fy));
261 if ((ix + iy) % 2 == 0) thread = 1.f - thread;
262 const float noise =
n.valueNoise(
u * 2.f,
v * 2.f);
263 return thread * 0.55f + noise * 0.35f + 0.1f;
271 ramp.add(0.00f, 88, 30, 60);
272 ramp.add(0.30f, 160, 74, 96);
273 ramp.add(0.50f, 224, 176, 120);
274 ramp.add(0.70f, 150, 96, 70);
275 ramp.add(1.00f, 74, 46, 60);
277 pbr.roughnessLow = 0.3f;
278 pbr.roughnessHigh = 0.6f;
279 defs.push_back(makeDef(
280 "tex.ornament", std::move(ramp),
281 [](
float u,
float v,
const NoiseField &
n) {
282 const float w =
n.warp(
u,
v, 3.5f, 4);
283 float swirl = 0.5f + 0.5f * (std::sin((
u +
w * 4.f) * 6.28318f) *
284 std::cos((
v -
w * 3.f) * 6.28318f));
285 return swirl * 0.6f +
n.fbm(
u * 2.f,
v * 2.f, 4) * 0.4f;
293 ramp.add(0.00f, 220, 200, 168);
294 ramp.add(0.35f, 196, 172, 138);
295 ramp.add(0.50f, 90, 62, 40);
296 ramp.add(0.75f, 60, 42, 30);
297 ramp.add(1.00f, 150, 120, 92);
299 pbr.roughnessLow = 0.5f;
300 pbr.roughnessHigh = 0.9f;
301 defs.push_back(makeDef(
302 "tex.spot", std::move(ramp),
303 [](
float u,
float v,
const NoiseField &
n) {
304 const float blobs =
n.valueNoise(
u * 2.2f,
v * 2.2f);
306 spot = spot * 0.8f +
n.fbm(
u * 3.f,
v * 3.f, 3) * 0.2f;
315 ramp.add(0.00f, 245, 245, 244);
316 ramp.add(0.50f, 30, 30, 32);
317 ramp.add(1.00f, 245, 245, 244);
319 pbr.roughnessLow = 0.4f;
320 pbr.roughnessHigh = 0.8f;
321 defs.push_back(makeDef(
322 "tex.zebra", std::move(ramp),
323 [](
float u,
float v,
const NoiseField &
n) {
324 const float w =
n.warp(
u,
v, 1.5f, 3);
325 float stripe = 0.5f + 0.5f * std::sin((
u * 5.f +
w * 4.f) * 6.28318f);
327 return stripe * 0.9f +
n.valueNoise(
u * 3.f,
v * 3.f) * 0.1f;
335 ramp.add(0.00f, 78, 46, 38);
336 ramp.add(0.30f, 130, 74, 54);
337 ramp.add(0.60f, 164, 96, 66);
338 ramp.add(1.00f, 196, 130, 92);
340 pbr.roughnessLow = 0.65f;
341 pbr.roughnessHigh = 0.95f;
342 pbr.aoStrength = 1.6f;
343 defs.push_back(makeDef(
344 "tex.wall", std::move(ramp),
345 [](
float u,
float v,
const NoiseField &
n) {
346 const float rows = 5.f;
348 const int row = int(std::floor(
v *
rows));
349 const float off = (
row % 2 == 0) ? 0.f : 0.5f;
350 const float bx =
u *
cols + off;
351 const float mh = std::fabs(std::fmod(
v *
rows, 1.f) - 0.5f);
352 const float mv = std::fabs(std::fmod(bx, 1.f) - 0.5f);
353 float brick = 1.f -
smoothstep(0.30f, 0.42f, std::min(mh, mv));
354 const float noise =
n.valueNoise(
u * 2.f,
v * 2.f);
355 return brick * 0.75f + noise * 0.25f * brick + (1.f - brick) * 0.15f;
363 ramp.add(0.00f, 60, 62, 66);
364 ramp.add(0.35f, 96, 100, 106);
365 ramp.add(0.70f, 138, 142, 148);
366 ramp.add(1.00f, 182, 186, 192);
368 pbr.roughnessLow = 0.6f;
369 pbr.roughnessHigh = 0.85f;
370 defs.push_back(makeDef(
371 "tex.cement", std::move(ramp),
372 [](
float u,
float v,
const NoiseField &
n) {
373 float mottle =
n.fbmPerlin(
u,
v, 4);
374 const float crack = 1.f -
smoothstep(0.55f, 0.72f,
n.ridgedPerlin(
u * 3.f,
v * 3.f, 3));
375 return mottle * 0.6f +
crack * 0.4f;
386 ramp.add(0.00f, 132, 134, 138);
387 ramp.add(0.30f, 172, 174, 178);
388 ramp.add(0.68f, 214, 216, 220);
389 ramp.add(1.00f, 244, 245, 247);
391 pbr.roughnessLow = 0.78f;
392 pbr.roughnessHigh = 0.96f;
393 pbr.normalStrength = 2.2f;
394 pbr.aoStrength = 0.65f;
395 pbr.heightStrength = 0.45f;
396 defs.push_back(makeDef(
397 "tex.asphalt", std::move(ramp),
398 [](
float u,
float v,
const NoiseField &
n) {
399 const float fine =
n.fbmPerlin(
u * 5.f,
v * 5.f, 4);
400 const float aggregate =
n.valueNoise(
u * 18.f + 3.7f,
v * 18.f + 9.1f);
401 const float wear =
smoothstep(0.68f, 0.82f,
n.ridgedPerlin(
u * 1.6f,
v * 1.6f, 3));
402 return std::clamp(fine * 0.55f + aggregate * 0.35f -
wear * 0.18f + 0.16f, 0.f, 1.f);
410 ramp.add(0.00f, 40, 26, 18);
411 ramp.add(0.30f, 84, 56, 34);
412 ramp.add(0.60f, 116, 78, 46);
413 ramp.add(1.00f, 148, 104, 62);
415 pbr.roughnessLow = 0.7f;
416 pbr.roughnessHigh = 1.f;
417 pbr.aoStrength = 1.8f;
418 defs.push_back(makeDef(
419 "tex.mud", std::move(ramp),
420 [](
float u,
float v,
const NoiseField &
n) {
421 float d = voronoiDist(
n,
u,
v);
423 return plate * 0.8f +
n.fbm(
u * 2.5f,
v * 2.5f, 3) * 0.2f;
431 ramp.add(0.00f, 28, 18, 12);
432 ramp.add(0.22f, 58, 38, 24);
433 ramp.add(0.48f, 98, 68, 42);
434 ramp.add(0.72f, 138, 102, 64);
435 ramp.add(1.00f, 72, 48, 30);
437 pbr.roughnessLow = 0.58f;
438 pbr.roughnessHigh = 0.98f;
439 pbr.aoStrength = 1.85f;
440 pbr.normalStrength = 3.6f;
441 defs.push_back(makeDef(
442 "tex.bark", std::move(ramp),
443 [](
float u,
float v,
const NoiseField &
n) {
444 const float warp =
n.warp(
u,
v, 2.1f, 3);
446 0.5f + 0.5f * std::sin((
u * 9.f +
warp * 6.f) * 6.28318f);
447 const float ridge =
n.ridged(
u * 1.6f,
v * 0.28f, 5);
449 std::pow(std::fabs(
n.valueNoise(
u * 1.4f,
v * 4.2f) - 0.5f) * 2.f, 1.55f);
450 const float flake =
n.fbm(
u * 3.2f + 1.7f,
v * 0.9f, 3);
451 return grain * 0.28f + ridge * 0.38f + (1.f -
crack) * 0.22f + flake * 0.12f;
459 ramp.add(0.00f, 36, 40, 32);
460 ramp.add(0.30f, 62, 72, 48);
461 ramp.add(0.52f, 78, 108, 52);
462 ramp.add(0.72f, 96, 132, 58);
463 ramp.add(1.00f, 148, 156, 138);
465 pbr.roughnessLow = 0.72f;
466 pbr.roughnessHigh = 1.f;
467 pbr.aoStrength = 1.7f;
468 pbr.normalStrength = 3.1f;
469 defs.push_back(makeDef(
470 "tex.moss", std::move(ramp),
471 [](
float u,
float v,
const NoiseField &
n) {
472 float rock =
n.ridged(
u * 0.95f,
v * 0.95f, 5);
473 float moss =
smoothstep(0.32f, 0.78f,
n.fbm(
u * 1.8f + 2.f,
v * 1.8f, 4));
475 std::pow(std::fabs(
n.valueNoise(
u * 2.4f,
v * 2.4f) - 0.5f) * 2.f, 1.5f);
476 return rock * (1.f - moss * 0.55f) + moss * 0.62f + (1.f -
crack) * 0.08f;
485CloudField::Params cloudFieldFromParams(
const Params &
params) {
486 CloudField::Params
p;
488 p.worldScale =
params.getFloat(
"worldScale", 96.f);
489 p.coverage =
params.getFloat(
"cloudCoverage", 0.55f);
490 p.softness =
params.getFloat(
"cloudSoftness", 0.12f);
491 p.detail =
params.getFloat(
"cloudDetail", 0.5f);
492 p.windSpeed =
params.getFloat(
"windSpeed", 4.f);
493 p.windAngle =
params.getFloat(
"windAngle", 0.f);
494 p.octaves =
params.getInt(
"octaves", 4);
499std::unique_ptr<image::ImageData> genCloud(
const Params &
params, std::string &
error) {
501 if (ctx.width > 4096 || ctx.height > 4096) {
502 error =
"texture size too large (max 4096)";
505 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
507 ramp.add(0.00f, 90, 140, 205);
508 ramp.add(0.30f, 168, 205, 238);
509 ramp.add(0.55f, 238, 248, 252);
510 ramp.add(1.00f, 255, 255, 255);
513 const float time =
params.getFloat(
"time", 0.f);
514 const float extent =
params.getFloat(
"extent",
field.params().worldScale);
515 std::vector<float>
height(
size_t(ctx.width * ctx.height));
516 field.sample(
height.data(), ctx.width, ctx.height,
time, 0.f, 0.f, extent);
522std::unique_ptr<image::ImageData> genCloudShadow(
const Params &
params, std::string &
error) {
524 if (ctx.width > 4096 || ctx.height > 4096) {
525 error =
"texture size too large (max 4096)";
528 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
530 ramp.add(0.00f, 18, 20, 26);
531 ramp.add(0.30f, 60, 62, 70);
532 ramp.add(0.60f, 120, 122, 130);
533 ramp.add(1.00f, 200, 202, 208);
535 CloudField::Params fp = cloudFieldFromParams(
params);
536 CloudShadow::Params sp;
537 sp.field = CloudField(fp);
538 sp.sunDirX =
params.getFloat(
"sunDirX", 0.f);
539 sp.sunDirY =
params.getFloat(
"sunDirY", 1.f);
540 sp.sunDirZ =
params.getFloat(
"sunDirZ", 0.f);
541 sp.cloudAltitude =
params.getFloat(
"cloudAltitude", 60.f);
542 sp.strength =
params.getFloat(
"cloudShadowStrength", 0.8f);
543 CloudShadow shadow(sp);
545 const float time =
params.getFloat(
"time", 0.f);
546 const float extent =
params.getFloat(
"extent", fp.worldScale);
547 std::vector<float>
height(
size_t(ctx.width * ctx.height));
548 shadow.sampleCoverage(
height.data(), ctx.width, ctx.height,
time, 0.f, 0.f, extent);
565constexpr int kLeafCardPanels = 6;
566constexpr int kLeafCardCols = 2;
567constexpr int kLeafCardRows = 3;
572void buildBlueNoiseCardStamps(std::uint32_t
seed,
int count, std::vector<LeafStamp> &stamps) {
576 const float minDist01 = std::sqrt(0.42f /
float(
count));
577 constexpr int kSide = 64;
578 const float radius = std::max(1.5f, minDist01 *
float(kSide));
580 std::mt19937 rng(
seed ^ 0x9e3779b9u);
581 std::uniform_real_distribution<float>
unit(0.f, 1.f);
582 constexpr float kMargin = 0.07f;
584 for (
const ProcgenPoint &
p : pts.points()) {
587 s.cx = kMargin + (1.f - 2.f * kMargin) * (
p.x /
float(kSide));
588 s.cy = kMargin + (1.f - 2.f * kMargin) * (
p.z /
float(kSide));
589 s.ang =
unit(rng) * 6.28318530718f;
590 s.halfW = 0.050f + 0.028f *
unit(rng);
591 s.halfH = 0.070f + 0.036f *
unit(rng);
599 s.cx = kMargin + (1.f - 2.f * kMargin) *
unit(rng);
600 s.cy = kMargin + (1.f - 2.f * kMargin) *
unit(rng);
602 for (
const LeafStamp &o : stamps) {
603 const float dx =
s.cx - o.cx,
dy =
s.cy - o.cy;
604 if (
dx *
dx +
dy *
dy < minDist01 * minDist01) {
610 s.ang =
unit(rng) * 6.28318530718f;
611 s.halfW = 0.050f + 0.028f *
unit(rng);
612 s.halfH = 0.070f + 0.036f *
unit(rng);
618void sampleOvateLeafCard(
const NoiseField &noise,
float u,
float v,
const std::vector<LeafStamp> &stamps,
619 float &cover,
float &shade) {
622 for (
const LeafStamp &
s : stamps) {
625 const float ca = std::cos(
s.ang), sa = std::sin(
s.ang);
626 const float rx =
dx * ca -
dy * sa;
627 const float ry =
dx * sa +
dy * ca;
629 const float halfW =
s.halfW * (1.f - 0.42f * std::max(0.f, ry / std::max(1e-4f,
s.halfH)));
630 const float d = (rx * rx) / std::max(1e-5f,
halfW *
halfW) +
631 (ry * ry) / std::max(1e-5f,
s.halfH *
s.halfH);
633 if (
mask <= cover)
continue;
636 shade = 0.48f + 0.12f * noise.hash01(
int(
s.cx * 97.f),
int(
s.cy * 53.f));
643void buildSixLeafCardPanels(std::uint32_t
seed,
const NoiseField &noise,
644 std::vector<std::vector<LeafStamp>> &panels) {
646 panels.resize(
size_t(kLeafCardPanels));
647 for (
int i = 0; i < kLeafCardPanels; ++i) {
648 const std::uint32_t panelSeed =
seed ^ (std::uint32_t(i + 1) * 2654435761u);
649 const int count = 8 + int(noise.hash01(i * 3 + 1, i * 7 + 2) * 8.999f);
650 buildBlueNoiseCardStamps(panelSeed,
count, panels[
size_t(i)]);
657void sampleLeafCardAtlas(
const NoiseField &noise,
float fu,
float v,
658 const std::vector<std::vector<LeafStamp>> &panels,
float &cover,
float &shade) {
659 const int col = std::clamp(
int(fu *
float(kLeafCardCols)), 0, kLeafCardCols - 1);
660 const int row = std::clamp(
int(
v *
float(kLeafCardRows)), 0, kLeafCardRows - 1);
661 const float localU = fu * float(kLeafCardCols) - float(col);
662 const float localV =
v * float(kLeafCardRows) - float(row);
663 const int panel =
row * kLeafCardCols + col;
664 sampleOvateLeafCard(noise,
localU,
localV, panels[
size_t(panel)], cover, shade);
668void sampleSingleLeafAtlas(
const NoiseField &noise,
float fu,
float v,
float &cover,
float &shade) {
669 const int col = std::clamp(
int(fu * 2.f), 0, 1),
row = std::clamp(
int(
v * 3.f), 0, 2);
670 const int panel = col + 2 *
row;
671 const float x = fu * 2.f - float(col),
y =
v * 3.f - float(row);
672 const float angle = (noise.hash01(panel, 71) - 0.5f) * 0.28f;
673 const float dx =
x - 0.5f,
dy =
y - 0.5f;
676 const float halfH = 0.37f + noise.hash01(panel, 73) * 0.035f;
677 const float halfW = (0.28f + noise.hash01(panel, 79) * 0.075f) * (1.f - ry * 0.38f);
678 const float bend = (noise.hash01(panel, 83) - 0.5f) * ry * ry * 0.8f;
679 const float side = rx - bend;
682 const float midrib = std::exp(-std::fabs(side) * 95.f);
683 const float veins = std::pow(std::max(0.f, std::cos((ry - std::fabs(side) * 0.65f) * 62.f)), 12.f);
684 shade = std::clamp(0.39f + 0.27f * noise.hash01(panel, 19) + 0.13f * noise.fbm(
x * 4.f,
y * 7.f, 3) -
685 0.09f * std::fabs(side) + 0.07f * midrib + 0.025f * veins,
692void sampleFoliageFill(
const NoiseField &noise,
float u,
float v,
float scale,
float &cover,
float &shade) {
695 const float su =
u *
scale;
697 for (
int oy = -1;
oy <= 1; ++
oy) {
698 for (
int ox = -1;
ox <= 1; ++
ox) {
699 const int cx = int(std::floor(su * 2.2f)) +
ox;
700 const int cy = int(std::floor(sv * 2.2f)) +
oy;
701 const float px = float(
cx) + noise.hash01(
cx,
cy);
702 const float py = float(
cy) + noise.hash01(
cx * 7 + 3,
cy * 13 + 5);
703 float dx = su * 2.2f -
px;
704 float dy =
sv * 2.2f -
py;
705 const float ang = (noise.hash01(
cx * 3 + 1,
cy * 5 + 2) - 0.5f) * 1.4f;
706 const float ca = std::cos(
ang), sa = std::sin(
ang);
707 const float rx =
dx * ca -
dy * sa;
708 const float ry =
dx * sa +
dy * ca;
709 const float halfW = 0.48f * (1.f - 0.4f * std::max(0.f, ry));
710 const float d = (rx * rx) / std::max(1e-4f,
halfW *
halfW) + (ry * ry) / 0.58f;
712 if (
mask <= cover)
continue;
714 const float vein = std::pow(
715 std::fabs(std::sin(ry * 8.f + noise.valueNoise(
px * 0.35f,
py * 0.35f) * 2.5f)), 3.f);
716 const float mott = noise.fbm(
px * 0.28f +
u * 1.6f,
py * 0.28f +
v * 1.6f, 3);
717 shade = std::clamp(0.30f + mott * 0.42f + (1.f - vein) * 0.28f, 0.f, 1.f);
721 cover = std::clamp(cover * 0.55f + 0.45f, 0.f, 1.f);
730BarkSample sampleBark(
const NoiseField &
source,
float u,
float v,
float scale) {
731 NoiseField noise =
source;
732 noise.periodX = noise.periodY = 0;
733 const auto field = [&](
float x) {
735 const float warp = noise.fbm(
x * 5.f + 19.f,
y * 1.7f, 3) - 0.5f;
736 const float bend = noise.fbm(
x * 12.f,
y * 4.f + 37.f, 3) - 0.5f;
737 const float px =
x * 30.f +
warp * 3.5f + bend * 0.8f;
738 const float py =
y * 3.2f + noise.fbm(
x * 7.f,
y * 2.f, 2) * 1.2f;
741 const float edge = noise.worleyEdge(
px,
py);
743 const float chips = noise.fbm(
x * 64.f +
warp,
y * 18.f, 3);
744 const float grain = noise.fbm(
x * 180.f,
y * 55.f, 2);
745 const float weather = noise.fbm(
x * 9.f + 41.f,
y * 7.f, 3);
746 const float h = std::clamp(0.58f - fissure * 0.40f + (chips - 0.5f) * 0.26f + (
grain - 0.5f) * 0.10f, 0.f, 1.f);
748 h, std::clamp(0.48f + (weather - 0.5f) * 0.6f + (chips - 0.5f) * 0.18f - fissure * 0.30f, 0.f, 1.f),
749 std::clamp(0.77f + weather * 0.16f + fissure * 0.06f, 0.f, 1.f), 1.f - fissure * 0.35f};
753 return {
a.height + (
b.height -
a.height) *
blend,
a.pigment + (
b.pigment -
a.pigment) *
blend,
754 a.roughness + (
b.roughness -
a.roughness) *
blend,
a.ao + (
b.ao -
a.ao) *
blend};
757Rgba8 sampleBarkColor(
const NoiseField &noise,
float u,
float v,
float scale,
int colors) {
759 ramp.add(0.f, 28, 23, 19);
760 ramp.add(0.3f, 58, 47, 37);
761 ramp.add(0.6f, 99, 84, 66);
762 ramp.add(1.f, 145, 131, 108);
768std::unique_ptr<image::ImageData> genBarkAtlasMap(
const Params &
params,
const TextureGenContext &ctx,
float barkWidth,
769 std::string &
error) {
770 const std::string map =
params.getString(
"map",
"albedo");
771 if (map !=
"normal" && map !=
"surface") {
772 error =
"map must be albedo|normal|surface";
776 noise.seed = ctx.seed;
777 const int bw = std::max(2,
int(std::ceil(barkWidth *
float(ctx.width - 1))));
778 std::vector<BarkSample> samples(
size_t(bw * ctx.height));
779 for (
int y = 0;
y < ctx.height; ++
y)
780 for (
int x = 0;
x < bw; ++
x)
781 samples[
size_t(
y * bw +
x)] =
782 sampleBark(noise,
float(
x) /
float(bw - 1),
float(
y) /
float(std::max(1, ctx.height - 1)), ctx.scale);
783 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
784 const float strength = std::clamp(
params.getFloat(
"barkNormalStrength", 1.f), 0.f, 3.f);
785 for (
int y = 0;
y < ctx.height; ++
y) {
786 for (
int x = 0;
x < ctx.width; ++
x) {
789 const int column =
x % (bw - 1);
791 const int below = std::max(0,
y - 1), above = std::min(ctx.height - 1,
y + 1);
792 nx = -(samples[size_t(
y * bw +
right)].height - samples[size_t(
y * bw +
left)].height) *
float(bw - 1) *
794 ny = -(samples[size_t(above * bw +
x)].height - samples[size_t(below * bw +
x)].height) *
795 float(ctx.height - 1) * 0.008f *
strength;
796 const float inv = 1.f / std::sqrt(
nx *
nx +
ny *
ny + 1.f);
801 ao = samples[size_t(
y * bw +
x)].ao;
806 map ==
"surface" ? 1.f -
roughness : 1.f});
816std::unique_ptr<image::ImageData> genTreeAtlas(
const Params &
params, std::string &
error) {
817 const std::string
layout =
params.getString(
"leafLayout",
"cards");
819 error =
"leafLayout must be cards|single";
824 if (ctx.width > 4096 || ctx.height > 4096) {
825 error =
"texture size too large (max 4096)";
828 if (
params.getString(
"map",
"albedo") !=
"albedo")
return genBarkAtlasMap(
params, ctx, 0.48f,
error);
829 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
831 noise.seed = ctx.seed;
833 noise.periodX = std::max(1,
int(ctx.scale));
834 noise.periodY = noise.periodX;
838 leafRamp.add(0.00f, 14, 36, 10);
839 leafRamp.add(0.28f, 32, 78, 22);
840 leafRamp.add(0.52f, 52, 118, 34);
841 leafRamp.add(0.78f, 88, 148, 46);
842 leafRamp.add(1.00f, 24, 54, 16);
844 std::vector<std::vector<LeafStamp>> leafPanels;
845 buildSixLeafCardPanels(std::uint32_t(ctx.seed), noise, leafPanels);
847 const float invW = 1.f / float(std::max(1, ctx.width - 1));
848 const float invH = 1.f / float(std::max(1, ctx.height - 1));
849 for (
int y = 0;
y < ctx.height; ++
y) {
850 for (
int x = 0;
x < ctx.width; ++
x) {
851 const float u = float(
x) * invW;
852 const float v = float(
y) * invH;
855 color = sampleBarkColor(noise,
u / 0.48f,
v, ctx.scale, ctx.colors);
856 }
else if (
u > 0.52f) {
857 const float fu = (
u - 0.52f) / 0.48f;
858 float cover = 0.f, shade = 0.f;
860 sampleSingleLeafAtlas(noise, fu,
v, cover, shade);
862 sampleLeafCardAtlas(noise, fu,
v, leafPanels, cover, shade);
864 color = leafRamp.sample(shade);
865 color.a = uint8_t(std::clamp(cover, 0.f, 1.f) * 255.f);
866 }
else if (cover < 0.5f) {
867 color = {0, 0, 0, 0};
869 color = leafRamp.sample(shade);
873 color = {72, 86, 48, 255};
889std::unique_ptr<image::ImageData> genFoliage(
const Params &
params, std::string &
error) {
890 const std::string
layout =
params.getString(
"leafLayout",
"cards");
892 error =
"leafLayout must be cards|single";
897 if (ctx.width > 4096 || ctx.height > 4096) {
898 error =
"texture size too large (max 4096)";
901 if (
params.getString(
"map",
"albedo") !=
"albedo")
return genBarkAtlasMap(
params, ctx, 0.22f,
error);
902 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
904 noise.seed = ctx.seed;
906 noise.periodX = std::max(1,
int(ctx.scale));
907 noise.periodY = noise.periodX;
912 leafRamp.add(0.00f, 12, 32, 16);
913 leafRamp.add(0.28f, 28, 68, 34);
914 leafRamp.add(0.52f, 42, 96, 48);
915 leafRamp.add(0.78f, 62, 122, 58);
916 leafRamp.add(1.00f, 22, 48, 28);
918 std::vector<std::vector<LeafStamp>> leafPanels;
919 buildSixLeafCardPanels(std::uint32_t(ctx.seed), noise, leafPanels);
921 const float invW = 1.f / float(std::max(1, ctx.width - 1));
922 const float invH = 1.f / float(std::max(1, ctx.height - 1));
923 for (
int y = 0;
y < ctx.height; ++
y) {
924 for (
int x = 0;
x < ctx.width; ++
x) {
925 const float u = float(
x) * invW;
926 const float v = float(
y) * invH;
927 float cover = 0.f, shade = 0.f;
930 color = sampleBarkColor(noise,
u / 0.22f,
v, ctx.scale, ctx.colors);
932 }
else if (
u < 0.24f) {
933 color = {72, 64, 42, 255};
934 }
else if (
u <= 0.50f) {
935 const float fu = (
u - 0.24f) / 0.26f;
936 sampleFoliageFill(noise, fu,
v, ctx.scale, cover, shade);
937 color = leafRamp.sampleBanded(shade, ctx.colors);
939 }
else if (
u < 0.52f) {
940 color = {40, 78, 36, 255};
942 const float fu = (
u - 0.52f) / 0.48f;
944 sampleSingleLeafAtlas(noise, fu,
v, cover, shade);
946 sampleLeafCardAtlas(noise, fu,
v, leafPanels, cover, shade);
948 color = leafRamp.sample(shade);
949 color.a = uint8_t(std::clamp(cover, 0.f, 1.f) * 255.f);
950 }
else if (cover < 0.5f) {
951 color = {0, 0, 0, 0};
953 color = leafRamp.sample(shade);
969std::unique_ptr<image::ImageData> genFlower(
const Params &
params, std::string &
error) {
971 if (ctx.width > 4096 || ctx.height > 4096) {
972 error =
"texture size too large (max 4096)";
975 auto img = std::make_unique<image::ImageData>(ctx.width, ctx.height,
"RGBA8");
977 noise.seed = ctx.seed;
979 noise.periodX = std::max(1,
int(ctx.scale));
980 noise.periodY = noise.periodX;
984 stemRamp.add(0.00f, 28, 72, 28);
985 stemRamp.add(0.55f, 48, 110, 42);
986 stemRamp.add(1.00f, 34, 86, 32);
989 petalRamp.add(0.00f, 210, 200, 205);
990 petalRamp.add(0.40f, 245, 236, 240);
991 petalRamp.add(0.70f, 255, 250, 252);
992 petalRamp.add(1.00f, 255, 236, 180);
994 const float invW = 1.f / float(std::max(1, ctx.width - 1));
995 const float invH = 1.f / float(std::max(1, ctx.height - 1));
996 for (
int y = 0;
y < ctx.height; ++
y) {
997 for (
int x = 0;
x < ctx.width; ++
x) {
998 const float u = float(
x) * invW;
999 const float v = float(
y) * invH;
1002 const float su =
u / 0.32f;
1004 std::clamp(0.45f + 0.35f * noise.fbm(su * 2.f,
v * 4.f, 3) +
1005 0.2f * std::sin(
v * 18.f),
1007 color = stemRamp.sampleBanded(
h, ctx.colors);
1011 const float pu = (
u - 0.36f) / 0.62f;
1013 const float dx = (pu - 0.5f) * 2.f;
1014 const float dy = (pv - 0.5f) * 2.f;
1015 const float halfW = 0.72f * (1.f - 0.35f * std::max(0.f,
dy));
1018 const float cover = 1.f -
smoothstep(0.72f, 1.05f,
d);
1019 const float vein = std::pow(
1020 std::fabs(std::sin(
dy * 6.f + noise.valueNoise(pu * 2.f, pv * 2.f) * 2.f)),
1023 const float h = std::clamp(0.35f + (1.f - vein) * 0.25f + centre * 0.45f +
1024 noise.fbm(pu * 3.f, pv * 3.f, 2) * 0.1f,
1026 color = petalRamp.sampleBanded(
h, ctx.colors);
1027 color.a =
static_cast<uint8_t
>(std::clamp(cover, 0.f, 1.f) * 255.f);
1040 static const std::vector<TextureRecipeDef> defs = [] {
1041 auto result = buildDefs();
1049 if (builtinsRegistered_)
return;
1051 auto cloudDescriptor = [](std::string
id, std::string
name,
bool shadow) {
1069 registerRecipe(cloudDescriptor(
"tex.cloud",
"Cloud",
false), genCloud);
1070 registerRecipe(cloudDescriptor(
"tex.cloud_shadow",
"Cloud Shadow",
true), genCloudShadow);
1088 foliage.
params.push_back(
1108 builtinsRegistered_ =
true;
std::array< float, 3 > scale
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
medialoader::Colorf Colorf
Owning, typed generation parameters.
void registerRecipe(const std::string &id, TextureRecipeFn fn)
Register a recipe without metadata.
void registerBuiltins()
Register engine-provided recipes once.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
float smoothstep(float edge0, float edge1, float value)
void registerRoadTextureRecipes(TextureRecipeRegistry ®istry)
Registers road texture recipes.
void registerCableTextureRecipes(TextureRecipeRegistry ®istry)
Register tex.cable.steel / tex.chain.iron / tex.rope.hemp.
std::vector< TextureRecipeDef > potteryTextureDefinitions()
Create terracotta, glazed ceramic, porcelain, celadon and stoneware CPU texture definitions.
void registerPrototypeTextureRecipes(TextureRecipeRegistry ®istry)
Register all patterns as tex.prototype.* texture recipes.
void fillHeightField(const TextureGenContext &ctx, const std::function< float(float, float, const NoiseField &)> &fn, std::vector< float > &height)
Sample a height function over the full [w×h] grid into height (clamped to [0,1]).
PointSet poissonDiskPoints(int width, int depth, float radius, uint32_t seed, int maxPoints)
Bridson blue-noise (Poisson disk) samples in a width x depth area (XZ, y=0).
RecipeDescriptor makeTextureRecipeDescriptor(const TextureRecipeDef &definition)
Build editable metadata for a texture definition.
const std::vector< TextureRecipeDef > & builtinTextureDefs()
All built-in texture definitions (albedo + PBR). Populated lazily.
void paintHeightToImage(image::ImageData &img, const std::vector< float > &height, int w, int h, const ColorRamp &ramp, int bands, int pixelSize)
Paint height to image.
void registerFloorTextureRecipes(TextureRecipeRegistry ®istry)
Register tex.floor.wood and tex.floor.tile with schema metadata.
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
static ParamDescriptor boolean(std::string key, std::string label, bool defaultValue)
Construct a Boolean descriptor stored as zero or one in Params.
static ParamDescriptor choice(std::string key, std::string label, std::string defaultValue, std::vector< std::string > choices)
Construct a finite-choice string descriptor.
static ParamDescriptor floating(std::string key, std::string label, float defaultValue, float minimum, float maximum, float step)
Construct a bounded floating-point descriptor.
static ParamDescriptor integer(std::string key, std::string label, int defaultValue, int minimum, int maximum, int step=1)
Construct a bounded integer descriptor.
Complete metadata and parameter schema shared by every procgen recipe family.
static RecipeDescriptor grid(std::string id, std::string displayName, std::string category, int minimumWidth, int minimumHeight, int maximumWidth=4096, int maximumHeight=4096)
Create a schema with common seed, width and height parameters.
std::vector< ParamDescriptor > params
static EVENGINE_API_DOMAINS TextureGenContext fromParams(const Params ¶ms)
From params.
A procedural texture is described by an albedo ramp, displacement, optional independent PBR pigment f...