载入中...
搜索中...
未找到
EverydayMaterials.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <array>
4#include <cmath>
6
7namespace eve::procgen {
8namespace {
9struct Profile {
10 Rgba8 color;
12};
13constexpr std::array<Profile, 13> profiles{{{{178, 132, 80}, .44f, 0.f, .16f},
14 {{180, 183, 188}, .28f, 1.f, .03f},
15 {{44, 45, 47}, .96f, 0.f, .55f},
16 {{83, 119, 171}, .94f, 0.f, .55f},
17 {{189, 25, 30}, .19f, 0.f, .22f},
18 {{225, 229, 234}, .055f, 1.f, .006f},
19 {{238, 249, 244}, .035f, 0.f, .01f},
20 {{235, 227, 211}, .90f, 0.f, .35f},
21 {{225, 226, 228}, .31f, 1.f, .04f},
22 {{88, 86, 82}, .89f, 1.f, .85f},
23 {{164, 159, 149}, .93f, 0.f, .75f},
24 {{22, 76, 160}, .26f, 0.f, .40f},
25 {{235, 230, 217}, .14f, 0.f, .10f}}};
26float control(const Params &p, const char *key, float fallback, float lo = 0.f, float hi = 1.f) {
27 return std::clamp(p.getFloat(key, fallback), lo, hi);
28}
29} // namespace
30
33 const auto index = static_cast<size_t>(family);
34 if (index >= profiles.size() || params.getWidth() > 2048 || params.getHeight() > 2048)
35 return Output::failure(
37 "Everyday material requires a known family and dimensions at most 2048"));
38 const auto profile = profiles[index];
39 const float detail = control(params, "detail", .5f);
40 const float variation = control(params, "variation", .4f);
41 const float finish = control(params, "finish", .5f);
42 // Material scale is independent of output resolution. Supersample unresolved maps
43 // instead of changing the size or count of physical features.
44 const int repeats = int(std::round(control(params, "scale", 4.f, 1.f, 32.f)));
45 const float du = 1.f / float(params.getWidth()), dv = 1.f / float(params.getHeight());
46 const bool prefilter = std::min(params.getWidth(), params.getHeight()) < repeats * 180;
47 auto sample = [family, seed = params.getSeed(), repeats, du, dv, prefilter](float u, float v) {
48 if (!prefilter) return detail::sampleEveryday(family, u, v, seed, repeats, 0);
49 detail::EverydaySample sum{0.f, 0.f, 0.f};
50 for (float y : {-.25f, .25f})
51 for (float x : {-.25f, .25f}) {
52 const auto s = detail::sampleEveryday(family, u + x * du, v + y * dv, seed, repeats, 0);
53 sum.pigment += s.pigment * .25f;
54 sum.relief += s.relief * .25f;
55 sum.roughness += s.roughness * .25f;
56 }
57 return sum;
58 };
60 def.id = "everyday";
61 def.height = [sample, profile, detail](float u, float v, const NoiseField &) {
62 return .5f + (sample(u, v).relief - .5f) * profile.relief * (.25f + 1.5f * detail);
63 };
64 def.colorField = [sample, variation](float u, float v, const NoiseField &) {
65 return .5f + (sample(u, v).pigment - .5f) * (variation * 2.f);
66 };
67 def.roughnessField = [sample](float u, float v, const NoiseField &) { return sample(u, v).roughness; };
68 const Rgba8 tint{static_cast<uint8_t>(control(params, "tintR", profile.color.r / 255.f) * 255.f),
69 static_cast<uint8_t>(control(params, "tintG", profile.color.g / 255.f) * 255.f),
70 static_cast<uint8_t>(control(params, "tintB", profile.color.b / 255.f) * 255.f), 255};
71 if (family == EverydayMaterial::Marble || family == EverydayMaterial::Wood) {
72 auto mineral = [&](float t, float r, float g, float b) {
73 def.albedo.add(t, uint8_t(std::clamp(r * tint.r, 0.f, 255.f)), uint8_t(std::clamp(g * tint.g, 0.f, 255.f)),
74 uint8_t(std::clamp(b * tint.b, 0.f, 255.f)));
75 };
76 if (family == EverydayMaterial::Wood) {
77 mineral(0.f, .25f, .19f, .13f);
78 mineral(.30f, .55f, .50f, .40f);
79 mineral(.50f, 1.f, 1.f, 1.f);
80 mineral(.70f, 1.25f, 1.32f, 1.45f);
81 mineral(1.f, 1.35f, 1.55f, 1.90f);
82 } else {
83 mineral(0.f, .25f, .26f, .27f);
84 mineral(.28f, .63f, .59f, .51f);
85 mineral(.40f, .91f, .86f, .76f);
86 mineral(.50f, 1.f, 1.f, 1.f);
87 mineral(.75f, 1.03f, 1.04f, 1.07f);
88 mineral(1.f, 1.08f, 1.10f, 1.14f);
89 }
90 } else {
91 def.albedo.add(0.f, uint8_t(tint.r * .35f), uint8_t(tint.g * .35f), uint8_t(tint.b * .35f));
92 def.albedo.add(.5f, tint.r, tint.g, tint.b);
93 def.albedo.add(1.f, uint8_t(tint.r + (255 - tint.r) * .2f), uint8_t(tint.g + (255 - tint.g) * .2f),
94 uint8_t(tint.b + (255 - tint.b) * .2f));
95 }
96 const float roughness = std::clamp(profile.roughness + (.5f - finish) * .26f, .025f, 1.f);
97 def.pbr.roughnessLow = std::max(.025f, roughness - .09f);
98 def.pbr.roughnessHigh = std::min(1.f, roughness + .09f);
99 def.pbr.metallic = profile.metallic;
100 def.pbr.normalStrength = float(std::min(params.getWidth(), params.getHeight())) / 80.f;
101 def.pbr.aoStrength = family == EverydayMaterial::Glass ? 0.f : 1.f;
102 Params effective;
103 effective.setSize(params.getWidth(), params.getHeight());
104 effective.setSeed(params.getSeed());
105 effective.setFloat("scale", 1.f);
106 effective.setInt("colors", 256);
107 effective.setBool("seamless", true);
108 std::string error;
109 auto maps = generatePbrSet(def, effective, error);
110 if (!maps) return Output::failure(eve::Diagnostic::error(eve::DiagnosticCode::Failed, error));
111 if (family == EverydayMaterial::Glass) {
112 maps->transmission = .96f;
113 maps->ior = 1.5f;
114 }
115 return Output::success(std::move(maps));
116}
117} // namespace eve::procgen
double y
double x
const std::string & s
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec4 tint
float u
Definition Grass.cpp:233
double r
float v
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
float roughness
float metallic
std::string error
Definition Package.cpp:174
int detail
float t
float relief
Definition TreeMesh.cpp:274
uint32_t seed
Definition TreeMesh.cpp:276
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static eve::Result< std::unique_ptr< PbrTextureSet > > generate(EverydayMaterial family, const Params &params)
Generate six aligned maps and optical surface metadata.
Owning, typed generation parameters.
Definition Params.h:27
void setSize(int width, int height)
Set the generation dimensions, clamping each non-positive value to one.
Definition Params.cpp:142
void setInt(const std::string &key, int value)
Store an Int64 parameter, preserving its numeric Value kind.
Definition Params.cpp:149
void setBool(const std::string &key, bool value)
Stores a boolean generation parameter.
Definition Params.cpp:167
void setSeed(uint32_t seed)
Set the deterministic seed used by generation.
Definition Params.cpp:139
void setFloat(const std::string &key, float value)
Store a finite or non-finite floating parameter as Value::Double without text conversion.
Definition Params.cpp:165
EverydaySample sampleEveryday(EverydayMaterial family, float u, float v, uint32_t seed, int repeats, int bandwidth)
Pure seeded surface sampling; no state, IO, callbacks or retained borrows.
double sample(const Heightmap &map, double u, double v)
Sample.
std::unique_ptr< PbrTextureSet > generatePbrSet(const TextureRecipeDef &def, const Params &params, std::string &error)
Builds a full PBR set from a texture definition and returns unique ownership.
EverydayMaterial
Everyday surface families; no scene, GPU or registry state is retained.
void add(float t, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds .
Definition ColorRamp.cpp:8
Deterministic 2D noise helpers for pixel textures and masks.
Definition NoiseField.h:24
Rgba8 public API.
Definition ColorRamp.h:11
A procedural texture is described by an albedo ramp, displacement, optional independent PBR pigment f...
std::function< float(float, float, const NoiseField &)> height
Float.
std::function< float(float, float, const NoiseField &)> colorField
Optional pigment field, independent of physical relief; empty uses height.
std::function< float(float, float, const NoiseField &)> roughnessField
Optional independent microfacet field, mapped through roughnessLow/High.
Internal pigment, physical relief and microfacet variation in a common periodic UV domain.
glm::vec4 color