载入中...
搜索中...
未找到
TerrainSplatmap.cpp
浏览该文件的文档.
3
4#include <algorithm>
5#include <cmath>
6#include <cstdint>
7#include <limits>
8#include <unordered_set>
9#include <vector>
10
15
16namespace eve::procgen {
17namespace {
18
19float fade(float value) { return value * value * value * (value * (value * 6.f - 15.f) + 10.f); }
20float seamHash(int x, int y) {
21 std::uint32_t value = std::uint32_t(x) * 0x8da6b343u ^ std::uint32_t(y) * 0xd8163841u;
22 value ^= value >> 13;
23 value *= 0xcb1ab31fu;
24 value ^= value >> 16;
25 return float(value & 0x00ffffffu) / float(0x00ffffffu);
26}
27float seamNoise(float x, float y) {
28 const int x0 = static_cast<int>(std::floor(x)), y0 = static_cast<int>(std::floor(y));
29 const float tx = fade(x - float(x0)), ty = fade(y - float(y0));
30 const float a = std::lerp(seamHash(x0, y0), seamHash(x0 + 1, y0), tx);
31 const float b = std::lerp(seamHash(x0, y0 + 1), seamHash(x0 + 1, y0 + 1), tx);
32 return std::lerp(a, b, ty);
33}
34} // namespace
40 std::vector<Layer> layers;
41};
42GtsHeightBlendSet::GtsHeightBlendSet() : impl_(std::make_unique<Impl>()) {}
45GtsHeightBlendSet& GtsHeightBlendSet::operator=(GtsHeightBlendSet&&) noexcept = default;
46Result<int> GtsHeightBlendSet::addLayer(const Heightmap& height, float contrast, float brightness, float increase) {
47 using namespace raster_detail;
48 if (!validRaster(height) || !std::isfinite(contrast) || contrast < 0 || !std::isfinite(brightness) ||
49 !std::isfinite(increase)) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.gtsHeightBlend: finite layer transform required"));
50 if (!impl_) impl_ = std::make_unique<Impl>();
51 if (impl_->layers.size() >= 8) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.gtsHeightBlend: at most eight layers supported"));
52 if (!impl_->layers.empty() && (height.getWidth() != impl_->layers[0].height.getWidth() ||
53 height.getHeight() != impl_->layers[0].height.getHeight()))
54 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.gtsHeightBlend: all height rasters must match"));
55 impl_->layers.push_back({height, contrast, brightness, increase});
56 return Result<int>::success(static_cast<int>(impl_->layers.size()));
57}
58int GtsHeightBlendSet::getLayerCount() const noexcept { return impl_ ? static_cast<int>(impl_->layers.size()) : 0; }
59TerrainSplatmap::TerrainSplatmap() : impl_(std::make_unique<Impl>()) {}
62TerrainSplatmap& TerrainSplatmap::operator=(TerrainSplatmap&&) noexcept = default;
64 : impl_(other.impl_ ? std::make_unique<Impl>(*other.impl_) : std::make_unique<Impl>()) {}
66 if (this != &other) {
67 if (!impl_) impl_ = std::make_unique<Impl>();
68 *impl_ = other.impl_ ? *other.impl_ : Impl{};
69 }
70 return *this;
71}
72Result<int> TerrainSplatmap::initialize(int width, int height, int layers, int defaultLayer) {
73 if (width <= 0 || height <= 0 || layers <= 0 || defaultLayer < 0 || defaultLayer >= layers)
74 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splatmap: positive topology and valid default layer required"));
75 const auto texels = std::uint64_t(width) * std::uint64_t(height);
76 if (texels > std::numeric_limits<std::size_t>::max() / std::uint64_t(layers) ||
77 texels * std::uint64_t(layers) > std::numeric_limits<int>::max())
78 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splatmap: topology exceeds supported size"));
79 if (!impl_) impl_ = std::make_unique<Impl>();
80 Impl next;
81 next.width = width; next.height = height; next.layers = layers;
82 next.weights.assign(static_cast<std::size_t>(texels) * layers, 0);
83 for (int y = 0; y < height; ++y)
84 for (int x = 0; x < width; ++x) next.weights[next.index(defaultLayer, x, y)] = 1;
85 *impl_ = std::move(next);
86 return Result<int>::success(static_cast<int>(texels));
87}
89 if (!impl_ || impl_->layers <= 0) return Result<float>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splatmap: initialize before sampling"));
90 if (layer < 0 || layer >= impl_->layers || x < 0 || x >= impl_->width || y < 0 || y >= impl_->height)
91 return Result<float>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splatmap: sample index out of range"));
92 return Result<float>::success(impl_->weights[impl_->index(layer, x, y)]);
93}
94
96 if (!impl_ || impl_->width <= 0 || layer < 0 || layer >= impl_->layers || output.getWidth() != impl_->width ||
97 output.getHeight() != impl_->height)
98 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.copyLayer: initialized source, valid layer and matching output required"));
99 std::vector<float> candidate(size_t(impl_->width) * impl_->height);
100 for (int y = 0; y < impl_->height; ++y)
101 for (int x = 0; x < impl_->width; ++x)
102 candidate[size_t(y) * impl_->width + x] = impl_->weights[impl_->index(layer, x, y)];
103 int changed = 0;
104 for (size_t i = 0; i < candidate.size(); ++i) changed += candidate[i] != output.data()[i];
105 output.data().swap(candidate);
107}
108int TerrainSplatmap::getLastChangedSamples() const noexcept { return impl_ ? impl_->lastChanged : 0; }
109int TerrainSplatmap::getWidth() const noexcept { return impl_ ? impl_->width : 0; }
110int TerrainSplatmap::getHeight() const noexcept { return impl_ ? impl_->height : 0; }
111int TerrainSplatmap::getLayerCount() const noexcept { return impl_ ? impl_->layers : 0; }
112
115 if (&terrainA == &terrainB || !terrainA.impl_ || !terrainB.impl_ || terrainA.impl_->layers <= 0 ||
116 terrainB.impl_->layers <= 0 || terrainA.impl_->width <= 0 || terrainA.impl_->height <= 0 ||
117 terrainB.impl_->width <= 0 || terrainB.impl_->height <= 0 ||
118 terrainA.impl_->width != terrainA.impl_->height || terrainB.impl_->width != terrainB.impl_->height ||
119 !std::isfinite(settings.terrainAOriginX) || !std::isfinite(settings.terrainAOriginZ) ||
120 !std::isfinite(settings.terrainAWidth) || !std::isfinite(settings.terrainADepth) ||
121 !std::isfinite(settings.terrainBOriginX) || !std::isfinite(settings.terrainBOriginZ) ||
122 !std::isfinite(settings.terrainBWidth) || !std::isfinite(settings.terrainBDepth) ||
123 !std::isfinite(settings.blendStrength) || !std::isfinite(settings.adjacencyTolerance) ||
124 settings.terrainAWidth <= 0 || settings.terrainADepth <= 0 || settings.terrainBWidth <= 0 ||
125 settings.terrainBDepth <= 0 || settings.blendStrength < 0 || settings.blendStrength > 1 ||
126 settings.blendWidth <= 0 || settings.adjacencyTolerance <= 0)
127 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.textureAlign: distinct initialized square maps and valid finite settings required"));
128
129 enum class Edge { None, Left, Right, Top, Bottom };
130 Edge edgeA = Edge::None, edgeB = Edge::None;
131 const double aMinX = settings.terrainAOriginX, aMaxX = aMinX + settings.terrainAWidth;
132 const double aMinZ = settings.terrainAOriginZ, aMaxZ = aMinZ + settings.terrainADepth;
133 const double bMinX = settings.terrainBOriginX, bMaxX = bMinX + settings.terrainBWidth;
134 const double bMinZ = settings.terrainBOriginZ, bMaxZ = bMinZ + settings.terrainBDepth;
135 const auto near = [&](double a, double b) { return std::abs(a - b) < settings.adjacencyTolerance; };
136 const bool xOverlaps = (bMaxX > aMinX && bMinX < aMaxX) || near(bMaxX, aMinX) ||
137 near(bMinX, aMaxX) || near(aMinX, bMinX) || near(aMaxX, bMaxX);
138 if (xOverlaps && near(aMaxZ, bMinZ)) edgeA = Edge::Right, edgeB = Edge::Left;
139 else if (xOverlaps && near(aMinZ, bMaxZ)) edgeA = Edge::Left, edgeB = Edge::Right;
140 else {
141 const bool zOverlaps = (bMaxZ > aMinZ && bMinZ < aMaxZ) || near(bMaxZ, aMinZ) ||
142 near(bMinZ, aMaxZ) || near(aMinZ, bMinZ) || near(aMaxZ, bMaxZ);
143 if (zOverlaps && near(aMaxX, bMinX)) edgeA = Edge::Top, edgeB = Edge::Bottom;
144 else if (zOverlaps && near(aMinX, bMaxX)) edgeA = Edge::Bottom, edgeB = Edge::Top;
145 }
146 if (edgeA == Edge::None)
147 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.textureAlign: terrain bounds are not adjacent"));
148
149 auto nextA = *terrainA.impl_, nextB = *terrainB.impl_;
150 const int resolution = std::min(nextA.width, nextB.width);
151 const int layers = std::min(nextA.layers, nextB.layers);
152 const bool horizontal = edgeA == Edge::Left || edgeA == Edge::Right;
153 int changedA = 0, changedB = 0;
154 for (int i = 0; i < settings.blendWidth; ++i) {
155 const float t = std::clamp(float(i) / float(settings.blendWidth), 0.f, 1.f);
156 const float blend = settings.blendStrength * (1.f - t * t * (3.f - 2.f * t));
157 for (int j = 0; j < resolution; ++j) {
158 const int depthA = std::min(i, resolution - 1), depthB = std::min(i, resolution - 1);
159 const int xA = horizontal ? (edgeA == Edge::Left ? depthA : resolution - 1 - depthA) : j;
160 const int yA = horizontal ? j : (edgeA == Edge::Bottom ? depthA : resolution - 1 - depthA);
161 const int xB = horizontal ? (edgeB == Edge::Left ? depthB : resolution - 1 - depthB) : j;
162 const int yB = horizontal ? j : (edgeB == Edge::Bottom ? depthB : resolution - 1 - depthB);
163 const float noise = seamNoise(horizontal ? j * 0.1f : i * 0.1f,
164 horizontal ? i * 0.1f : j * 0.1f) * 0.5f + 0.5f;
165 const float weightA = noise * blend, weightB = (1.f - noise) * blend;
166 std::vector<float> oldA(static_cast<std::size_t>(layers));
167 std::vector<float> oldB(static_cast<std::size_t>(layers));
168 for (int layer = 0; layer < layers; ++layer) {
169 oldA[static_cast<std::size_t>(layer)] = terrainA.impl_->weights[terrainA.impl_->index(layer, xA, yA)];
170 oldB[static_cast<std::size_t>(layer)] = terrainB.impl_->weights[terrainB.impl_->index(layer, xB, yB)];
171 }
172 float sumA = 0, sumB = 0;
173 for (int layer = 0; layer < layers; ++layer) {
174 const float a = oldA[static_cast<std::size_t>(layer)], b = oldB[static_cast<std::size_t>(layer)];
175 nextA.weights[nextA.index(layer, xA, yA)] = std::lerp(a, b, weightA);
176 nextB.weights[nextB.index(layer, xB, yB)] = std::lerp(b, a, weightB);
177 sumA += nextA.weights[nextA.index(layer, xA, yA)];
178 sumB += nextB.weights[nextB.index(layer, xB, yB)];
179 }
180 if (sumA > 0 && sumB > 0) for (int layer = 0; layer < layers; ++layer) {
181 nextA.weights[nextA.index(layer, xA, yA)] /= sumA;
182 nextB.weights[nextB.index(layer, xB, yB)] /= sumB;
183 }
184 bool aChanged = false, bChanged = false;
185 for (int layer = 0; layer < layers; ++layer) {
186 aChanged |= nextA.weights[nextA.index(layer, xA, yA)] != oldA[static_cast<std::size_t>(layer)];
187 bChanged |= nextB.weights[nextB.index(layer, xB, yB)] != oldB[static_cast<std::size_t>(layer)];
188 }
189 changedA += aChanged;
190 changedB += bChanged;
191 }
192 }
193 nextA.lastChanged = changedA;
194 nextB.lastChanged = changedB;
195 *terrainA.impl_ = std::move(nextA);
196 *terrainB.impl_ = std::move(nextB);
197 return Result<int>::success(changedA + changedB);
198}
199
201 using namespace raster_detail;
202 if (!target.impl_ || target.impl_->layers <= 0 || !validRaster(paint) ||
203 paint.getWidth() != target.impl_->width || paint.getHeight() != target.impl_->height ||
204 targetLayer < 0 || targetLayer >= target.impl_->layers ||
205 !std::all_of(paint.data().begin(), paint.data().end(), [](float value) {
206 return std::isfinite(value) && value >= 0 && value <= 1;
207 }) || (target.impl_->layers == 1 && std::any_of(paint.data().begin(), paint.data().end(),
208 [](float value) { return value != 1; })))
209 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat: initialized matching topology, layer and normalized paint required"));
210 auto next = *target.impl_;
211 int changed = 0;
212 for (int y = 0; y < next.height; ++y) {
213 for (int x = 0; x < next.width; ++x) {
214 const auto targetIndex = next.index(targetLayer, x, y);
215 const float oldTarget = next.weights[targetIndex], replacement = paint.height(x, y);
216 if (oldTarget != replacement) ++changed;
217 const double oldOther = 1.0 - oldTarget;
218 if (oldOther > 1e-7) {
219 const double factor = (1.0 - replacement) / oldOther;
220 for (int layer = 0; layer < next.layers; ++layer)
221 if (layer != targetLayer) next.weights[next.index(layer, x, y)] =
222 static_cast<float>(next.weights[next.index(layer, x, y)] * factor);
223 } else {
224 const float share = next.layers > 1 ? (1 - replacement) / float(next.layers - 1) : 0;
225 for (int layer = 0; layer < next.layers; ++layer)
226 if (layer != targetLayer) next.weights[next.index(layer, x, y)] = share;
227 }
228 next.weights[targetIndex] = replacement;
229 }
230 }
231 next.lastChanged = changed;
232 *target.impl_ = std::move(next);
234}
235
237 const GtsHeightBlendSet& heights, float blendFactor) {
238 if (!input.impl_ || input.impl_->layers <= 0 || !heights.impl_ ||
239 heights.impl_->layers.size() != static_cast<std::size_t>(input.impl_->layers) ||
240 !std::isfinite(blendFactor) || blendFactor < 0 || blendFactor > 1)
241 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.gtsHeightBlend: initialized splat, matching heights and normalized factor required"));
242 for (const auto& layer : heights.impl_->layers)
243 if (layer.height.getWidth() != input.impl_->width || layer.height.getHeight() != input.impl_->height)
244 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.gtsHeightBlend: topology mismatch"));
245 auto next = *input.impl_;
246 int changed = 0;
247 std::vector<float> weighted(static_cast<std::size_t>(next.layers));
248 for (int y = 0; y < next.height; ++y) for (int x = 0; x < next.width; ++x) {
249 float maxHeight = 0;
250 for (int layer = 0; layer < next.layers; ++layer) {
251 const auto& source = heights.impl_->layers[static_cast<std::size_t>(layer)];
252 const float transformed = std::pow(std::abs(source.height.height(x, y)), source.contrast) *
253 source.brightness + source.increase;
254 weighted[static_cast<std::size_t>(layer)] = transformed * input.impl_->weights[input.impl_->index(layer, x, y)];
255 maxHeight = std::max(maxHeight, weighted[static_cast<std::size_t>(layer)]);
256 }
257 float sum = 0;
258 const float transition = std::max(blendFactor, 1e-5F);
259 for (int layer = 0; layer < next.layers; ++layer) {
260 float& value = weighted[static_cast<std::size_t>(layer)];
261 value = std::max(0.0F, value + transition - maxHeight);
262 value = (value + 1e-6F) * input.impl_->weights[input.impl_->index(layer, x, y)];
263 sum += value;
264 }
265 sum = std::max(sum, 1e-6F);
266 bool texelChanged = false;
267 for (int layer = 0; layer < next.layers; ++layer) {
268 const auto index = next.index(layer, x, y);
269 const float value = weighted[static_cast<std::size_t>(layer)] / sum;
270 texelChanged |= value != next.weights[index]; next.weights[index] = value;
271 }
272 changed += texelChanged;
273 }
274 next.lastChanged = changed;
275 if (!output.impl_) output.impl_ = std::make_unique<TerrainSplatmap::Impl>();
276 *output.impl_ = std::move(next);
278}
279
281 const std::vector<TerrainSplatTile>& tiles, const Heightmap& operationPaint, int targetLayer,
282 const TerrainStampSettings& operationSettings, bool worldMapOperation,
283 const std::vector<std::string>& validTerrainNames) {
284 using namespace raster_detail;
285 if (tiles.empty() || !validRaster(operationPaint) ||
286 !std::all_of(operationPaint.data().begin(), operationPaint.data().end(), [](float value) {
287 return std::isfinite(value) && value >= 0 && value <= 1;
288 }))
289 return Result<TerrainMultiTileReport>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat.multitile: tiles and normalized paint required"));
290 std::vector<TerrainOperationTile> descriptors;
291 std::unordered_set<TerrainSplatmap*> owners;
292 descriptors.reserve(tiles.size());
293 for (const auto& tile : tiles) {
294 if (!tile.splatmap || !tile.splatmap->impl_ || tile.splatmap->impl_->layers <= 0 ||
295 !owners.insert(tile.splatmap).second || targetLayer < 0 || targetLayer >= tile.splatmap->impl_->layers)
296 return Result<TerrainMultiTileReport>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat.multitile: distinct initialized owners and common target layer required"));
297 descriptors.push_back({tile.name, tile.originX, tile.originZ, tile.width, tile.depth,
298 tile.splatmap->impl_->width, tile.splatmap->impl_->height, tile.worldMap});
299 }
300 auto mapped = mapTerrainOperationMultiTile(descriptors, operationSettings, TerrainOperationDomain::Texture,
301 worldMapOperation, validTerrainNames);
302 if (!mapped.ok()) return mapped;
303 auto report = std::move(mapped.value());
304 if (operationPaint.getWidth() != report.operationWidth || operationPaint.getHeight() != report.operationHeight)
305 return Result<TerrainMultiTileReport>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat.multitile: paint dimensions must match the shared operation window"));
306 struct Candidate { TerrainSplatmap* target; TerrainSplatmap::Impl value; };
307 std::vector<Candidate> candidates;
308 candidates.reserve(report.mappings.size());
309 for (const auto& mapping : report.mappings) {
310 const auto tile = std::find_if(tiles.begin(), tiles.end(),
311 [&](const auto& value) { return value.name == mapping.terrainName; });
312 if (tile == tiles.end())
314 DiagnosticCode::InvariantViolation, "terrain.paintSplat.multitile: mapped terrain was not supplied"));
315 if (tile->splatmap->impl_->layers == 1) {
316 for (int y = 0; y < mapping.height; ++y)
317 for (int x = 0; x < mapping.width; ++x)
318 if (operationPaint.height(mapping.operationX + x, mapping.operationY + y) != 1)
319 return Result<TerrainMultiTileReport>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat.multitile: a selected single-layer splatmap must remain one"));
320 }
321 auto next = *tile->splatmap->impl_;
322 int changed = 0;
323 for (int y = 0; y < mapping.height; ++y) {
324 for (int x = 0; x < mapping.width; ++x) {
325 const int localX = mapping.localX + x, localY = mapping.localY + y;
326 const float replacement = operationPaint.height(mapping.operationX + x, mapping.operationY + y);
327 const auto index = next.index(targetLayer, localX, localY);
328 const float oldTarget = next.weights[index];
329 if (oldTarget != replacement) ++changed;
330 const double oldOther = 1.0 - oldTarget;
331 if (oldOther > 1e-7) {
332 const double factor = (1.0 - replacement) / oldOther;
333 for (int layer = 0; layer < next.layers; ++layer)
334 if (layer != targetLayer) next.weights[next.index(layer, localX, localY)] =
335 static_cast<float>(next.weights[next.index(layer, localX, localY)] * factor);
336 } else {
337 const float share = next.layers > 1 ? (1 - replacement) / float(next.layers - 1) : 0;
338 for (int layer = 0; layer < next.layers; ++layer)
339 if (layer != targetLayer) next.weights[next.index(layer, localX, localY)] = share;
340 }
341 next.weights[index] = replacement;
342 }
343 }
344 next.lastChanged = changed;
345 if (report.changedSamples > std::numeric_limits<int>::max() - changed)
346 return Result<TerrainMultiTileReport>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.paintSplat.multitile: changed texel count exceeds range"));
347 report.changedSamples += changed;
348 candidates.push_back({tile->splatmap, std::move(next)});
349 }
350 for (auto& candidate : candidates) *candidate.target->impl_ = std::move(candidate.value);
351 return Result<TerrainMultiTileReport>::success(std::move(report));
352}
353
355 struct Tile {
356 std::string name;
358 double originX = 0, originZ = 0, width = 1, depth = 1;
359 bool worldMap = false;
360 };
361 struct Snapshot {
362 std::vector<TerrainSplatmap> maps;
364 };
365 std::vector<Tile> tiles;
367 std::vector<Snapshot> history;
368 int cursor = 0;
371 value.maps.reserve(tiles.size());
372 for (const auto& tile : tiles) value.maps.push_back(tile.splatmap);
373 value.report = last;
374 return value;
375 }
376 void restore(const Snapshot& value) {
377 for (std::size_t i = 0; i < tiles.size(); ++i) tiles[i].splatmap = value.maps[i];
378 last = value.report;
379 }
380};
381
386
387Result<int> TerrainMultiSplatWorkspace::addTile(const std::string& name, const TerrainSplatmap& splatmap,
388 double originX, double originZ, double width, double depth,
389 bool worldMap) {
390 if (!impl_ || name.empty() || splatmap.getWidth() <= 0 || splatmap.getHeight() <= 0)
391 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: initialized named tile required"));
392 if (std::any_of(impl_->tiles.begin(), impl_->tiles.end(), [&](const auto& tile) { return tile.name == name; }))
393 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: duplicate tile name"));
394 if (impl_->history.size() > 1)
395 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: topology is fixed after painting"));
396 auto candidate = std::make_unique<Impl>(*impl_);
397 candidate->tiles.push_back({name, splatmap, originX, originZ, width, depth, worldMap});
398 std::vector<TerrainOperationTile> descriptors;
399 for (const auto& tile : candidate->tiles)
400 descriptors.push_back({tile.name, tile.originX, tile.originZ, tile.width, tile.depth,
401 tile.splatmap.getWidth(), tile.splatmap.getHeight(), tile.worldMap});
403 bounds.centerX = originX + width * 0.5;
404 bounds.centerZ = originZ + depth * 0.5;
405 bounds.width = width;
406 bounds.depth = depth;
407 auto checked = mapTerrainOperationMultiTile(descriptors, bounds, TerrainOperationDomain::Texture, worldMap, {name});
408 if (!checked.ok()) return Result<int>::failure(checked.status());
409 candidate->history.clear();
410 candidate->history.push_back(candidate->snapshot());
411 candidate->cursor = 0;
412 impl_.swap(candidate);
413 return Result<int>::success(static_cast<int>(impl_->tiles.size()));
414}
415
416Result<int> TerrainMultiSplatWorkspace::paint(const Heightmap& operationPaint, int targetLayer,
417 const TerrainStampSettings& operationSettings,
418 bool worldMapOperation) {
419 if (!impl_) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: moved-from workspace"));
420 auto candidate = std::make_unique<Impl>(*impl_);
421 std::vector<TerrainSplatTile> descriptors;
422 for (auto& tile : candidate->tiles)
423 descriptors.push_back({tile.name, &tile.splatmap, tile.originX, tile.originZ, tile.width, tile.depth,
424 tile.worldMap});
425 auto painted = paintTerrainSplatLayerMultiTile(descriptors, operationPaint, targetLayer, operationSettings,
426 worldMapOperation);
427 if (!painted.ok()) return Result<int>::failure(painted.status());
428 candidate->last = std::move(painted.value());
429 candidate->history.resize(static_cast<std::size_t>(candidate->cursor + 1));
430 candidate->history.push_back(candidate->snapshot());
431 ++candidate->cursor;
432 const int changed = candidate->last.changedSamples;
433 impl_.swap(candidate);
435}
436
437Result<int> TerrainMultiSplatWorkspace::copyTile(const std::string& name, TerrainSplatmap& output) const {
438 if (!impl_) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: moved-from workspace"));
439 const auto found = std::find_if(impl_->tiles.begin(), impl_->tiles.end(),
440 [&](const auto& tile) { return tile.name == name; });
441 if (found == impl_->tiles.end()) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: tile not found"));
442 TerrainSplatmap copy = found->splatmap;
443 output = std::move(copy);
444 return Result<int>::success(output.getWidth() * output.getHeight());
445}
446Result<int> TerrainMultiSplatWorkspace::undo() {
447 if (!impl_ || impl_->cursor <= 0) return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: no undo snapshot"));
448 --impl_->cursor;
449 impl_->restore(impl_->history[static_cast<std::size_t>(impl_->cursor)]);
450 return Result<int>::success(impl_->cursor);
451}
452Result<int> TerrainMultiSplatWorkspace::redo() {
453 if (!impl_ || impl_->cursor + 1 >= static_cast<int>(impl_->history.size()))
454 return Result<int>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.splat.workspace: no redo snapshot"));
455 ++impl_->cursor;
456 impl_->restore(impl_->history[static_cast<std::size_t>(impl_->cursor)]);
457 return Result<int>::success(impl_->cursor);
458}
459int TerrainMultiSplatWorkspace::getTileCount() const noexcept { return impl_ ? static_cast<int>(impl_->tiles.size()) : 0; }
460int TerrainMultiSplatWorkspace::getLastChangedSamples() const noexcept { return impl_ ? impl_->last.changedSamples : 0; }
461int TerrainMultiSplatWorkspace::getLastAffectedTiles() const noexcept { return impl_ ? impl_->last.affectedTiles : 0; }
462int TerrainMultiSplatWorkspace::getOperationCount() const noexcept {
463 return impl_ ? std::max(0, static_cast<int>(impl_->history.size()) - 1) : 0;
464}
465int TerrainMultiSplatWorkspace::getAppliedCount() const noexcept { return impl_ ? impl_->cursor : 0; }
466} // namespace eve::procgen
double y
double x
LogicalId target
std::string output
float contrast
bool changed
std::string name
Definition ECS.cpp:154
EvpackChunkInput input
Definition Evpack.cpp:170
float blend
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanWakeSettings settings
TileLayer * layer
Object::Ptr value
float t
std::vector< WfcTile > tiles
bool found
std::string_view source
std::map< std::array< int, 4 >, std::vector< const Subject * > > candidates
Definition Sensing.cpp:32
std::uint32_t depth
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
Owning ordered GTS layer-height rasters and their alpha-channel transforms.
GtsHeightBlendSet()
Gts height blend set.
int getLayerCount() const noexcept
Return the number of owned height layers.
~GtsHeightBlendSet()
Gts height blend set.
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:21
int getHeight() const
Returns the height.
Definition Heightmap.cpp:17
const std::vector< float > & data() const
Data.
Definition Heightmap.h:48
float height(int x, int y) const
Height.
Definition Heightmap.cpp:28
int getWidth() const
Returns the width.
Definition Heightmap.cpp:16
Owning script-safe multi-terrain splat transaction and all-tile undo history. Input maps are copied....
~TerrainMultiSplatWorkspace()
Terrain multi splat workspace.
TerrainMultiSplatWorkspace()
Terrain multi splat workspace.
Owning normalized terrain texture-layer weights with atomic mutation. Layers share one finite width/h...
int getHeight() const noexcept
Current splat height, or zero before initialization.
Result< int > copyLayer(int layer, Heightmap &output) const
Copy one complete texture layer into a same-sized scalar mask atomically.
int getLayerCount() const noexcept
Current layer count, or zero before initialization.
~TerrainSplatmap()
Terrain splatmap.
Result< int > initialize(int width, int height, int layerCount, int defaultLayer=0)
Initialize topology and assign all weight to defaultLayer.
TerrainSplatmap & operator=(TerrainSplatmap &&) noexcept
Operator =.
Result< float > sample(int layer, int x, int y) const
Return one layer weight, or a structured range/precondition failure.
int getWidth() const noexcept
Current splat width, or zero before initialization.
int getLastChangedSamples() const noexcept
Number of texels changed by the last successful paint operation.
bool validRaster(const Heightmap &map)
Valid raster.
Result< TerrainMultiTileReport > paintTerrainSplatLayerMultiTile(const std::vector< TerrainSplatTile > &tiles, const Heightmap &operationPaint, int targetLayer, const TerrainStampSettings &operationSettings, bool worldMapOperation, const std::vector< std::string > &validTerrainNames)
Paint one shared Pcg Texture-domain operation raster across selected tiles atomically.
Result< TerrainMultiTileReport > mapTerrainOperationMultiTile(const std::vector< TerrainOperationTile > &tiles, const TerrainStampSettings &settings, TerrainOperationDomain domain, bool worldMapOperation, const std::vector< std::string > &validTerrainNames)
Calculate Pcg-compatible local and shared pixel rectangles for any multi-terrain raster domain.
Result< int > paintTerrainSplatLayer(TerrainSplatmap &target, const Heightmap &paint, int targetLayer)
Replace one terrain layer with a normalized paint raster and proportionally renormalize all other lay...
Result< int > applyGtsHeightBlend(TerrainSplatmap &output, const TerrainSplatmap &input, const GtsHeightBlendSet &heights, float blendFactor)
Recompute all splat weights using the GTS texture-height blending equation.
Result< int > alignTerrainSplatTextures(TerrainSplatmap &terrainA, TerrainSplatmap &terrainB, const TerrainTextureAlignSettings &settings)
Blend texture weights on adjacent terrain edges using Pcg's synchronized-noise equation.
Completed multi-tile stamp statistics and Pcg-compatible affected-pixel mappings.
Value configuration for a rectangular stamp in world X/Z coordinates.
World-space placement and Pcg-compatible seam blending controls for two splatmaps.
glm::vec4 bounds