载入中...
搜索中...
未找到
ProceduralDecal.cpp
浏览该文件的文档.
3
4#include "common/Diagnostic.h"
5
6#include <algorithm>
7#include <cerrno>
8#include <charconv>
9#include <cmath>
10#include <cstdlib>
11#include <limits>
12#include <optional>
13#include <string>
14#include <unordered_map>
15#include <utility>
16
17namespace eve::decal {
18namespace {
19
20Result<ProceduralDecalRecipe> recipeFailure(std::string message) {
22 Diagnostic::error(DiagnosticCode::InvalidArgument, std::move(message), "decal.procedural.preset"));
23}
24
25Result<ProceduralDecalBake> bakeFailure(std::string message) {
27 Diagnostic::error(DiagnosticCode::InvalidArgument, std::move(message), "decal.procedural.recipe"));
28}
29
30std::optional<std::string_view> attribute(std::string_view element, std::string_view name) {
31 const std::string needle = std::string(name) + "=\"";
32 const auto begin = element.find(needle);
33 if (begin == std::string_view::npos) return std::nullopt;
34 const auto valueBegin = begin + needle.size();
35 const auto valueEnd = element.find('"', valueBegin);
36 if (valueEnd == std::string_view::npos) return std::nullopt;
37 return element.substr(valueBegin, valueEnd - valueBegin);
38}
39
40std::optional<float> number(std::string_view text) {
41 if (text.empty()) return std::nullopt;
42 if (text.find_first_of(" \t\n\r\f\v") != std::string_view::npos) return std::nullopt;
43 const std::string copy(text);
44 char* end = nullptr;
45 errno = 0;
46 const float value = std::strtof(copy.c_str(), &end);
47 if (end != copy.c_str() + copy.size() || errno == ERANGE || !std::isfinite(value)) return std::nullopt;
48 return value;
49}
50
51std::optional<int> integer(std::string_view text) {
52 int value = 0;
53 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);
54 if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size()) return std::nullopt;
55 return value;
56}
57
58std::optional<std::array<float, 3>> color3(std::string_view text) {
59 std::array<float, 3> color{};
60 for (std::size_t channel = 0; channel < color.size(); ++channel) {
61 const auto comma = text.find(',');
62 const auto token = comma == std::string_view::npos ? text : text.substr(0, comma);
63 auto value = number(token);
64 if (!value) return std::nullopt;
65 color[channel] = *value;
66 if (channel + 1 < color.size()) {
67 if (comma == std::string_view::npos) return std::nullopt;
68 text.remove_prefix(comma + 1);
69 } else if (comma != std::string_view::npos) {
70 return std::nullopt;
71 }
72 }
73 return color;
74}
75
76DecalPattern importedPattern(int sourceId) {
77 // Substance exposes a larger pattern catalogue. Preserve deterministic variation while mapping it
78 // onto the five native pattern families rather than depending on Substance at runtime.
81 const auto magnitude = sourceId < 0 ? std::uint64_t(-std::int64_t(sourceId)) : std::uint64_t(sourceId);
82 return families[std::size_t(magnitude % std::size(families))];
83}
84
85float clamp01(float value) { return std::clamp(value, 0.f, 1.f); }
86
87float smoothStep(float edge0, float edge1, float value) {
88 const float t = clamp01((value - edge0) / (edge1 - edge0));
89 return t * t * (3.f - 2.f * t);
90}
91
92std::uint32_t hash(std::uint32_t value) {
93 value ^= value >> 16;
94 value *= 0x7feb352du;
95 value ^= value >> 15;
96 value *= 0x846ca68bu;
97 return value ^ (value >> 16);
98}
99
100float random01(std::uint32_t x, std::uint32_t y, std::uint32_t seed) {
101 return float(hash(x * 0x9e3779b9u ^ y * 0x85ebca6bu ^ seed)) / float(std::numeric_limits<std::uint32_t>::max());
102}
103
104float valueNoise(float x, float y, std::uint32_t seed) {
105 const int ix = static_cast<int>(std::floor(x));
106 const int iy = static_cast<int>(std::floor(y));
107 const float fx = x - float(ix);
108 const float fy = y - float(iy);
109 const float sx = fx * fx * (3.f - 2.f * fx);
110 const float sy = fy * fy * (3.f - 2.f * fy);
111 const auto sample = [seed](int px, int py) {
112 return random01(static_cast<std::uint32_t>(px), static_cast<std::uint32_t>(py), seed);
113 };
114 const float a = std::lerp(sample(ix, iy), sample(ix + 1, iy), sx);
115 const float b = std::lerp(sample(ix, iy + 1), sample(ix + 1, iy + 1), sx);
116 return std::lerp(a, b, sy);
117}
118
119float fractal(float x, float y, std::uint32_t seed) {
120 float value = 0.f;
121 float weight = 0.55f;
122 for (int octave = 0; octave < 4; ++octave) {
123 value += valueNoise(x, y, seed + std::uint32_t(octave) * 1013u) * weight;
124 x *= 2.03f;
125 y *= 2.03f;
126 weight *= 0.5f;
127 }
128 return value / 1.03125f;
129}
130
131float patternValue(DecalPattern pattern, int sourcePatternId, float u, float v, float scale,
132 std::uint32_t seed) {
133 const float x = (u - 0.5f) * scale;
134 const float y = (v - 0.5f) * scale;
135 const float radius = std::sqrt(x * x + y * y);
136 const float noise = fractal(x + 17.1f, y - 9.7f, seed);
137 const int family = sourcePatternId >= 0 ? sourcePatternId % 12 : static_cast<int>(pattern);
138 switch (family) {
139 case 0: {
140 const float cells = valueNoise(x * 2.1f, y * 2.1f, seed + 71u);
141 return clamp01((cells - 0.42f) * 2.8f) * clamp01(1.15f - radius * 0.55f);
142 }
143 case 1: {
144 const float ridge = 1.f - std::abs(noise * 2.f - 1.f);
145 return clamp01((ridge - 0.76f) * 5.2f) * clamp01(1.2f - radius * 0.45f);
146 }
147 case 2: {
148 const float streak = fractal(x * 0.45f, y * 3.8f, seed + 191u);
149 return clamp01((streak - 0.38f) * 2.1f) * clamp01(1.1f - std::abs(x) * 0.22f);
150 }
151 case 3: {
152 const float warpedRadius = radius * (0.78f + 0.42f * noise);
153 return 1.f - smoothStep(0.62f, 1.05f, warpedRadius);
154 }
155 case 4: return clamp01((noise - 0.28f) * 1.45f) * clamp01(1.25f - radius * 0.38f);
156 case 5: {
157 const float islands = fractal(x * 1.45f + noise * 0.55f, y * 1.45f - noise * 0.35f,
158 seed + 257u);
159 return clamp01((islands - 0.46f) * 2.65f) * clamp01(1.14f - radius * 0.48f);
160 }
161 case 6: {
162 const float scratches = fractal((x + y) * 4.8f, (y - x) * 0.38f, seed + 313u);
163 return clamp01((scratches - 0.56f) * 3.2f) * clamp01(1.18f - radius * 0.42f);
164 }
165 case 7: {
166 const float blobs = valueNoise(x * 1.35f, y * 1.35f, seed + 419u);
167 const float drops = valueNoise(x * 4.7f, y * 4.7f, seed + 431u);
168 return clamp01((std::max(blobs, drops * 0.82f) - 0.48f) * 2.7f) *
169 clamp01(1.15f - radius * 0.46f);
170 }
171 case 8: {
172 const float cells = valueNoise(x * 2.6f, y * 2.6f, seed + 557u);
173 const float edge = 1.f - std::abs(cells * 2.f - 1.f);
174 return clamp01((edge - 0.68f) * 4.1f) * clamp01(1.2f - radius * 0.44f);
175 }
176 case 9: {
177 const float smear = fractal(x * 2.8f + noise, y * 0.42f, seed + 631u);
178 return clamp01((smear - 0.4f) * 2.25f) * clamp01(1.1f - radius * 0.48f);
179 }
180 case 10: {
181 const float drops = valueNoise(x * 5.3f, y * 5.3f, seed + 743u);
182 return clamp01((drops - 0.63f) * 4.4f) * clamp01(1.16f - radius * 0.4f);
183 }
184 case 11: {
185 const float coarse = fractal(x * 0.72f, y * 0.72f, seed + 829u);
186 const float fine = fractal(x * 3.1f, y * 3.1f, seed + 853u);
187 return clamp01((coarse * 0.7f + fine * 0.3f - 0.38f) * 2.f) *
188 clamp01(1.22f - radius * 0.4f);
189 }
190 }
191 return 0.f;
192}
193
194std::vector<float> layerMask(const ProceduralDecalRecipe& recipe, const ProceduralDecalLayer& layer) {
195 const std::size_t count = std::size_t(recipe.width) * std::size_t(recipe.height);
196 std::vector<float> mask(count, 0.f);
197 if (!layer.enabled) return mask;
198 const float c = std::cos(layer.rotation);
199 const float s = std::sin(layer.rotation);
200 for (int y = 0; y < recipe.height; ++y) {
201 for (int x = 0; x < recipe.width; ++x) {
202 const float u = (float(x) + 0.5f) / float(recipe.width) - 0.5f;
203 const float v = (float(y) + 0.5f) / float(recipe.height) - 0.5f;
204 const float ru = c * u - s * v + 0.5f;
205 const float rv = s * u + c * v + 0.5f;
206 float value = patternValue(layer.pattern, layer.sourcePatternId, ru, rv, layer.scale,
207 recipe.seed ^ layer.seed);
208 if (layer.invertMask) value = 1.f - value;
209 value = clamp01((value - 0.5f) * layer.contrast + 0.5f);
210 mask[std::size_t(y) * std::size_t(recipe.width) + std::size_t(x)] = clamp01(value * layer.amount);
211 }
212 }
213 const int radius = std::clamp(static_cast<int>(std::round(layer.blur * 12.f)), 0, 12);
214 if (radius == 0) return mask;
215 std::vector<float> horizontal(count, 0.f);
216 std::vector<float> result(count, 0.f);
217 for (int y = 0; y < recipe.height; ++y) {
218 for (int x = 0; x < recipe.width; ++x) {
219 float sum = 0.f;
220 for (int dx = -radius; dx <= radius; ++dx)
221 sum += mask[std::size_t(y) * std::size_t(recipe.width) +
222 std::size_t(std::clamp(x + dx, 0, recipe.width - 1))];
223 horizontal[std::size_t(y) * std::size_t(recipe.width) + std::size_t(x)] = sum / float(radius * 2 + 1);
224 }
225 }
226 for (int y = 0; y < recipe.height; ++y) {
227 for (int x = 0; x < recipe.width; ++x) {
228 float sum = 0.f;
229 for (int dy = -radius; dy <= radius; ++dy)
230 sum += horizontal[std::size_t(std::clamp(y + dy, 0, recipe.height - 1)) * std::size_t(recipe.width) +
231 std::size_t(x)];
232 result[std::size_t(y) * std::size_t(recipe.width) + std::size_t(x)] = sum / float(radius * 2 + 1);
233 }
234 }
235 return result;
236}
237
238std::uint8_t byte(float value) { return static_cast<std::uint8_t>(std::round(clamp01(value) * 255.f)); }
239
240std::array<float, 3> rotateHue(const std::array<float, 3>& color, float angle) {
241 const float c = std::cos(angle);
242 const float s = std::sin(angle);
243 return {
244 clamp01(color[0] * (0.299f + 0.701f * c + 0.168f * s) +
245 color[1] * (0.587f - 0.587f * c + 0.330f * s) +
246 color[2] * (0.114f - 0.114f * c - 0.497f * s)),
247 clamp01(color[0] * (0.299f - 0.299f * c - 0.328f * s) +
248 color[1] * (0.587f + 0.413f * c + 0.035f * s) +
249 color[2] * (0.114f - 0.114f * c + 0.292f * s)),
250 clamp01(color[0] * (0.299f - 0.300f * c + 1.250f * s) +
251 color[1] * (0.587f - 0.588f * c - 1.050f * s) +
252 color[2] * (0.114f + 0.886f * c - 0.203f * s)),
253 };
254}
255
256bool finiteLayer(const ProceduralDecalLayer& layer) {
257 const float values[] = {layer.amount, layer.scale, layer.rotation, layer.blur,
258 layer.contrast, layer.color[0], layer.color[1], layer.color[2],
259 layer.colorVariation, layer.hueVariation, layer.roughness, layer.roughnessVariation,
260 layer.metallic, layer.emissive, layer.normalStrength, layer.normalSoftness,
261 layer.normalTrim, layer.normalThickness, layer.height};
262 return std::all_of(std::begin(values), std::end(values), [](float value) { return std::isfinite(value); });
263}
264
265bool validLayer(const ProceduralDecalLayer& layer) {
266 const auto inRange = [](float value, float minimum, float maximum) {
267 return value >= minimum && value <= maximum;
268 };
269 return finiteLayer(layer) && inRange(layer.amount, 0.f, 1.f) &&
270 inRange(layer.scale, 0.001f, 4096.f) && std::abs(layer.rotation) <= 1000000.f &&
271 inRange(layer.blur, 0.f, 1.f) && inRange(layer.contrast, 0.f, 64.f) &&
272 std::all_of(layer.color.begin(), layer.color.end(),
273 [&](float value) { return inRange(value, 0.f, 1.f); }) &&
274 inRange(layer.colorVariation, 0.f, 1.f) && inRange(layer.hueVariation, 0.f, 1.f) &&
275 inRange(layer.roughness, 0.f, 1.f) && inRange(layer.roughnessVariation, 0.f, 1.f) &&
276 inRange(layer.metallic, 0.f, 1.f) && inRange(layer.emissive, 0.f, 1.f) &&
277 inRange(layer.normalStrength, 0.f, 64.f) && inRange(layer.normalSoftness, 0.005f, 1.f) &&
278 inRange(layer.normalTrim, 0.f, 1.f) && inRange(layer.normalThickness, 0.1f, 16.f) &&
279 layer.normalStyle >= 0 && layer.normalStyle <= 16 && inRange(layer.height, 0.f, 1.f);
280}
281
282ProceduralDecalLayer layer(DecalPattern pattern, std::array<float, 3> color, float roughness, float metallic,
283 float height, std::uint32_t seed) {
284 ProceduralDecalLayer value;
285 value.pattern = pattern;
286 value.color = color;
287 value.roughness = roughness;
288 value.metallic = metallic;
289 value.height = height;
290 value.seed = seed;
291 return value;
292}
293
294} // namespace
295
298 recipe.seed = seed;
299 recipe.layerB.amount = 0.45f;
300 recipe.layerB.scale = 9.f;
301 recipe.layerB.blur = 0.04f;
302 if (name == "blood-wet") {
303 recipe.layerA = layer(DecalPattern::Puddle, {0.34f, 0.002f, 0.001f}, 0.04f, 0.f, 0.65f, 11u);
304 recipe.layerB = layer(DecalPattern::Spots, {0.62f, 0.03f, 0.02f}, 0.12f, 0.f, 0.42f, 12u);
305 } else if (name == "blood-dried") {
306 recipe.layerA = layer(DecalPattern::Spots, {0.16f, 0.012f, 0.006f}, 0.82f, 0.f, 0.28f, 21u);
307 recipe.layerB = layer(DecalPattern::Cracks, {0.07f, 0.004f, 0.002f}, 0.94f, 0.f, 0.36f, 22u);
308 } else if (name == "damage") {
309 recipe.layerA = layer(DecalPattern::Cracks, {0.055f, 0.05f, 0.045f}, 0.88f, 0.f, 0.82f, 31u);
310 recipe.layerA.normalStrength = 5.f;
311 recipe.layerB = layer(DecalPattern::Spots, {0.12f, 0.1f, 0.08f}, 0.75f, 0.f, 0.35f, 32u);
312 } else if (name == "dirt") {
313 recipe.layerA = layer(DecalPattern::Grunge, {0.22f, 0.14f, 0.07f}, 0.92f, 0.f, 0.38f, 41u);
314 recipe.layerB = layer(DecalPattern::Streaks, {0.32f, 0.24f, 0.12f}, 0.86f, 0.f, 0.22f, 42u);
315 } else if (name == "rust") {
316 recipe.layerA = layer(DecalPattern::Grunge, {0.42f, 0.075f, 0.018f}, 0.91f, 0.55f, 0.5f, 51u);
317 recipe.layerB = layer(DecalPattern::Spots, {0.12f, 0.025f, 0.008f}, 0.98f, 0.25f, 0.68f, 52u);
318 } else if (name == "puddle") {
319 recipe.layerA = layer(DecalPattern::Puddle, {0.035f, 0.045f, 0.05f}, 0.025f, 0.f, 0.08f, 61u);
320 recipe.layerB.enabled = false;
321 } else if (name == "paint") {
322 recipe.layerA = layer(DecalPattern::Spots, {0.12f, 0.32f, 0.8f}, 0.32f, 0.f, 0.42f, 71u);
323 recipe.layerB = layer(DecalPattern::Cracks, {0.025f, 0.025f, 0.025f}, 0.9f, 0.f, 0.62f, 72u);
324 } else if (name == "moss" || name == "mold" || name == "lichen") {
325 const std::array<float, 3> base =
326 name == "mold" ? std::array<float, 3>{0.08f, 0.11f, 0.075f} : std::array<float, 3>{0.12f, 0.25f, 0.06f};
327 recipe.layerA = layer(DecalPattern::Grunge, base, 0.96f, 0.f, 0.65f, 81u);
328 recipe.layerB = layer(DecalPattern::Spots, {0.24f, 0.36f, 0.09f}, 0.88f, 0.f, 0.32f, 82u);
329 } else {
330 return recipeFailure("Unknown procedural decal preset: " + std::string(name));
331 }
332 recipe.layerA.seed ^= seed;
333 recipe.layerB.seed ^= seed * 1664525u + 1013904223u;
335}
336
338 constexpr std::size_t maxPresetBytes = 1024u * 1024u;
339 if (xml.empty()) return recipeFailure("Substance preset document is empty");
340 if (xml.size() > maxPresetBytes) return recipeFailure("Substance preset document exceeds the 1 MiB limit");
341 if (xml.find("<sbspresets") == std::string_view::npos || xml.find("<sbspreset") == std::string_view::npos)
342 return recipeFailure("Input is not a Substance preset document");
343
344 std::unordered_map<std::string, std::string> inputs;
345 std::size_t cursor = 0;
346 while ((cursor = xml.find("<presetinput", cursor)) != std::string_view::npos) {
347 const auto end = xml.find("/>", cursor);
348 if (end == std::string_view::npos) return recipeFailure("Unterminated presetinput element");
349 const auto element = xml.substr(cursor, end + 2 - cursor);
350 const auto id = attribute(element, "identifier");
351 const auto value = attribute(element, "value");
352 if (!id || !value) return recipeFailure("presetinput requires identifier and value attributes");
353 inputs.insert_or_assign(std::string(*id), std::string(*value));
354 cursor = end + 2;
355 }
356 if (inputs.empty()) return recipeFailure("Substance preset contains no inputs");
357
359 bool malformedKnownInput = false;
360 const auto readFloat = [&](std::string_view name) -> std::optional<float> {
361 const auto found = inputs.find(std::string(name));
362 if (found == inputs.end()) return std::nullopt;
363 auto value = number(found->second);
364 malformedKnownInput = malformedKnownInput || !value.has_value();
365 return value;
366 };
367 const auto readInt = [&](std::string_view name) -> std::optional<int> {
368 const auto found = inputs.find(std::string(name));
369 if (found == inputs.end()) return std::nullopt;
370 auto value = integer(found->second);
371 malformedKnownInput = malformedKnownInput || !value.has_value();
372 return value;
373 };
374 const auto applyLayer = [&](std::string_view prefix, ProceduralDecalLayer& target) -> bool {
375 const std::string key(prefix);
376 if (const auto value = readFloat(key + "amount")) target.amount = clamp01(*value);
377 if (const auto value = readInt(key + "pattern")) {
378 target.sourcePatternId = *value;
379 target.pattern = importedPattern(*value);
380 }
381 if (const auto value = readFloat(key + "tweak")) target.scale = 1.f + clamp01(*value) * 11.f;
382 if (const auto value = readFloat(key + "tile")) target.scale *= std::clamp(*value, 0.25f, 8.f);
383 if (const auto value = readInt(key + "mask_invert")) target.invertMask = *value != 0;
384 if (const auto value = readFloat(key + "blur")) target.blur = std::max(0.f, *value) * 0.15f;
385 if (const auto value = readFloat(key + "contrast")) target.contrast = 0.5f + std::max(0.f, *value) * 2.f;
386 const auto colorIt = inputs.find(key + "color");
387 if (colorIt != inputs.end()) {
388 auto value = color3(colorIt->second);
389 if (!value) return false;
390 target.color = *value;
391 }
392 if (const auto value = readFloat(key + "color_value_variation"))
393 target.colorVariation = clamp01(*value);
394 if (const auto value = readFloat(key + "color_hue_variation")) target.hueVariation = clamp01(*value);
395 if (const auto value = readFloat(key + "roughness")) target.roughness = clamp01(*value);
396 if (const auto value = readFloat(key + "rough_variation"))
397 target.roughnessVariation = clamp01(*value);
398 if (const auto value = readFloat(key + "metallic")) target.metallic = clamp01(*value);
399 if (const auto value = readFloat(key + "emissive")) target.emissive = clamp01(*value);
400 if (const auto value = readFloat(key + "normal")) target.normalStrength = std::abs(*value);
401 if (const auto value = readFloat(key + "normal_softness"))
402 target.normalSoftness = std::clamp(*value, 0.005f, 1.f);
403 if (const auto value = readFloat(key + "normal_trim")) target.normalTrim = clamp01(*value);
404 if (const auto value = readFloat(key + "normal_thickness"))
405 target.normalThickness = std::clamp(*value, 0.1f, 16.f);
406 if (const auto value = readInt(key + "normal_style")) target.normalStyle = *value;
407 if (const auto value = readFloat(key + "height")) target.height = clamp01(*value);
408 if (const auto value = readFloat(key + "color_opacity")) target.amount *= clamp01(*value);
409 return true;
410 };
411
412 if (const auto seed = readInt("$randomseed")) recipe.seed = static_cast<std::uint32_t>(*seed);
413 const auto outputSize = inputs.find("$outputsize");
414 if (outputSize != inputs.end()) {
415 const auto comma = outputSize->second.find(',');
416 if (comma == std::string::npos) return recipeFailure("$outputsize must contain width,height exponents");
417 const auto x = integer(std::string_view(outputSize->second).substr(0, comma));
418 const auto y = integer(std::string_view(outputSize->second).substr(comma + 1));
419 if (!x || !y || *x < 0 || *y < 0 || *x > 12 || *y > 12)
420 return recipeFailure("$outputsize exponents must be in [0, 12]");
421 recipe.width = 1 << *x;
422 recipe.height = 1 << *y;
423 }
424 if (!applyLayer("a_", recipe.layerA) || !applyLayer("b_", recipe.layerB))
425 return recipeFailure("Substance preset contains an invalid color value");
426 if (const auto value = readFloat("ab_height_masking"))
427 recipe.layerHeightBlend = clamp01((*value + 1.f) * 0.5f);
428 if (const auto value = readFloat("ab_balance")) recipe.layerBalance = clamp01(*value);
429 if (const auto value = readInt("ab_blendmode")) recipe.layerBlendMode = *value;
430 if (malformedKnownInput)
431 return recipeFailure("Substance preset contains a malformed recognized numeric value");
432 recipe.layerA.seed = recipe.seed ^ 0x9e3779b9u;
433 recipe.layerB.seed = recipe.seed ^ 0x85ebca6bu;
435}
436
439 return bakeFailure("Unsupported procedural decal recipe schema version");
440 if (recipe.width <= 0 || recipe.height <= 0 || recipe.width > 4096 || recipe.height > 4096)
441 return bakeFailure("Procedural decal dimensions must be in [1, 4096]");
442 if (!validLayer(recipe.layerA) || !validLayer(recipe.layerB) ||
443 !std::isfinite(recipe.layerHeightBlend) || recipe.layerHeightBlend < 0.f ||
444 recipe.layerHeightBlend > 1.f || !std::isfinite(recipe.layerBalance) ||
445 recipe.layerBalance < 0.f || recipe.layerBalance > 1.f || recipe.layerBlendMode < 0 ||
446 recipe.layerBlendMode > 2 || !std::isfinite(recipe.opacity) || recipe.opacity < 0.f ||
447 recipe.opacity > 1.f)
448 return bakeFailure("Procedural decal recipe contains values outside supported ranges");
449
450 const auto maskA = layerMask(recipe, recipe.layerA);
451 const auto maskB = layerMask(recipe, recipe.layerB);
452 const std::size_t pixels = std::size_t(recipe.width) * std::size_t(recipe.height);
454 bake.width = recipe.width;
455 bake.height = recipe.height;
456 bake.albedo.resize(pixels * 4u);
457 bake.normal.resize(pixels * 4u);
458 bake.params.resize(pixels * 4u);
459 std::vector<float> height(pixels, 0.f);
460 std::vector<float> normalHeight(pixels, 0.f);
461 std::vector<float> coverage(pixels, 0.f);
462 for (std::size_t index = 0; index < pixels; ++index) {
463 const float a = maskA[index] * (2.f * (1.f - recipe.layerBalance));
464 const float b = maskB[index] * (2.f * recipe.layerBalance);
465 const float bWeight =
466 b * clamp01(0.5f + (recipe.layerB.height - recipe.layerA.height) * recipe.layerHeightBlend);
467 const float combined = recipe.layerBlendMode == 2 ? std::max(a, bWeight) : a + bWeight * (1.f - a);
468 const float alpha = clamp01(combined) * recipe.opacity;
469 const float mixB = alpha > 1e-6f ? bWeight / std::max(a + bWeight, 1e-6f) : 0.f;
470 const float variation =
471 random01(static_cast<std::uint32_t>(index % std::size_t(recipe.width)),
472 static_cast<std::uint32_t>(index / std::size_t(recipe.width)), recipe.seed + 991u) *
473 2.f -
474 1.f;
475 const float hueNoise = random01(static_cast<std::uint32_t>(index % std::size_t(recipe.width)),
476 static_cast<std::uint32_t>(index / std::size_t(recipe.width)),
477 recipe.seed + 1877u) * 2.f - 1.f;
478 const auto colorA = rotateHue(recipe.layerA.color, hueNoise * recipe.layerA.hueVariation * 3.14159265f);
479 const auto colorB = rotateHue(recipe.layerB.color, hueNoise * recipe.layerB.hueVariation * 3.14159265f);
480 for (int channel = 0; channel < 3; ++channel) {
481 const float color = std::lerp(colorA[channel], colorB[channel], mixB);
482 const float colorVar = std::lerp(recipe.layerA.colorVariation, recipe.layerB.colorVariation, mixB);
483 bake.albedo[index * 4u + std::size_t(channel)] = byte(color * (1.f + variation * colorVar));
484 }
485 bake.albedo[index * 4u + 3u] = byte(alpha);
486 const float roughness =
487 std::lerp(recipe.layerA.roughness, recipe.layerB.roughness, mixB) +
488 variation * std::lerp(recipe.layerA.roughnessVariation, recipe.layerB.roughnessVariation, mixB);
489 bake.params[index * 4u] = byte(roughness);
490 bake.params[index * 4u + 1u] = byte(std::lerp(recipe.layerA.metallic, recipe.layerB.metallic, mixB));
491 bake.params[index * 4u + 2u] = byte(std::lerp(recipe.layerA.emissive, recipe.layerB.emissive, mixB));
492 height[index] = clamp01(a * recipe.layerA.height + bWeight * recipe.layerB.height);
493 const auto normalLayer = [](float mask, const ProceduralDecalLayer& layer) {
494 const float edge0 = std::min(layer.normalTrim * 0.9f, 0.95f);
495 float shaped = smoothStep(edge0, std::min(1.f, edge0 + layer.normalSoftness), clamp01(mask));
496 if (layer.normalStyle > 0)
497 shaped = std::pow(shaped, 0.65f + 0.1f * float(layer.normalStyle % 5));
498 const float thickness = 0.5f + std::log2(1.f + layer.normalThickness) * 0.75f;
499 return shaped * layer.height * thickness;
500 };
501 normalHeight[index] = clamp01(normalLayer(a, recipe.layerA) + normalLayer(bWeight, recipe.layerB));
502 bake.params[index * 4u + 3u] = byte(height[index]);
503 coverage[index] = alpha;
504 }
505 for (int y = 0; y < recipe.height; ++y) {
506 for (int x = 0; x < recipe.width; ++x) {
507 const auto at = [&](int px, int py) {
508 return normalHeight[std::size_t(std::clamp(py, 0, recipe.height - 1)) * std::size_t(recipe.width) +
509 std::size_t(std::clamp(px, 0, recipe.width - 1))];
510 };
511 const std::size_t index = std::size_t(y) * std::size_t(recipe.width) + std::size_t(x);
512 const float layerMix = maskB[index] / std::max(maskA[index] + maskB[index], 1e-6f);
513 const float strength = std::lerp(recipe.layerA.normalStrength, recipe.layerB.normalStrength, layerMix);
514 float nx = (at(x - 1, y) - at(x + 1, y)) * strength;
515 float ny = (at(x, y - 1) - at(x, y + 1)) * strength;
516 const float invLength = 1.f / std::sqrt(nx * nx + ny * ny + 1.f);
517 nx *= invLength;
518 ny *= invLength;
519 const float nz = invLength;
520 bake.normal[index * 4u] = byte(nx * 0.5f + 0.5f);
521 bake.normal[index * 4u + 1u] = byte(ny * 0.5f + 0.5f);
522 bake.normal[index * 4u + 2u] = byte(nz * 0.5f + 0.5f);
523 bake.normal[index * 4u + 3u] = byte(coverage[index]);
524 }
525 }
526 return Result<ProceduralDecalBake>::success(std::move(bake));
527}
528
529} // namespace eve::decal
double y
double x
LogicalId target
std::size_t cursor
const std::string & s
int mask
Stable, structured diagnostics shared by engine modules.
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::uint64_t sourceId
std::string pattern
float strength
std::string message
float maximum[3]
float minimum[3]
float u
Definition Grass.cpp:233
int inputs
Definition GridGraph.cpp:23
float v
std::int32_t c
std::vector< Colorf > px
std::uint32_t height
std::array< float, 3 > scale
float thickness
std::string coverage
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float roughness
float metallic
OceanGpuFrameToken token
glm::dvec3 dx
BakeRecipe recipe
TileLayer * layer
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
std::set< std::string > values
Object::Ptr value
std::string hash
float t
uint8_t * pixels
std::string attribute
double number
bool found
std::string text
size_t begin
size_t end
float dy
Definition Shadow.cpp:151
std::string element
int count
Share string attribute map operations through the Windows Foundation DLL.
uint32_t seed
Definition TreeMesh.cpp:276
std::size_t at
double weight
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 Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
float smoothStep(float value) noexcept
Smooth step.
Result< ProceduralDecalBake > bakeProceduralDecal(const ProceduralDecalRecipe &recipe)
Bake a two-layer decal recipe into renderer-ready texture bytes.
Result< ProceduralDecalRecipe > proceduralDecalPreset(std::string_view name, std::uint32_t seed)
Build a deterministic preset recipe.
DecalPattern
Built-in procedural masks used by a procedural decal layer.
Result< ProceduralDecalRecipe > importProceduralDecalSbsprs(std::string_view xml)
Import one Substance .sbsprs preset into the native version-two recipe.
double sample(const Heightmap &map, double u, double v)
Sample.
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
Owning, renderer-neutral output of one decal bake.
std::vector< std::uint8_t > albedo
std::vector< std::uint8_t > params
std::vector< std::uint8_t > normal
One independently art-directed layer of a procedural decal recipe.
Versioned two-layer authoring recipe analogous to Procedural Decal's Standard graph and material-inst...
static constexpr std::uint32_t kSchemaVersion
glm::vec4 color