载入中...
搜索中...
未找到
TerrainStamp.cpp
浏览该文件的文档.
1// eve-hot-path
3#include "common/SmoothMax.h"
4
7
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <string>
12#include <vector>
13
14namespace eve::procgen {
15using namespace raster_detail;
16
18 bool invert) {
19 if (!validRaster(target) || !validRaster(source) || target.getWidth() != source.getWidth() ||
20 target.getHeight() != source.getHeight())
21 return invalid("terrain.mask: finite nonempty rasters with matching dimensions required");
22 if (!std::isfinite(strength) || strength < 0 || strength > 1 || mode < TerrainMaskBlend::Multiply ||
24 return invalid("terrain.mask: invalid blend mode or strength");
25 std::vector<float> candidate(target.data().size());
26 for (size_t i = 0; i < candidate.size(); ++i) {
27 const double a = target.data()[i], b = invert ? 1.0 - source.data()[i] : source.data()[i];
28 double value = a;
29 switch (mode) {
30 case TerrainMaskBlend::Multiply: value = a * b; break;
31 case TerrainMaskBlend::Maximum: value = std::max(a, b); break;
32 case TerrainMaskBlend::Minimum: value = std::min(a, b); break;
33 case TerrainMaskBlend::Add: value = a + b; break;
34 case TerrainMaskBlend::Subtract: value = a - b; break;
35 }
36 value = std::lerp(a, value, double(strength));
37 if (!isRepresentable(value)) return invalid("terrain.mask: result exceeds finite height range");
38 candidate[i] = float(value);
39 }
40 return publish(target, std::move(candidate));
41}
42
44 const Heightmap& localMask, const Heightmap& globalMask) {
45 if (!validRaster(target) || !validRaster(stamp) || !validRaster(localMask) || !validRaster(globalMask))
46 return invalid("terrain.stamp: finite nonempty rasters required");
47 for (double v : {s.originX, s.originZ, s.spacingX, s.spacingZ, s.centerX, s.centerZ, s.width, s.depth, s.rotation,
48 double(s.amplitude), double(s.baseHeight), double(s.blendStrength), double(s.smoothWidth),
49 double(s.edgeFade)})
50 if (!std::isfinite(v)) return invalid("terrain.stamp: settings must be finite");
51 if (s.spacingX <= 0 || s.spacingZ <= 0 || s.width <= 0 || s.depth <= 0 || s.blendStrength < 0 ||
52 s.blendStrength > 1 || s.operation < TerrainStampOperation::Raise ||
53 s.operation > TerrainStampOperation::SmoothRaise || s.smoothWidth < 0 || s.edgeFade < 0)
54 return invalid("terrain.stamp: invalid operation, extent, spacing or blend strength");
55
56 std::vector<float> candidate = target.data();
57 const double c = std::cos(s.rotation), sn = std::sin(s.rotation);
58 for (int y = 0; y < target.getHeight(); ++y) {
59 for (int x = 0; x < target.getWidth(); ++x) {
60 const double dx = (s.originX + x * s.spacingX) - s.centerX;
61 const double dz = (s.originZ + y * s.spacingZ) - s.centerZ;
62 double u = (c * dx + sn * dz) / s.width + 0.5;
63 double v = (-sn * dx + c * dz) / s.depth + 0.5;
64 if (!std::isfinite(u) || !std::isfinite(v))
65 return invalid("terrain.stamp: world-to-stamp coordinates overflow");
66 // A quarter-turn must not lose an edge sample to trigonometric roundoff.
67 constexpr double edgeTolerance = 8 * std::numeric_limits<double>::epsilon();
68 if (u < -edgeTolerance || u > 1 + edgeTolerance || v < -edgeTolerance || v > 1 + edgeTolerance) continue;
69 u = std::clamp(u, 0.0, 1.0);
70 v = std::clamp(v, 0.0, 1.0);
71 double weight = std::clamp(sample(globalMask, u, v), 0.0, 1.0);
72 if (s.operation == TerrainStampOperation::SmoothRaise && s.edgeFade > 0) {
73 const auto fade = [](double distance, double width) {
74 const double t = std::clamp(distance / width, 0.0, 1.0);
75 return t * t * (3.0 - 2.0 * t);
76 };
77 weight *=
78 fade(std::min(u, 1.0 - u) * s.width, s.edgeFade) * fade(std::min(v, 1.0 - v) * s.depth, s.edgeFade);
79 }
80 if (weight == 0) continue;
81 const double old = target.height(x, y);
82 const double level = s.baseHeight + s.amplitude * sample(stamp, u, v) * sample(localMask, u, v);
83 double next = old;
84 switch (s.operation) {
85 case TerrainStampOperation::Raise: next = std::max(old, level); break;
86 case TerrainStampOperation::Lower: next = std::min(old, level); break;
87 case TerrainStampOperation::Set: next = level; break;
88 case TerrainStampOperation::Blend: next = std::lerp(old, level, double(s.blendStrength)); break;
89 case TerrainStampOperation::Add: next = old + level; break;
90 case TerrainStampOperation::Subtract: next = old - level; break;
92 if (s.smoothWidth == 0 || std::abs(old - level) >= s.smoothWidth) {
93 next = std::max(old, level);
94 break;
95 }
97 return invalid("terrain.stamp: stamp level exceeds finite height range");
98 next = eve::math::smoothMax(float(old), float(level), s.smoothWidth);
99 break;
100 }
101 next = std::lerp(old, next, weight);
102 if (!isRepresentable(next)) return invalid("terrain.stamp: result exceeds finite height range");
103 candidate[size_t(y) * size_t(target.getWidth()) + size_t(x)] = float(next);
104 }
105 }
106 return publish(target, std::move(candidate));
107}
108} // namespace eve::procgen
double y
double x
LogicalId target
const std::string & s
float strength
float u
Definition Grass.cpp:233
float v
std::int32_t c
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
int level
glm::dvec3 dx
glm::dvec3 dz
Object::Ptr value
float t
std::string_view source
Heightmap stamp
double weight
Move-only operation result carrying either a value or Status.
Definition Result.h:176
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:21
float smoothMax(float a, float b, float width)
C1 polynomial maximum for blending two height fields.
Definition SmoothMax.h:24
double sample(const Heightmap &map, double u, double v)
Sample.
Result< int > publish(Heightmap &target, std::vector< float > candidate)
Publish.
bool validRaster(const Heightmap &map)
Valid raster.
Result< int > invalid(std::string message)
Invalid.
bool isRepresentable(double value)
True when representable.
Result< int > blendTerrainMask(Heightmap &target, const Heightmap &source, TerrainMaskBlend mode, float strength, bool invert)
Blend one scalar raster into an existing mask stack, in call order.
TerrainMaskBlend
Ordered scalar-mask arithmetic; intermediate values are not clamped.
Definition TerrainStamp.h:9
Result< int > applyTerrainStamp(Heightmap &target, const Heightmap &stamp, const TerrainStampSettings &s, const Heightmap &localMask, const Heightmap &globalMask)
Apply a rotated, bilinearly sampled stamp; publish only after all checks succeed.
Value configuration for a rectangular stamp in world X/Z coordinates.